##// END OF EJS Templates
Do not shadow explicit magic completion....
Matthias Bussonnier -
Show More
@@ -1221,7 +1221,9 b' class IPCompleter(Completer):'
1221 1221 cell_magics = lsm['cell']
1222 1222 pre = self.magic_escape
1223 1223 pre2 = pre+pre
1224
1224
1225 explicit_magic = text.startswith(pre)
1226
1225 1227 # Completion logic:
1226 1228 # - user gives %%: only do cell magics
1227 1229 # - user gives %: do both line and cell magics
@@ -1229,11 +1231,23 b' class IPCompleter(Completer):'
1229 1231 # In other words, line magics are skipped if the user gives %% explicitly
1230 1232 #
1231 1233 # We also exclude magics that match any currently visible names:
1232 # https://github.com/ipython/ipython/issues/4877
1234 # https://github.com/ipython/ipython/issues/4877, unless the user has
1235 # typed a %:
1236 # https://github.com/ipython/ipython/issues/10754
1233 1237 bare_text = text.lstrip(pre)
1234 1238 global_matches = self.global_matches(bare_text)
1235 matches = lambda magic: magic.startswith(bare_text) \
1236 and magic not in global_matches
1239 if not explicit_magic:
1240 def matches(magic):
1241 """
1242 Filter magics, in particular remove magics that match
1243 a name present in global namespace.
1244 """
1245 return ( magic.startswith(bare_text) and
1246 magic not in global_matches )
1247 else:
1248 def matches(magic):
1249 return magic.startswith(bare_text)
1250
1237 1251 comp = [ pre2+m for m in cell_magics if matches(m)]
1238 1252 if not text.startswith(pre2):
1239 1253 comp += [ pre+m for m in line_magics if matches(m)]
@@ -589,6 +589,24 b' def test_magic_completion_shadowing():'
589 589 text, matches = c.complete("mat")
590 590 nt.assert_equal(matches, ["%matplotlib"])
591 591
592 def test_magic_completion_shadowing_explicit():
593 """
594 If the user try to complete a shadowed magic, and explicit % start should
595 still return the completions.
596 """
597 ip = get_ipython()
598 c = ip.Completer
599
600 # Before importing matplotlib, %matplotlib magic should be the only option.
601 text, matches = c.complete("%mat")
602 nt.assert_equal(matches, ["%matplotlib"])
603
604 ip.run_cell("matplotlib = 1")
605
606 # After removing matplotlib from namespace, the magic should still be
607 # the only option.
608 text, matches = c.complete("%mat")
609 nt.assert_equal(matches, ["%matplotlib"])
592 610
593 611 def test_magic_config():
594 612 ip = get_ipython()
General Comments 0
You need to be logged in to leave comments. Login now