Show More
@@ -346,20 +346,15 b' class IPCompleter(Completer):' | |||||
346 | Completer.__init__(self, namespace, global_namespace) |
|
346 | Completer.__init__(self, namespace, global_namespace) | |
347 |
|
347 | |||
348 | self.magic_escape = ESC_MAGIC |
|
348 | self.magic_escape = ESC_MAGIC | |
349 |
|
||||
350 | self.splitter = CompletionSplitter() |
|
349 | self.splitter = CompletionSplitter() | |
351 |
|
350 | |||
352 | # Readline-dependent code |
|
351 | # Readline configuration, only used by the rlcompleter method. | |
353 | self.use_readline = use_readline |
|
|||
354 | if use_readline: |
|
352 | if use_readline: | |
|
353 | # We store the right version of readline so that later code | |||
355 | import IPython.utils.rlineimpl as readline |
|
354 | import IPython.utils.rlineimpl as readline | |
356 | self.readline = readline |
|
355 | self.readline = readline | |
357 | delims = self.readline.get_completer_delims() |
|
356 | else: | |
358 | delims = delims.replace(self.magic_escape,'') |
|
357 | self.readline = None | |
359 | self.readline.set_completer_delims(delims) |
|
|||
360 | self.get_line_buffer = self.readline.get_line_buffer |
|
|||
361 | self.get_endidx = self.readline.get_endidx |
|
|||
362 | # /end readline-dependent code |
|
|||
363 |
|
358 | |||
364 | # List where completion matches will be stored |
|
359 | # List where completion matches will be stored | |
365 | self.matches = [] |
|
360 | self.matches = [] | |
@@ -444,18 +439,18 b' class IPCompleter(Completer):' | |||||
444 | else: |
|
439 | else: | |
445 | text_prefix = '' |
|
440 | text_prefix = '' | |
446 |
|
441 | |||
447 | lbuf = self.lbuf |
|
442 | text_until_cursor = self.text_until_cursor | |
448 | open_quotes = 0 # track strings with open quotes |
|
443 | open_quotes = 0 # track strings with open quotes | |
449 | try: |
|
444 | try: | |
450 |
lsplit = shlex.split( |
|
445 | lsplit = shlex.split(text_until_cursor)[-1] | |
451 | except ValueError: |
|
446 | except ValueError: | |
452 | # typically an unmatched ", or backslash without escaped char. |
|
447 | # typically an unmatched ", or backslash without escaped char. | |
453 |
if |
|
448 | if text_until_cursor.count('"')==1: | |
454 | open_quotes = 1 |
|
449 | open_quotes = 1 | |
455 |
lsplit = |
|
450 | lsplit = text_until_cursor.split('"')[-1] | |
456 |
elif |
|
451 | elif text_until_cursor.count("'")==1: | |
457 | open_quotes = 1 |
|
452 | open_quotes = 1 | |
458 |
lsplit = |
|
453 | lsplit = text_until_cursor.split("'")[-1] | |
459 | else: |
|
454 | else: | |
460 | return [] |
|
455 | return [] | |
461 | except IndexError: |
|
456 | except IndexError: | |
@@ -497,7 +492,7 b' class IPCompleter(Completer):' | |||||
497 |
|
492 | |||
498 | def magic_matches(self, text): |
|
493 | def magic_matches(self, text): | |
499 | """Match magics""" |
|
494 | """Match magics""" | |
500 |
#print 'Completer->magic_matches:',text,'lb',self. |
|
495 | #print 'Completer->magic_matches:',text,'lb',self.text_until_cursor # dbg | |
501 | # Get all shell magics now rather than statically, so magics loaded at |
|
496 | # Get all shell magics now rather than statically, so magics loaded at | |
502 | # runtime show up too |
|
497 | # runtime show up too | |
503 | magics = self.shell.lsmagic() |
|
498 | magics = self.shell.lsmagic() | |
@@ -507,19 +502,19 b' class IPCompleter(Completer):' | |||||
507 |
|
502 | |||
508 | def alias_matches(self, text): |
|
503 | def alias_matches(self, text): | |
509 | """Match internal system aliases""" |
|
504 | """Match internal system aliases""" | |
510 |
#print 'Completer->alias_matches:',text,'lb',self. |
|
505 | #print 'Completer->alias_matches:',text,'lb',self.text_until_cursor # dbg | |
511 |
|
506 | |||
512 |
# if we are not in the first 'item', alias matching |
|
507 | # if we are not in the first 'item', alias matching | |
513 | # doesn't make sense - unless we are starting with 'sudo' command. |
|
508 | # doesn't make sense - unless we are starting with 'sudo' command. | |
514 | if ' ' in self.lbuf.lstrip() and \ |
|
509 | main_text = self.text_until_cursor.lstrip() | |
515 |
|
|
510 | if ' ' in main_text and not main_text.startswith('sudo'): | |
516 | return [] |
|
511 | return [] | |
517 | text = os.path.expanduser(text) |
|
512 | text = os.path.expanduser(text) | |
518 | aliases = self.alias_table.keys() |
|
513 | aliases = self.alias_table.keys() | |
519 |
if text == |
|
514 | if text == '': | |
520 | return aliases |
|
515 | return aliases | |
521 | else: |
|
516 | else: | |
522 |
return [a |
|
517 | return [a for a in aliases if a.startswith(text)] | |
523 |
|
518 | |||
524 | def python_matches(self,text): |
|
519 | def python_matches(self,text): | |
525 | """Match attributes or global python names""" |
|
520 | """Match attributes or global python names""" | |
@@ -581,7 +576,7 b' class IPCompleter(Completer):' | |||||
581 | ''', re.VERBOSE | re.DOTALL) |
|
576 | ''', re.VERBOSE | re.DOTALL) | |
582 | # 1. find the nearest identifier that comes before an unclosed |
|
577 | # 1. find the nearest identifier that comes before an unclosed | |
583 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" |
|
578 | # parenthesis e.g. for "foo (1+bar(x), pa", the candidate is "foo" | |
584 |
tokens = regexp.findall(self. |
|
579 | tokens = regexp.findall(self.line_buffer) | |
585 | tokens.reverse() |
|
580 | tokens.reverse() | |
586 | iterTokens = iter(tokens); openPar = 0 |
|
581 | iterTokens = iter(tokens); openPar = 0 | |
587 | for token in iterTokens: |
|
582 | for token in iterTokens: | |
@@ -626,7 +621,7 b' class IPCompleter(Completer):' | |||||
626 |
|
621 | |||
627 | def dispatch_custom_completer(self,text): |
|
622 | def dispatch_custom_completer(self,text): | |
628 | #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg |
|
623 | #print "Custom! '%s' %s" % (text, self.custom_completers) # dbg | |
629 |
line = self. |
|
624 | line = self.line_buffer | |
630 | if not line.strip(): |
|
625 | if not line.strip(): | |
631 | return None |
|
626 | return None | |
632 |
|
627 | |||
@@ -640,13 +635,13 b' class IPCompleter(Completer):' | |||||
640 | # for foo etc, try also to find completer for %foo |
|
635 | # for foo etc, try also to find completer for %foo | |
641 | if not cmd.startswith(self.magic_escape): |
|
636 | if not cmd.startswith(self.magic_escape): | |
642 | try_magic = self.custom_completers.s_matches( |
|
637 | try_magic = self.custom_completers.s_matches( | |
643 | self.magic_escape + cmd) |
|
638 | self.magic_escape + cmd) | |
644 | else: |
|
639 | else: | |
645 | try_magic = [] |
|
640 | try_magic = [] | |
646 |
|
641 | |||
647 | for c in itertools.chain(self.custom_completers.s_matches(cmd), |
|
642 | for c in itertools.chain(self.custom_completers.s_matches(cmd), | |
648 |
|
|
643 | try_magic, | |
649 |
|
|
644 | self.custom_completers.flat_matches(self.text_until_cursor)): | |
650 | #print "try",c # dbg |
|
645 | #print "try",c # dbg | |
651 | try: |
|
646 | try: | |
652 | res = c(event) |
|
647 | res = c(event) | |
@@ -703,8 +698,8 b' class IPCompleter(Completer):' | |||||
703 | line_buffer = text |
|
698 | line_buffer = text | |
704 |
|
699 | |||
705 | magic_escape = self.magic_escape |
|
700 | magic_escape = self.magic_escape | |
706 |
self. |
|
701 | self.line_buffer = line_buffer | |
707 |
self. |
|
702 | self.text_until_cursor = self.line_buffer[:cursor_pos] | |
708 | #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg |
|
703 | #io.rprint('\nCOMP2 %r %r %r' % (text, line_buffer, cursor_pos)) # dbg | |
709 |
|
704 | |||
710 | # Start with a clean slate of completions |
|
705 | # Start with a clean slate of completions | |
@@ -750,8 +745,8 b' class IPCompleter(Completer):' | |||||
750 | """ |
|
745 | """ | |
751 | if state==0: |
|
746 | if state==0: | |
752 |
|
747 | |||
753 |
self. |
|
748 | self.line_buffer = line_buffer = self.readline.get_line_buffer() | |
754 | cursor_pos = self.get_endidx() |
|
749 | cursor_pos = self.readline.get_endidx() | |
755 |
|
750 | |||
756 | #io.rprint("\nRLCOMPLETE: %r %r %r" % |
|
751 | #io.rprint("\nRLCOMPLETE: %r %r %r" % | |
757 | # (text, line_buffer, cursor_pos) ) # dbg |
|
752 | # (text, line_buffer, cursor_pos) ) # dbg |
@@ -260,6 +260,11 b' class InteractiveShell(Configurable, Magic):' | |||||
260 | # init_readline() must come before init_io(), because init_io uses |
|
260 | # init_readline() must come before init_io(), because init_io uses | |
261 | # readline related things. |
|
261 | # readline related things. | |
262 | self.init_readline() |
|
262 | self.init_readline() | |
|
263 | # init_completer must come after init_readline, because it needs to | |||
|
264 | # know whether readline is present or not system-wide to configure the | |||
|
265 | # completers, since the completion machinery can now operate | |||
|
266 | # independently of readline (e.g. over the network) | |||
|
267 | self.init_completer() | |||
263 | # TODO: init_io() needs to happen before init_traceback handlers |
|
268 | # TODO: init_io() needs to happen before init_traceback handlers | |
264 | # because the traceback handlers hardcode the stdout/stderr streams. |
|
269 | # because the traceback handlers hardcode the stdout/stderr streams. | |
265 | # This logic in in debugger.Pdb and should eventually be changed. |
|
270 | # This logic in in debugger.Pdb and should eventually be changed. | |
@@ -1511,78 +1516,6 b' class InteractiveShell(Configurable, Magic):' | |||||
1511 | self._showtraceback(etype, value, stb) |
|
1516 | self._showtraceback(etype, value, stb) | |
1512 |
|
1517 | |||
1513 | #------------------------------------------------------------------------- |
|
1518 | #------------------------------------------------------------------------- | |
1514 | # Things related to tab completion |
|
|||
1515 | #------------------------------------------------------------------------- |
|
|||
1516 |
|
||||
1517 | def complete(self, text, line=None, cursor_pos=None): |
|
|||
1518 | """Return the completed text and a list of completions. |
|
|||
1519 |
|
||||
1520 | Parameters |
|
|||
1521 | ---------- |
|
|||
1522 |
|
||||
1523 | text : string |
|
|||
1524 | A string of text to be completed on. It can be given as empty and |
|
|||
1525 | instead a line/position pair are given. In this case, the |
|
|||
1526 | completer itself will split the line like readline does. |
|
|||
1527 |
|
||||
1528 | line : string, optional |
|
|||
1529 | The complete line that text is part of. |
|
|||
1530 |
|
||||
1531 | cursor_pos : int, optional |
|
|||
1532 | The position of the cursor on the input line. |
|
|||
1533 |
|
||||
1534 | Returns |
|
|||
1535 | ------- |
|
|||
1536 | text : string |
|
|||
1537 | The actual text that was completed. |
|
|||
1538 |
|
||||
1539 | matches : list |
|
|||
1540 | A sorted list with all possible completions. |
|
|||
1541 |
|
||||
1542 | The optional arguments allow the completion to take more context into |
|
|||
1543 | account, and are part of the low-level completion API. |
|
|||
1544 |
|
||||
1545 | This is a wrapper around the completion mechanism, similar to what |
|
|||
1546 | readline does at the command line when the TAB key is hit. By |
|
|||
1547 | exposing it as a method, it can be used by other non-readline |
|
|||
1548 | environments (such as GUIs) for text completion. |
|
|||
1549 |
|
||||
1550 | Simple usage example: |
|
|||
1551 |
|
||||
1552 | In [1]: x = 'hello' |
|
|||
1553 |
|
||||
1554 | In [2]: _ip.complete('x.l') |
|
|||
1555 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) |
|
|||
1556 | """ |
|
|||
1557 |
|
||||
1558 | # Inject names into __builtin__ so we can complete on the added names. |
|
|||
1559 | with self.builtin_trap: |
|
|||
1560 | return self.Completer.complete(text, line, cursor_pos) |
|
|||
1561 |
|
||||
1562 | def set_custom_completer(self, completer, pos=0): |
|
|||
1563 | """Adds a new custom completer function. |
|
|||
1564 |
|
||||
1565 | The position argument (defaults to 0) is the index in the completers |
|
|||
1566 | list where you want the completer to be inserted.""" |
|
|||
1567 |
|
||||
1568 | newcomp = new.instancemethod(completer,self.Completer, |
|
|||
1569 | self.Completer.__class__) |
|
|||
1570 | self.Completer.matchers.insert(pos,newcomp) |
|
|||
1571 |
|
||||
1572 | def set_readline_completer(self): |
|
|||
1573 | """Reset readline's completer to be our own.""" |
|
|||
1574 | self.readline.set_completer(self.Completer.rlcomplete) |
|
|||
1575 |
|
||||
1576 | def set_completer_frame(self, frame=None): |
|
|||
1577 | """Set the frame of the completer.""" |
|
|||
1578 | if frame: |
|
|||
1579 | self.Completer.namespace = frame.f_locals |
|
|||
1580 | self.Completer.global_namespace = frame.f_globals |
|
|||
1581 | else: |
|
|||
1582 | self.Completer.namespace = self.user_ns |
|
|||
1583 | self.Completer.global_namespace = self.user_global_ns |
|
|||
1584 |
|
||||
1585 | #------------------------------------------------------------------------- |
|
|||
1586 | # Things related to readline |
|
1519 | # Things related to readline | |
1587 | #------------------------------------------------------------------------- |
|
1520 | #------------------------------------------------------------------------- | |
1588 |
|
1521 | |||
@@ -1610,16 +1543,6 b' class InteractiveShell(Configurable, Magic):' | |||||
1610 | self.readline = readline |
|
1543 | self.readline = readline | |
1611 | sys.modules['readline'] = readline |
|
1544 | sys.modules['readline'] = readline | |
1612 |
|
1545 | |||
1613 | from IPython.core.completer import IPCompleter |
|
|||
1614 | self.Completer = IPCompleter(self, |
|
|||
1615 | self.user_ns, |
|
|||
1616 | self.user_global_ns, |
|
|||
1617 | self.readline_omit__names, |
|
|||
1618 | self.alias_manager.alias_table) |
|
|||
1619 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) |
|
|||
1620 | self.strdispatchers['complete_command'] = sdisp |
|
|||
1621 | self.Completer.custom_completers = sdisp |
|
|||
1622 |
|
||||
1623 | # Platform-specific configuration |
|
1546 | # Platform-specific configuration | |
1624 | if os.name == 'nt': |
|
1547 | if os.name == 'nt': | |
1625 | # FIXME - check with Frederick to see if we can harmonize |
|
1548 | # FIXME - check with Frederick to see if we can harmonize | |
@@ -1646,8 +1569,6 b' class InteractiveShell(Configurable, Magic):' | |||||
1646 | warn('Problems reading readline initialization file <%s>' |
|
1569 | warn('Problems reading readline initialization file <%s>' | |
1647 | % inputrc_name) |
|
1570 | % inputrc_name) | |
1648 |
|
1571 | |||
1649 | self.set_readline_completer() |
|
|||
1650 |
|
||||
1651 | # Configure readline according to user's prefs |
|
1572 | # Configure readline according to user's prefs | |
1652 | # This is only done if GNU readline is being used. If libedit |
|
1573 | # This is only done if GNU readline is being used. If libedit | |
1653 | # is being used (as on Leopard) the readline config is |
|
1574 | # is being used (as on Leopard) the readline config is | |
@@ -1662,6 +1583,7 b' class InteractiveShell(Configurable, Magic):' | |||||
1662 | delims = readline.get_completer_delims().encode("ascii", "ignore") |
|
1583 | delims = readline.get_completer_delims().encode("ascii", "ignore") | |
1663 | delims = delims.translate(string._idmap, |
|
1584 | delims = delims.translate(string._idmap, | |
1664 | self.readline_remove_delims) |
|
1585 | self.readline_remove_delims) | |
|
1586 | delims = delims.replace(ESC_MAGIC, '') | |||
1665 | readline.set_completer_delims(delims) |
|
1587 | readline.set_completer_delims(delims) | |
1666 | # otherwise we end up with a monster history after a while: |
|
1588 | # otherwise we end up with a monster history after a while: | |
1667 | readline.set_history_length(1000) |
|
1589 | readline.set_history_length(1000) | |
@@ -1708,6 +1630,100 b' class InteractiveShell(Configurable, Magic):' | |||||
1708 | return self.indent_current_nsp * ' ' |
|
1630 | return self.indent_current_nsp * ' ' | |
1709 |
|
1631 | |||
1710 | #------------------------------------------------------------------------- |
|
1632 | #------------------------------------------------------------------------- | |
|
1633 | # Things related to text completion | |||
|
1634 | #------------------------------------------------------------------------- | |||
|
1635 | ||||
|
1636 | def init_completer(self): | |||
|
1637 | """Initialize the completion machinery. | |||
|
1638 | ||||
|
1639 | This creates completion machinery that can be used by client code, | |||
|
1640 | either interactively in-process (typically triggered by the readline | |||
|
1641 | library), programatically (such as in test suites) or out-of-prcess | |||
|
1642 | (typically over the network by remote frontends). | |||
|
1643 | """ | |||
|
1644 | from IPython.core.completer import IPCompleter | |||
|
1645 | self.Completer = IPCompleter(self, | |||
|
1646 | self.user_ns, | |||
|
1647 | self.user_global_ns, | |||
|
1648 | self.readline_omit__names, | |||
|
1649 | self.alias_manager.alias_table, | |||
|
1650 | self.has_readline) | |||
|
1651 | sdisp = self.strdispatchers.get('complete_command', StrDispatch()) | |||
|
1652 | self.strdispatchers['complete_command'] = sdisp | |||
|
1653 | self.Completer.custom_completers = sdisp | |||
|
1654 | ||||
|
1655 | if self.has_readline: | |||
|
1656 | self.set_readline_completer() | |||
|
1657 | ||||
|
1658 | def complete(self, text, line=None, cursor_pos=None): | |||
|
1659 | """Return the completed text and a list of completions. | |||
|
1660 | ||||
|
1661 | Parameters | |||
|
1662 | ---------- | |||
|
1663 | ||||
|
1664 | text : string | |||
|
1665 | A string of text to be completed on. It can be given as empty and | |||
|
1666 | instead a line/position pair are given. In this case, the | |||
|
1667 | completer itself will split the line like readline does. | |||
|
1668 | ||||
|
1669 | line : string, optional | |||
|
1670 | The complete line that text is part of. | |||
|
1671 | ||||
|
1672 | cursor_pos : int, optional | |||
|
1673 | The position of the cursor on the input line. | |||
|
1674 | ||||
|
1675 | Returns | |||
|
1676 | ------- | |||
|
1677 | text : string | |||
|
1678 | The actual text that was completed. | |||
|
1679 | ||||
|
1680 | matches : list | |||
|
1681 | A sorted list with all possible completions. | |||
|
1682 | ||||
|
1683 | The optional arguments allow the completion to take more context into | |||
|
1684 | account, and are part of the low-level completion API. | |||
|
1685 | ||||
|
1686 | This is a wrapper around the completion mechanism, similar to what | |||
|
1687 | readline does at the command line when the TAB key is hit. By | |||
|
1688 | exposing it as a method, it can be used by other non-readline | |||
|
1689 | environments (such as GUIs) for text completion. | |||
|
1690 | ||||
|
1691 | Simple usage example: | |||
|
1692 | ||||
|
1693 | In [1]: x = 'hello' | |||
|
1694 | ||||
|
1695 | In [2]: _ip.complete('x.l') | |||
|
1696 | Out[2]: ('x.l', ['x.ljust', 'x.lower', 'x.lstrip']) | |||
|
1697 | """ | |||
|
1698 | ||||
|
1699 | # Inject names into __builtin__ so we can complete on the added names. | |||
|
1700 | with self.builtin_trap: | |||
|
1701 | return self.Completer.complete(text, line, cursor_pos) | |||
|
1702 | ||||
|
1703 | def set_custom_completer(self, completer, pos=0): | |||
|
1704 | """Adds a new custom completer function. | |||
|
1705 | ||||
|
1706 | The position argument (defaults to 0) is the index in the completers | |||
|
1707 | list where you want the completer to be inserted.""" | |||
|
1708 | ||||
|
1709 | newcomp = new.instancemethod(completer,self.Completer, | |||
|
1710 | self.Completer.__class__) | |||
|
1711 | self.Completer.matchers.insert(pos,newcomp) | |||
|
1712 | ||||
|
1713 | def set_readline_completer(self): | |||
|
1714 | """Reset readline's completer to be our own.""" | |||
|
1715 | self.readline.set_completer(self.Completer.rlcomplete) | |||
|
1716 | ||||
|
1717 | def set_completer_frame(self, frame=None): | |||
|
1718 | """Set the frame of the completer.""" | |||
|
1719 | if frame: | |||
|
1720 | self.Completer.namespace = frame.f_locals | |||
|
1721 | self.Completer.global_namespace = frame.f_globals | |||
|
1722 | else: | |||
|
1723 | self.Completer.namespace = self.user_ns | |||
|
1724 | self.Completer.global_namespace = self.user_global_ns | |||
|
1725 | ||||
|
1726 | #------------------------------------------------------------------------- | |||
1711 | # Things related to magics |
|
1727 | # Things related to magics | |
1712 | #------------------------------------------------------------------------- |
|
1728 | #------------------------------------------------------------------------- | |
1713 |
|
1729 |
General Comments 0
You need to be logged in to leave comments.
Login now