Show More
@@ -177,6 +177,45 b' def compress_user(path, tilde_expand, tilde_val):' | |||||
177 | return path |
|
177 | return path | |
178 |
|
178 | |||
179 |
|
179 | |||
|
180 | ||||
|
181 | def penalize_magics_key(word): | |||
|
182 | """key for sorting that penalizes magic commands in the ordering | |||
|
183 | ||||
|
184 | Normal words are left alone. | |||
|
185 | ||||
|
186 | Magic commands have the initial % moved to the end, e.g. | |||
|
187 | %matplotlib is transformed as follows: | |||
|
188 | ||||
|
189 | %matplotlib -> matplotlib% | |||
|
190 | ||||
|
191 | [The choice of the final % is arbitrary.] | |||
|
192 | ||||
|
193 | Since "matplotlib" < "matplotlib%" as strings, | |||
|
194 | "timeit" will appear before the magic "%timeit" in the ordering | |||
|
195 | ||||
|
196 | For consistency, move "%%" to the end, so cell magics appear *after* | |||
|
197 | line magics with the same name. | |||
|
198 | ||||
|
199 | A check is performed that there are no other "%" in the string; | |||
|
200 | if there are, then the string is not a magic command and is left unchanged. | |||
|
201 | ||||
|
202 | """ | |||
|
203 | ||||
|
204 | # Move any % signs from start to end of the key | |||
|
205 | # provided there are no others elsewhere in the string | |||
|
206 | ||||
|
207 | if word[:2] == "%%": | |||
|
208 | if not "%" in word[2:]: | |||
|
209 | return word[2:] + "%%" | |||
|
210 | ||||
|
211 | if word[:1] == "%": | |||
|
212 | if not "%" in word[1:]: | |||
|
213 | return word[1:] + "%" | |||
|
214 | ||||
|
215 | return word | |||
|
216 | ||||
|
217 | ||||
|
218 | ||||
180 | class Bunch(object): pass |
|
219 | class Bunch(object): pass | |
181 |
|
220 | |||
182 |
|
221 | |||
@@ -888,7 +927,10 b' class IPCompleter(Completer):' | |||||
888 | # different types of objects. The rlcomplete() method could then |
|
927 | # different types of objects. The rlcomplete() method could then | |
889 | # simply collapse the dict into a list for readline, but we'd have |
|
928 | # simply collapse the dict into a list for readline, but we'd have | |
890 | # richer completion semantics in other evironments. |
|
929 | # richer completion semantics in other evironments. | |
891 | self.matches = sorted(set(self.matches)) |
|
930 | ||
|
931 | # use penalize_magics_key to put magics after variables with same name | |||
|
932 | self.matches = sorted(set(self.matches), key=penalize_magics_key) | |||
|
933 | ||||
892 | #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg |
|
934 | #io.rprint('COMP TEXT, MATCHES: %r, %r' % (text, self.matches)) # dbg | |
893 | return text, self.matches |
|
935 | return text, self.matches | |
894 |
|
936 |
@@ -362,3 +362,31 b' def test_line_cell_magics():' | |||||
362 | nt.assert_not_in('%_bar_cellm', matches) |
|
362 | nt.assert_not_in('%_bar_cellm', matches) | |
363 | nt.assert_in('%%_bar_cellm', matches) |
|
363 | nt.assert_in('%%_bar_cellm', matches) | |
364 |
|
364 | |||
|
365 | ||||
|
366 | def test_magic_completion_order(): | |||
|
367 | ||||
|
368 | ip = get_ipython() | |||
|
369 | c = ip.Completer | |||
|
370 | ||||
|
371 | # Test ordering of magics and non-magics with the same name | |||
|
372 | # We want the non-magic first | |||
|
373 | ||||
|
374 | # Before importing matplotlib, there should only be one option: | |||
|
375 | ||||
|
376 | text, matches = c.complete('mat') | |||
|
377 | nt.assert_equal(matches, ["%matplotlib"]) | |||
|
378 | ||||
|
379 | ||||
|
380 | ip.run_cell("matplotlib = 1") # introduce name into namespace | |||
|
381 | ||||
|
382 | # After the import, there should be two options, ordered like this: | |||
|
383 | text, matches = c.complete('mat') | |||
|
384 | nt.assert_equal(matches, ["matplotlib", "%matplotlib"]) | |||
|
385 | ||||
|
386 | ||||
|
387 | ip.run_cell("timeit = 1") # define a user variable called 'timeit' | |||
|
388 | ||||
|
389 | # Order of user variable and line and cell magics with same name: | |||
|
390 | text, matches = c.complete('timeit') | |||
|
391 | nt.assert_equal(matches, ["timeit", "%timeit","%%timeit"]) | |||
|
392 |
General Comments 0
You need to be logged in to leave comments.
Login now