##// END OF EJS Templates
Add completion support for cell magics and handling of line/cell magics....
Fernando Perez -
Show More
@@ -53,15 +53,14 b' used, and this module (and the readline module) are silently inactive.'
53 # proper procedure is to maintain its copyright as belonging to the Python
53 # proper procedure is to maintain its copyright as belonging to the Python
54 # Software Foundation (in addition to my own, for all new code).
54 # Software Foundation (in addition to my own, for all new code).
55 #
55 #
56 # Copyright (C) 2008-2011 IPython Development Team
56 # Copyright (C) 2008 IPython Development Team
57 # Copyright (C) 2001-2007 Fernando Perez. <fperez@colorado.edu>
57 # Copyright (C) 2001 Fernando Perez. <fperez@colorado.edu>
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
58 # Copyright (C) 2001 Python Software Foundation, www.python.org
59 #
59 #
60 # Distributed under the terms of the BSD License. The full license is in
60 # Distributed under the terms of the BSD License. The full license is in
61 # the file COPYING, distributed as part of this software.
61 # the file COPYING, distributed as part of this software.
62 #
62 #
63 #*****************************************************************************
63 #*****************************************************************************
64 from __future__ import print_function
65
64
66 #-----------------------------------------------------------------------------
65 #-----------------------------------------------------------------------------
67 # Imports
66 # Imports
@@ -606,12 +605,23 b' class IPCompleter(Completer):'
606 """Match magics"""
605 """Match magics"""
607 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
606 #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg
608 # Get all shell magics now rather than statically, so magics loaded at
607 # Get all shell magics now rather than statically, so magics loaded at
609 # runtime show up too
608 # runtime show up too.
610 # FIXME - cell magics not implemented here yet.
609 lsm = self.shell.magics_manager.lsmagic()
611 magics = self.shell.magics_manager.lsmagic()['line']
610 line_magics = lsm['line']
611 cell_magics = lsm['cell']
612 pre = self.magic_escape
612 pre = self.magic_escape
613 baretext = text.lstrip(pre)
613 pre2 = pre+pre
614 return [ pre+m for m in magics if m.startswith(baretext)]
614
615 # Completion logic:
616 # - user gives %%: only do cell magics
617 # - user gives %: do both line and cell magics
618 # - no prefix: do both
619 # In other words, line magics are skipped if the user gives %% explicitly
620 bare_text = text.lstrip(pre)
621 comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
622 if not text.startswith(pre2):
623 comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
624 return comp
615
625
616 def alias_matches(self, text):
626 def alias_matches(self, text):
617 """Match internal system aliases"""
627 """Match internal system aliases"""
@@ -290,3 +290,54 b' def test_func_kw_completions():'
290 # Simulate completing with cursor right after b (pos==10):
290 # Simulate completing with cursor right after b (pos==10):
291 s, matches = c.complete(None,'myfunc(1,b)', 10)
291 s, matches = c.complete(None,'myfunc(1,b)', 10)
292 nt.assert_in('b=', matches)
292 nt.assert_in('b=', matches)
293
294
295 def test_line_magics():
296 ip = get_ipython()
297 c = ip.Completer
298 s, matches = c.complete(None, 'lsmag')
299 nt.assert_in('%lsmagic', matches)
300 s, matches = c.complete(None, '%lsmag')
301 nt.assert_in('%lsmagic', matches)
302
303
304 def test_cell_magics():
305 from IPython.core.magic import register_cell_magic
306
307 @register_cell_magic
308 def _foo_cellm(line, cell):
309 pass
310
311 ip = get_ipython()
312 c = ip.Completer
313
314 s, matches = c.complete(None, '_foo_ce')
315 nt.assert_in('%%_foo_cellm', matches)
316 s, matches = c.complete(None, '%%_foo_ce')
317 nt.assert_in('%%_foo_cellm', matches)
318
319
320 def test_line_cell_magics():
321 from IPython.core.magic import register_line_cell_magic
322
323 @register_line_cell_magic
324 def _bar_cellm(line, cell):
325 pass
326
327 ip = get_ipython()
328 c = ip.Completer
329
330 # The policy here is trickier, see comments in completion code. The
331 # returned values depend on whether the user passes %% or not explicitly,
332 # and this will show a difference if the same name is both a line and cell
333 # magic.
334 s, matches = c.complete(None, '_bar_ce')
335 nt.assert_in('%_bar_cellm', matches)
336 nt.assert_in('%%_bar_cellm', matches)
337 s, matches = c.complete(None, '%_bar_ce')
338 nt.assert_in('%_bar_cellm', matches)
339 nt.assert_in('%%_bar_cellm', matches)
340 s, matches = c.complete(None, '%%_bar_ce')
341 nt.assert_not_in('%_bar_cellm', matches)
342 nt.assert_in('%%_bar_cellm', matches)
343
General Comments 0
You need to be logged in to leave comments. Login now