##// END OF EJS Templates
Make variables shadow magics in autocompletion
Alex Alekseyev -
Show More
@@ -0,0 +1,1 b''
1 Variables now shadow magics in autocompletion. See :ghissue:`4877` and :ghpull:`10542`.
@@ -1158,10 +1158,17 b' class IPCompleter(Completer):'
1158 # - user gives %: do both line and cell magics
1158 # - user gives %: do both line and cell magics
1159 # - no prefix: do both
1159 # - no prefix: do both
1160 # In other words, line magics are skipped if the user gives %% explicitly
1160 # In other words, line magics are skipped if the user gives %% explicitly
1161 #
1162 # We also exclude magics that match any currently visible names:
1163 # https://github.com/ipython/ipython/issues/4877
1161 bare_text = text.lstrip(pre)
1164 bare_text = text.lstrip(pre)
1162 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
1165 global_matches = self.global_matches(bare_text)
1166 matches = lambda magic: magic.startswith(bare_text) \
1167 and magic not in global_matches
1168 comp = [ pre2+m for m in cell_magics if matches(m)]
1163 if not text.startswith(pre2):
1169 if not text.startswith(pre2):
1164 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
1170 comp += [ pre+m for m in line_magics if matches(m)]
1171
1165 return [cast_unicode_py2(c) for c in comp]
1172 return [cast_unicode_py2(c) for c in comp]
1166
1173
1167 def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str):
1174 def _jedi_matches(self, cursor_column:int, cursor_line:int, text:str):
@@ -519,31 +519,33 b' def test_line_cell_magics():'
519
519
520
520
521 def test_magic_completion_order():
521 def test_magic_completion_order():
522
523 ip = get_ipython()
522 ip = get_ipython()
524 c = ip.Completer
523 c = ip.Completer
525
524
526 # Test ordering of magics and non-magics with the same name
525 # Test ordering of line and cell magics.
527 # We want the non-magic first
526 text, matches = c.complete("timeit")
528
527 nt.assert_equal(matches, ["%timeit", "%%timeit"])
529 # Before importing matplotlib, there should only be one option:
530
531 text, matches = c.complete('mat')
532 nt.assert_equal(matches, ["%matplotlib"])
533
528
534
529
535 ip.run_cell("matplotlib = 1") # introduce name into namespace
530 def test_magic_completion_shadowing():
531 ip = get_ipython()
532 c = ip.Completer
536
533
537 # After the import, there should be two options, ordered like this:
534 # Before importing matplotlib, %matplotlib magic should be the only option.
538 text, matches = c.complete('mat')
535 text, matches = c.complete("mat")
539 nt.assert_equal(matches, ["matplotlib", "%matplotlib"])
536 nt.assert_equal(matches, ["%matplotlib"])
540
537
538 # The newly introduced name should shadow the magic.
539 ip.run_cell("matplotlib = 1")
540 text, matches = c.complete("mat")
541 nt.assert_equal(matches, ["matplotlib"])
541
542
542 ip.run_cell("timeit = 1") # define a user variable called 'timeit'
543 # After removing matplotlib from namespace, the magic should again be
544 # the only option.
545 del ip.user_ns["matplotlib"]
546 text, matches = c.complete("mat")
547 nt.assert_equal(matches, ["%matplotlib"])
543
548
544 # Order of user variable and line and cell magics with same name:
545 text, matches = c.complete('timeit')
546 nt.assert_equal(matches, ["timeit", "%timeit", "%%timeit"])
547
549
548 def test_match_dict_keys():
550 def test_match_dict_keys():
549 """
551 """
General Comments 0
You need to be logged in to leave comments. Login now