##// END OF EJS Templates
the __future__ is now.
the __future__ is now.

File last commit:

r22963:2961b531
r22963:2961b531
Show More
completer.py
1236 lines | 42.4 KiB | text/x-python | PythonLexer
Brian E. Granger
Initial messing around....
r17700 # encoding: utf-8
ville
initialization (no svn history)
r988 """Word completion for IPython.
Matthias Bussonnier
Typo + Add Warning class for deprecation....
r22633 This module started as fork of the rlcompleter module in the Python standard
ville
initialization (no svn history)
r988 library. The original enhancements made to rlcompleter have been sent
Matthias Bussonnier
Remove readline related code. Pass 1
r22628 upstream and were accepted as of Python 2.3,
ville
initialization (no svn history)
r988
"""
Paul Ivanov
fix for underscore completion
r17735 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
Paul Ivanov
refer to rlcompleter orginins...
r17756 #
# Some of this code originated from rlcompleter in the Python standard library
# Copyright (C) 2001 Python Software Foundation, www.python.org
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365
Kelly Liu
Initial patch with Jedi completion (no function header description)....
r22292
ville
initialization (no svn history)
r988 import __main__
import glob
Brian Granger
Work to address the review comments on Fernando's branch....
r2498 import inspect
Brian Granger
Minor bug fixes and comments to address review....
r2248 import itertools
ville
initialization (no svn history)
r988 import keyword
import os
import re
import sys
Matthias Bussonnier
worjk latex completion and back
r21101 import unicodedata
import string
Thomas Kluyver
Remove unused IPython.utils.rlineimpl readline module...
r22934 import warnings
Diego Garcia
use `import_module` instead of `__import__` ( FIX #10008 )
r22954 from importlib import import_module
Brian Granger
Continuing a massive refactor of everything.
r2205
Min RK
adopt traitlets 4.2 API...
r22340 from traitlets.config.configurable import Configurable
Brian Granger
Continuing a massive refactor of everything.
r2205 from IPython.core.error import TryNext
MinRK
define ESC_FOO consts only in inputsplitter...
r7437 from IPython.core.inputsplitter import ESC_MAGIC
Matthias Bussonnier
worjk latex completion and back
r21101 from IPython.core.latex_symbols import latex_symbols, reverse_latex_symbol
Fernando Perez
Restored major default completer functionality (cd, import, run)....
r2959 from IPython.utils import generics
Thomas Kluyver
Remove some unhelpful info from IPython.core.completer
r17137 from IPython.utils.decorators import undoc
Thomas Kluyver
Consolidate code to check for method in IPython.utils.dir2
r22148 from IPython.utils.dir2 import dir2, get_real_method
Fernando Perez
Fix completer crash with certain unicode inputs....
r3074 from IPython.utils.process import arg_split
Matthias Bussonnier
Force completion results to be unicode....
r22243 from IPython.utils.py3compat import builtin_mod, string_types, PY3, cast_unicode_py2
Min RK
adopt traitlets 4.2 API...
r22340 from traitlets import Bool, Enum, observe
ville
initialization (no svn history)
r988
Matthias Bussonnier
Do not emit single completions trailing with space....
r22632 from functools import wraps
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 #-----------------------------------------------------------------------------
# Globals
#-----------------------------------------------------------------------------
# Public API
ville
initialization (no svn history)
r988 __all__ = ['Completer','IPCompleter']
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 if sys.platform == 'win32':
PROTECTABLES = ' '
else:
Fernando Perez
Fix protection of special characters on tab completion....
r3176 PROTECTABLES = ' ()[]{}?=\\|;:\'#*"^&'
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365
Joel Nothman
Make dict key completion more conservative...
r16466
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 #-----------------------------------------------------------------------------
Matthias Bussonnier
Do not emit single completions trailing with space....
r22632 # Work around BUG decorators.
#-----------------------------------------------------------------------------
def _strip_single_trailing_space(complete):
"""
This is a workaround for a weird IPython/Prompt_toolkit behavior,
that can be removed once we rely on a slightly more recent prompt_toolkit
version (likely > 1.0.3). So this can likely be removed in IPython 6.0
cf https://github.com/ipython/ipython/issues/9658
and https://github.com/jonathanslenders/python-prompt-toolkit/pull/328
The bug is due to the fact that in PTK the completer will reinvoke itself
after trying to completer to the longuest common prefix of all the
completions, unless only one completion is available.
This logic is faulty if the completion ends with space, which can happen in
case like::
from foo import im<ta>
which only matching completion is `import `. Note the leading space at the
end. So leaving a space at the end is a reasonable request, but for now
we'll strip it.
"""
@wraps(complete)
def comp(*args, **kwargs):
text, matches = complete(*args, **kwargs)
if len(matches) == 1:
return text, [matches[0].rstrip()]
return text, matches
return comp
#-----------------------------------------------------------------------------
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 # Main functions and classes
#-----------------------------------------------------------------------------
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 def has_open_quotes(s):
"""Return whether a string has open quotes.
This simply counts whether the number of quote characters of either type in
the string is odd.
Returns
-------
If there is an open quote, the quote character is returned. Else, return
False.
"""
# We check " first, then ', so complex cases with nested quotes will get
# the " to take precedence.
if s.count('"') % 2:
return '"'
elif s.count("'") % 2:
return "'"
else:
return False
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 def protect_filename(s):
"""Escape a string to protect certain characters."""
Antony Lee
On Windows, quote paths instead of escaping them.
r22418 if set(s) & set(PROTECTABLES):
if sys.platform == "win32":
return '"' + s + '"'
else:
Antony Lee
PR cleanup according to comments.
r22438 return "".join(("\\" + c if c in PROTECTABLES else c) for c in s)
Antony Lee
On Windows, quote paths instead of escaping them.
r22418 else:
return s
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365
Fernando Perez
Fix bugs with completions of paths that have ~ in them....
r2965 def expand_user(path):
"""Expand '~'-style usernames in strings.
This is similar to :func:`os.path.expanduser`, but it computes and returns
extra information that will be useful if the input was being used in
computing completions, and you wish to return the completions with the
original '~' instead of its expanded value.
Parameters
----------
path : str
String to be expanded. If no ~ is present, the output is the same as the
input.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Fix bugs with completions of paths that have ~ in them....
r2965 Returns
-------
newpath : str
Result of ~ expansion in the input path.
tilde_expand : bool
Whether any expansion was performed or not.
tilde_val : str
The value that ~ was replaced with.
"""
# Default values
tilde_expand = False
tilde_val = ''
newpath = path
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Fix bugs with completions of paths that have ~ in them....
r2965 if path.startswith('~'):
tilde_expand = True
MinRK
fix inappropriate str.replace in completer.expand_user...
r5201 rest = len(path)-1
Fernando Perez
Fix bugs with completions of paths that have ~ in them....
r2965 newpath = os.path.expanduser(path)
MinRK
fix inappropriate str.replace in completer.expand_user...
r5201 if rest:
tilde_val = newpath[:-rest]
else:
tilde_val = newpath
Fernando Perez
Fix bugs with completions of paths that have ~ in them....
r2965
return newpath, tilde_expand, tilde_val
def compress_user(path, tilde_expand, tilde_val):
"""Does the opposite of expand_user, with its outputs.
"""
if tilde_expand:
return path.replace(tilde_val, '~')
else:
return path
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945
Thomas Kluyver
Improve order of completions
r21917 def completions_sorting_key(word):
"""key for sorting completions
David P. Sanders
Reimplemented penalize_magics_key
r13343
Thomas Kluyver
Improve order of completions
r21917 This does several things:
David P. Sanders
Reimplemented penalize_magics_key
r13343
Thomas Kluyver
Improve order of completions
r21917 - Lowercase all completions, so they are sorted alphabetically with
upper and lower case words mingled
- Demote any completions starting with underscores to the end
- Insert any %magic and %%cellmagic completions in the alphabetical order
by their name
David P. Sanders
Reimplemented penalize_magics_key
r13343 """
Thomas Kluyver
Improve order of completions
r21917 # Case insensitive sort
word = word.lower()
David P. Sanders
Reimplemented penalize_magics_key
r13343
Thomas Kluyver
Improve order of completions
r21917 prio1, prio2 = 0, 0
David P. Sanders
Reimplemented penalize_magics_key
r13343
Thomas Kluyver
Improve order of completions
r21917 if word.startswith('__'):
prio1 = 2
elif word.startswith('_'):
prio1 = 1
David P. Sanders
Reimplemented penalize_magics_key
r13343
Matthias Bussonnier
Promote keywords arguments completion....
r22282 if word.endswith('='):
prio1 = -1
Thomas Kluyver
Improve order of completions
r21917 if word.startswith('%%'):
# If there's another % in there, this is something else, so leave it alone
if not "%" in word[2:]:
word = word[2:]
prio2 = 2
elif word.startswith('%'):
David P. Sanders
Reimplemented penalize_magics_key
r13343 if not "%" in word[1:]:
Thomas Kluyver
Improve order of completions
r21917 word = word[1:]
prio2 = 1
return prio1, word, prio2
David P. Sanders
Reimplemented penalize_magics_key
r13343
Thomas Kluyver
Remove some unhelpful info from IPython.core.completer
r17137 @undoc
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855 class Bunch(object): pass
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945
tmr232
Improved Windows path completion....
r22745 if sys.platform == 'win32':
DELIMS = ' \t\n`!@#$^&*()=+[{]}|;\'",<>?'
else:
DELIMS = ' \t\n`!@#$^&*()=+[{]}\\|;:\'",<>?'
klonuo
add '=' to greedy completer delims...
r9207 GREEDY_DELIMS = ' =\r\n'
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855 class CompletionSplitter(object):
"""An object to split an input line in a manner similar to readline.
By having our own implementation, we can expose readline-like completion in
a uniform manner to all frontends. This object only needs to be given the
line of text to be split and the cursor position on said line, and it
returns the 'word' to be completed on at the cursor after splitting the
entire line.
What characters are used as splitting delimiters can be controlled by
setting the `delims` attribute (this is a property that internally
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945 automatically builds the necessary regular expression)"""
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855
# Private interface
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855 # A string of delimiter characters. The default value makes sense for
# IPython's most typical usage patterns.
MinRK
make Completer.greedy configurable
r4825 _delims = DELIMS
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855
# The expression (a normal string) to be compiled into a regular expression
# for actual splitting. We store it as an attribute mostly for ease of
# debugging, since this type of code can be so tricky to debug.
_delim_expr = None
# The regular expression that does the actual splitting
_delim_re = None
def __init__(self, delims=None):
delims = CompletionSplitter._delims if delims is None else delims
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945 self.delims = delims
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945 @property
def delims(self):
"""Return the string of delimiter characters."""
return self._delims
@delims.setter
def delims(self, delims):
Fernando Perez
Add line splitting for text completion (like readline does internally)....
r2855 """Set the delimiters for line splitting."""
expr = '[' + ''.join('\\'+ c for c in delims) + ']'
self._delim_re = re.compile(expr)
self._delims = delims
self._delim_expr = expr
def split_line(self, line, cursor_pos=None):
"""Split a line of text with a cursor at the given position.
"""
l = line if cursor_pos is None else line[:cursor_pos]
return self._delim_re.split(l)[-1]
MinRK
make Completer.greedy configurable
r4825 class Completer(Configurable):
Bernardo B. Marques
remove all trailling spaces
r4872
Min RK
adopt traitlets 4.2 API...
r22340 greedy = Bool(False,
MinRK
make Completer.greedy configurable
r4825 help="""Activate greedy completion
Kelly Liu
Initial patch with Jedi completion (no function header description)....
r22292 PENDING DEPRECTION. this is now mostly taken care of with Jedi.
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
make Completer.greedy configurable
r4825 This will enable completion on elements of lists, results of function calls, etc.,
but can be unsafe because the code is actually evaluated on TAB.
"""
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
MinRK
move completer configurables to IPCompleter where they belong...
r5231
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
use `parent=self` throughout IPython...
r11064 def __init__(self, namespace=None, global_namespace=None, **kwargs):
ville
initialization (no svn history)
r988 """Create a new completer for the command line.
Kelly Liu
Initial patch with Jedi completion (no function header description)....
r22292 Completer(namespace=ns, global_namespace=ns2) -> completer instance.
ville
initialization (no svn history)
r988
If unspecified, the default namespace where completions are performed
is __main__ (technically, __main__.__dict__). Namespaces should be
given as dictionaries.
An optional second namespace can be given. This allows the completer
to handle cases where both the local and global scopes need to be
distinguished.
Completer instances should be used as the completion mechanism of
readline via the set_completer() call:
readline.set_completer(Completer(my_namespace).complete)
"""
# Don't bind to namespace quite yet, but flag whether the user wants a
# specific namespace or to use __main__.__dict__. This will allow us
# to bind to __main__.__dict__ at completion time, not now.
if namespace is None:
self.use_main_ns = 1
else:
self.use_main_ns = 0
self.namespace = namespace
# The global namespace, if given, can be bound directly
if global_namespace is None:
self.global_namespace = {}
else:
self.global_namespace = global_namespace
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
use `parent=self` throughout IPython...
r11064 super(Completer, self).__init__(**kwargs)
ville
initialization (no svn history)
r988
def complete(self, text, state):
"""Return the next possible completion for 'text'.
This is called successively with state == 0, 1, 2, ... until it
returns None. The completion should begin with 'text'.
"""
if self.use_main_ns:
self.namespace = __main__.__dict__
Bernardo B. Marques
remove all trailling spaces
r4872
ville
initialization (no svn history)
r988 if state == 0:
if "." in text:
self.matches = self.attr_matches(text)
else:
self.matches = self.global_matches(text)
try:
return self.matches[state]
except IndexError:
return None
def global_matches(self, text):
"""Compute matches when text is a simple name.
Return a list of all keywords, built-in functions and names currently
defined in self.namespace or self.global_namespace that match.
"""
matches = []
match_append = matches.append
n = len(text)
for lst in [keyword.kwlist,
Thomas Kluyver
Fix imports of builtins module
r13351 builtin_mod.__dict__.keys(),
ville
initialization (no svn history)
r988 self.namespace.keys(),
self.global_namespace.keys()]:
for word in lst:
if word[:n] == text and word != "__builtins__":
match_append(word)
Matthias Bussonnier
Force completion results to be unicode....
r22243 return [cast_unicode_py2(m) for m in matches]
ville
initialization (no svn history)
r988
def attr_matches(self, text):
"""Compute matches when text contains a dot.
Assuming the text is of the form NAME.NAME....[NAME], and is
evaluatable in self.namespace or self.global_namespace, it will be
evaluated and its attributes (as revealed by dir()) are used as
possible completions. (For class instances, class members are are
also considered.)
WARNING: this can still invoke arbitrary C code, if an object
with a __getattr__ hook is evaluated.
"""
# Another option, seems to work great. Catches things like ''.<tab>
m = re.match(r"(\S+(\.\w+)*)\.(\w*)$", text)
Tim Couper
Added test_dir2 for the dir2 (bonus) tests...
r6309
macgyver
Greedy completer for qtconsole....
r4477 if m:
Bernardo B. Marques
remove all trailling spaces
r4872 expr, attr = m.group(1, 3)
MinRK
make Completer.greedy configurable
r4825 elif self.greedy:
macgyver
Greedy completer for qtconsole....
r4477 m2 = re.match(r"(.+)\.(\w*)$", self.line_buffer)
if not m2:
return []
expr, attr = m2.group(1,2)
MinRK
make Completer.greedy configurable
r4825 else:
return []
Tim Couper
Added test_dir2 for the dir2 (bonus) tests...
r6309
ville
initialization (no svn history)
r988 try:
obj = eval(expr, self.namespace)
except:
try:
obj = eval(expr, self.global_namespace)
except:
return []
Tim Couper
added __all__ to completer.py and added basic tests for test_dir2...
r6308 if self.limit_to__all__ and hasattr(obj, '__all__'):
words = get__all__entries(obj)
else:
words = dir2(obj)
Bernardo B. Marques
remove all trailling spaces
r4872
ville
initialization (no svn history)
r988 try:
words = generics.complete_object(obj, words)
Brian Granger
Continuing a massive refactor of everything.
r2205 except TryNext:
ville
initialization (no svn history)
r988 pass
Thomas Kluyver
Silence errors from custom attribute completer functions....
r5155 except Exception:
# Silence errors from completion function
#raise # dbg
pass
ville
initialization (no svn history)
r988 # Build match list to return
n = len(attr)
Matthias Bussonnier
Force completion results to be unicode....
r22243 return [u"%s.%s" % (expr, w) for w in words if w[:n] == attr ]
ville
initialization (no svn history)
r988
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365
Tim Couper
added __all__ to completer.py and added basic tests for test_dir2...
r6308 def get__all__entries(obj):
"""returns the strings in the __all__ attribute"""
try:
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945 words = getattr(obj, '__all__')
Tim Couper
added __all__ to completer.py and added basic tests for test_dir2...
r6308 except:
return []
Matthias Bussonnier
Force completion results to be unicode....
r22243 return [cast_unicode_py2(w) for w in words if isinstance(w, string_types)]
Tim Couper
added __all__ to completer.py and added basic tests for test_dir2...
r6308
Jeff Hussmann
makes dictionary key completion use same delimiters as splitter
r21294 def match_dict_keys(keys, prefix, delims):
Joel Nothman
Add dict key completion in IPCompleter
r15766 """Used by dict_key_matches, matching the prefix to a list of keys"""
if not prefix:
MinRK
fix some issues in dict key completion...
r16564 return None, 0, [repr(k) for k in keys
Joel Nothman
Add dict key completion in IPCompleter
r15766 if isinstance(k, (string_types, bytes))]
quote_match = re.search('["\']', prefix)
quote = quote_match.group()
try:
prefix_str = eval(prefix + quote, {})
except Exception:
MinRK
fix some issues in dict key completion...
r16564 return None, 0, []
Jeff Hussmann
makes dictionary key completion use same delimiters as splitter
r21294
pattern = '[^' + ''.join('\\' + c for c in delims) + ']*$'
token_match = re.search(pattern, prefix, re.UNICODE)
MinRK
fix some issues in dict key completion...
r16564 token_start = token_match.start()
token_prefix = token_match.group()
Joel Nothman
Add dict key completion in IPCompleter
r15766
# TODO: support bytes in Py3k
matched = []
for key in keys:
try:
if not key.startswith(prefix_str):
continue
Joel Nothman
Comments and minor fixes
r16456 except (AttributeError, TypeError, UnicodeError):
Joel Nothman
Add dict key completion in IPCompleter
r15766 # Python 3+ TypeError on b'a'.startswith('a') or vice-versa
continue
# reformat remainder of key to begin with prefix
rem = key[len(prefix_str):]
Joel Nothman
Comments and minor fixes
r16456 # force repr wrapped in '
Joel Nothman
Add dict key completion in IPCompleter
r15766 rem_repr = repr(rem + '"')
if rem_repr.startswith('u') and prefix[0] not in 'uU':
Joel Nothman
Comments and minor fixes
r16456 # Found key is unicode, but prefix is Py2 string.
# Therefore attempt to interpret key as string.
Joel Nothman
Add dict key completion in IPCompleter
r15766 try:
rem_repr = repr(rem.encode('ascii') + '"')
except UnicodeEncodeError:
continue
rem_repr = rem_repr[1 + rem_repr.index("'"):-2]
if quote == '"':
Joel Nothman
Comments and minor fixes
r16456 # The entered prefix is quoted with ",
# but the match is quoted with '.
# A contained " hence needs escaping for comparison:
Joel Nothman
Add dict key completion in IPCompleter
r15766 rem_repr = rem_repr.replace('"', '\\"')
# then reinsert prefix from start of token
matched.append('%s%s' % (token_prefix, rem_repr))
MinRK
fix some issues in dict key completion...
r16564 return quote, token_start, matched
Joel Nothman
Add dict key completion in IPCompleter
r15766
Joel Nothman
Safe isinstance for unloaded modules
r16468 def _safe_isinstance(obj, module, class_name):
"""Checks if obj is an instance of module.class_name if loaded
"""
return (module in sys.modules and
Diego Garcia
use `import_module` instead of `__import__` ( FIX #10008 )
r22954 isinstance(obj, getattr(import_module(module), class_name)))
Joel Nothman
Safe isinstance for unloaded modules
r16468
Joel Nothman
Add dict key completion in IPCompleter
r15766
Matthias Bussonnier
use function, and tests
r21103 def back_unicode_name_matches(text):
u"""Match unicode characters back to unicode name
This does ☃ -> \\snowman
Note that snowman is not a valid python3 combining character but will be expanded.
Though it will not recombine back to the snowman character by the completion machinery.
Thomas Kluyver
Fix various warnings for broken references etc
r21578 This will not either back-complete standard sequences like \\n, \\b ...
Matthias Bussonnier
use function, and tests
r21103
Used on Python 3 only.
"""
if len(text)<2:
return u'', ()
maybe_slash = text[-2]
if maybe_slash != '\\':
return u'', ()
char = text[-1]
# no expand on quote for completion in strings.
# nor backcomplete standard ascii keys
if char in string.ascii_letters or char in ['"',"'"]:
return u'', ()
try :
unic = unicodedata.name(char)
return '\\'+char,['\\'+unic]
Min RK
adopt traitlets 4.2 API...
r22340 except KeyError:
Matthias Bussonnier
use function, and tests
r21103 pass
return u'', ()
def back_latex_name_matches(text):
u"""Match latex characters back to unicode name
This does ->\\sqrt
Used on Python 3 only.
"""
if len(text)<2:
return u'', ()
maybe_slash = text[-2]
if maybe_slash != '\\':
return u'', ()
char = text[-1]
# no expand on quote for completion in strings.
# nor backcomplete standard ascii keys
if char in string.ascii_letters or char in ['"',"'"]:
return u'', ()
try :
latex = reverse_latex_symbol[char]
# '\\' replace the \ as well
return '\\'+char,[latex]
Min RK
adopt traitlets 4.2 API...
r22340 except KeyError:
Matthias Bussonnier
use function, and tests
r21103 pass
return u'', ()
ville
initialization (no svn history)
r988 class IPCompleter(Completer):
"""Extension of the completer class with IPython-specific features"""
Min RK
adopt traitlets 4.2 API...
r22340
@observe('greedy')
def _greedy_changed(self, change):
MinRK
make Completer.greedy configurable
r4825 """update the splitter and readline delims when greedy is changed"""
Min RK
adopt traitlets 4.2 API...
r22340 if change['new']:
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945 self.splitter.delims = GREEDY_DELIMS
MinRK
make Completer.greedy configurable
r4825 else:
Fernando Perez
Fix bugs in completer.py with incompletely-implemented property....
r6945 self.splitter.delims = DELIMS
MinRK
move completer configurables to IPCompleter where they belong...
r5231
Min RK
adopt traitlets 4.2 API...
r22340 merge_completions = Bool(True,
MinRK
move completer configurables to IPCompleter where they belong...
r5231 help="""Whether to merge completion results into a single list
If False, only the completion results from the first non-empty
completer will be returned.
"""
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
omit__names = Enum((0,1,2), default_value=2,
MinRK
move completer configurables to IPCompleter where they belong...
r5231 help="""Instruct the completer to omit private method names
Specifically, when completing on ``object.<tab>``.
When 2 [default]: all names that start with '_' will be excluded.
When 1: all 'magic' names (``__foo__``) will be excluded.
When 0: nothing will be excluded.
"""
Min RK
adopt traitlets 4.2 API...
r22340 ).tag(config=True)
limit_to__all__ = Bool(False,
Kelly Liu
Initial patch with Jedi completion (no function header description)....
r22292 help="""
DEPRECATED as of version 5.0.
Instruct the completer to use __all__ for the completion
Tim Couper
added __all__ to completer.py and added basic tests for test_dir2...
r6308
Specifically, when completing on ``object.<tab>``.
Tim Couper
Changed type of limit_to__all__ from Enum to CBool
r6311 When True: only those names in obj.__all__ will be included.
Tim Couper
added __all__ to completer.py and added basic tests for test_dir2...
r6308
Tim Couper
Changed type of limit_to__all__ from Enum to CBool
r6311 When False [default]: the __all__ attribute is ignored
Min RK
adopt traitlets 4.2 API...
r22340 """,
).tag(config=True)
Bernardo B. Marques
remove all trailling spaces
r4872
MinRK
make Completer.greedy configurable
r4825 def __init__(self, shell=None, namespace=None, global_namespace=None,
Thomas Kluyver
Remove unused IPython.utils.rlineimpl readline module...
r22934 use_readline=False, config=None, **kwargs):
ville
initialization (no svn history)
r988 """IPCompleter() -> completer
Return a completer object suitable for use by the readline library
via readline.set_completer().
Inputs:
- shell: a pointer to the ipython shell itself. This is needed
Thomas Kluyver
Improvements to docs formatting.
r12553 because this completer knows about magic functions, and those can
only be accessed via the ipython instance.
ville
initialization (no svn history)
r988
- namespace: an optional dict where completions are performed.
- global_namespace: secondary optional dict for completions, to
Thomas Kluyver
Improvements to docs formatting.
r12553 handle cases (such as IPython embedded inside functions) where
both Python scopes are visible.
ville
initialization (no svn history)
r988
Fernando Perez
Multiple improvements to tab completion....
r2839 use_readline : bool, optional
Thomas Kluyver
Remove unused IPython.utils.rlineimpl readline module...
r22934 DEPRECATED, ignored.
"""
ville
initialization (no svn history)
r988
Brian Granger
More work on refactoring things into components....
r2244 self.magic_escape = ESC_MAGIC
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857 self.splitter = CompletionSplitter()
Fernando Perez
Multiple improvements to tab completion....
r2839 if use_readline:
Thomas Kluyver
Tweak wording of warning message
r22937 warnings.warn('The use_readline parameter is deprecated and ignored since IPython 6.0.',
Thomas Kluyver
Remove unused IPython.utils.rlineimpl readline module...
r22934 DeprecationWarning, stacklevel=2)
Fernando Perez
Multiple improvements to tab completion....
r2839
MinRK
make Completer.greedy configurable
r4825 # _greedy_changed() depends on splitter and readline being defined:
Completer.__init__(self, namespace=namespace, global_namespace=global_namespace,
MinRK
move completer configurables to IPCompleter where they belong...
r5231 config=config, **kwargs)
MinRK
make Completer.greedy configurable
r4825
Fernando Perez
Multiple improvements to tab completion....
r2839 # List where completion matches will be stored
self.matches = []
Fernando Perez
First full decoupling of magics into a standalone object.
r6906 self.shell = shell
ville
initialization (no svn history)
r988 # Regexp to split filenames with spaces in them
self.space_name_re = re.compile(r'([^\\] )')
# Hold a local ref. to glob.glob for speed
self.glob = glob.glob
# Determine if we are running on 'dumb' terminals, like (X)Emacs
# buffers, to avoid completion problems.
term = os.environ.get('TERM','xterm')
self.dumb_terminal = term in ['dumb','emacs']
Bernardo B. Marques
remove all trailling spaces
r4872
ville
initialization (no svn history)
r988 # Special handling of backslashes needed in win32 platforms
if sys.platform == "win32":
self.clean_glob = self._clean_glob_win32
else:
self.clean_glob = self._clean_glob
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365
Piti Ongmongkolkul
cache regexp and PEP8
r9169 #regexp to parse docstring for function signature
self.docstring_sig_re = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
self.docstring_kwd_re = re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
#use this if positional argument name is also needed
#= re.compile(r'[\s|\[]*(\w+)(?:\s*=?\s*.*)')
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 # All active matcher routines for completion
Kelly Liu
Initial patch with Jedi completion (no function header description)....
r22292 self.matchers = [
Matthias Bussonnier
Remove Jedi integration before 5.0...
r22352 self.python_matches,
ville
initialization (no svn history)
r988 self.file_matches,
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 self.magic_matches,
Fernando Perez
Multiple improvements to tab completion....
r2839 self.python_func_kw_matches,
Joel Nothman
Add dict key completion in IPCompleter
r15766 self.dict_key_matches,
Fernando Perez
Multiple improvements to tab completion....
r2839 ]
Bernardo B. Marques
remove all trailling spaces
r4872
Thomas Kluyver
Integrate completion into debugger
r22388 # This is set externally by InteractiveShell
self.custom_completers = None
ville
initialization (no svn history)
r988 def all_completions(self, text):
andy wilson
Fix emacs tab-completion with simpler implementation....
r3430 """
Matthias Bussonnier
Remove Pydb integration as Pydb is unmaintained and not ion PyPI...
r22359 Wrapper around the complete method for the benefit of emacs.
andy wilson
Fix emacs tab-completion with simpler implementation....
r3430 """
return self.complete(text)[1]
ville
initialization (no svn history)
r988
Kelly Liu
Initial patch with Jedi completion (no function header description)....
r22292 def _clean_glob(self, text):
ville
initialization (no svn history)
r988 return self.glob("%s*" % text)
def _clean_glob_win32(self,text):
return [f.replace("\\","/")
Bernardo B. Marques
remove all trailling spaces
r4872 for f in self.glob("%s*" % text)]
ville
initialization (no svn history)
r988
def file_matches(self, text):
"""Match filenames, expanding ~USER type strings.
Most of the seemingly convoluted logic in this completer is an
attempt to handle filenames with spaces in them. And yet it's not
quite perfect, because Python's readline doesn't expose all of the
GNU readline details needed for this to be done correctly.
For a filename with a space in it, the printed completions will be
only the parts after what's already been typed (instead of the
full completions, as is normally done). I don't think with the
current (as of Python 2.3) Python readline it's possible to do
better."""
# chars that require escaping with backslash - i.e. chars
# that readline treats incorrectly as delimiters, but we
# don't want to treat as delimiters in filename matching
# when escaped with backslash
if text.startswith('!'):
text = text[1:]
Matthias Bussonnier
Force completion results to be unicode....
r22243 text_prefix = u'!'
ville
initialization (no svn history)
r988 else:
Matthias Bussonnier
Force completion results to be unicode....
r22243 text_prefix = u''
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184
Fernando Perez
Reorganization of readline and completion dependent code....
r2956 text_until_cursor = self.text_until_cursor
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 # track strings with open quotes
open_quotes = has_open_quotes(text_until_cursor)
if '(' in text_until_cursor or '[' in text_until_cursor:
lsplit = text
else:
try:
# arg_split ~ shlex.split, but with unicode bugs fixed by us
lsplit = arg_split(text_until_cursor)[-1]
except ValueError:
# typically an unmatched ", or backslash without escaped char.
if open_quotes:
lsplit = text_until_cursor.split(open_quotes)[-1]
else:
return []
except IndexError:
# tab pressed on empty line
lsplit = ""
ville
initialization (no svn history)
r988
Fernando Perez
Fix file completion of commands with spaces in them.
r2903 if not open_quotes and lsplit != protect_filename(lsplit):
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 # if protectables are found, do matching on the whole escaped name
has_protectables = True
ville
initialization (no svn history)
r988 text0,text = text,lsplit
else:
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 has_protectables = False
ville
initialization (no svn history)
r988 text = os.path.expanduser(text)
if text == "":
Matthias Bussonnier
Fix casting
r22244 return [text_prefix + cast_unicode_py2(protect_filename(f)) for f in self.glob("*")]
ville
initialization (no svn history)
r988
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 # Compute the matches from the filesystem
tmr232
Improved Windows path completion....
r22745 if sys.platform == 'win32':
m0 = self.clean_glob(text)
else:
m0 = self.clean_glob(text.replace('\\', ''))
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184
ville
initialization (no svn history)
r988 if has_protectables:
# If we had protectables, we need to revert our changes to the
# beginning of filename so that we don't double-write the part
# of the filename we have so far
len_lsplit = len(lsplit)
Bernardo B. Marques
remove all trailling spaces
r4872 matches = [text_prefix + text0 +
ville
initialization (no svn history)
r988 protect_filename(f[len_lsplit:]) for f in m0]
else:
if open_quotes:
# if we have a string with an open quote, we don't need to
# protect the names at all (and we _shouldn't_, as it
# would cause bugs when the filesystem call is made).
matches = m0
else:
Bernardo B. Marques
remove all trailling spaces
r4872 matches = [text_prefix +
ville
initialization (no svn history)
r988 protect_filename(f) for f in m0]
Bradley M. Froehle
core.completer: Clean up excessive and unused code....
r5766 # Mark directories in input list by appending '/' to their names.
Matthias Bussonnier
Force completion results to be unicode....
r22243 return [cast_unicode_py2(x+'/') if os.path.isdir(x) else x for x in matches]
ville
initialization (no svn history)
r988
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 def magic_matches(self, text):
"""Match magics"""
# Get all shell magics now rather than statically, so magics loaded at
Fernando Perez
Add completion support for cell magics and handling of line/cell magics....
r6991 # runtime show up too.
lsm = self.shell.magics_manager.lsmagic()
line_magics = lsm['line']
cell_magics = lsm['cell']
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 pre = self.magic_escape
Fernando Perez
Add completion support for cell magics and handling of line/cell magics....
r6991 pre2 = pre+pre
# Completion logic:
# - user gives %%: only do cell magics
# - user gives %: do both line and cell magics
# - no prefix: do both
# In other words, line magics are skipped if the user gives %% explicitly
bare_text = text.lstrip(pre)
comp = [ pre2+m for m in cell_magics if m.startswith(bare_text)]
if not text.startswith(pre2):
comp += [ pre+m for m in line_magics if m.startswith(bare_text)]
Matthias Bussonnier
Force completion results to be unicode....
r22243 return [cast_unicode_py2(c) for c in comp]
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365
Kelly Liu
Initial patch with Jedi completion (no function header description)....
r22292
def python_matches(self, text):
"""Match attributes or global python names"""
ville
initialization (no svn history)
r988 if "." in text:
try:
matches = self.attr_matches(text)
if text.endswith('.') and self.omit__names:
if self.omit__names == 1:
# true if txt is _not_ a __ name, false otherwise:
no__name = (lambda txt:
re.match(r'.*\.__.*?__',txt) is None)
else:
# true if txt is _not_ a _ name, false otherwise:
no__name = (lambda txt:
Paul Ivanov
update regex
r17755 re.match(r'\._.*?',txt[txt.rindex('.'):]) is None)
ville
initialization (no svn history)
r988 matches = filter(no__name, matches)
except NameError:
# catches <undefined attributes>.<tab>
matches = []
else:
matches = self.global_matches(text)
return matches
Piti Ongmongkolkul
_default_arguments_from_docstring docstring style
r9171 def _default_arguments_from_docstring(self, doc):
Piti Ongmongkolkul
use splitlines instead of StringIO.readline and rebase
r9173 """Parse the first line of docstring for call signature.
Piti Ongmongkolkul
_default_arguments_from_docstring docstring style
r9171
Docstring should be of the form 'min(iterable[, key=func])\n'.
Piti Ongmongkolkul
couple typo in docstring
r9172 It can also parse cython docstring of the form
'Minuit.migrad(self, int ncall=10000, resume=True, int nsplit=1)'.
Piti Ongmongkolkul
docstring for _default_arguments_from_docstring
r9166 """
Piti Ongmongkolkul
cache regexp and PEP8
r9169 if doc is None:
return []
Piti Ongmongkolkul
use splitlines instead of StringIO.readline and rebase
r9173
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 #care only the firstline
Piti Ongmongkolkul
use splitlines instead of StringIO.readline and rebase
r9173 line = doc.lstrip().splitlines()[0]
Piti Ongmongkolkul
cache regexp and PEP8
r9169 #p = re.compile(r'^[\w|\s.]+\(([^)]*)\).*')
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 #'min(iterable[, key=func])\n' -> 'iterable[, key=func]'
Piti Ongmongkolkul
cache regexp and PEP8
r9169 sig = self.docstring_sig_re.search(line)
if sig is None:
return []
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 # iterable[, key=func]' -> ['iterable[' ,' key=func]']
sig = sig.groups()[0].split(',')
ret = []
for s in sig:
Piti Ongmongkolkul
cache regexp and PEP8
r9169 #re.compile(r'[\s|\[]*(\w+)(?:\s*=\s*.*)')
ret += self.docstring_kwd_re.findall(s)
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 return ret
ville
initialization (no svn history)
r988 def _default_arguments(self, obj):
"""Return the list of default arguments of obj if it is callable,
or empty list otherwise."""
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 call_obj = obj
ret = []
if inspect.isbuiltin(obj):
Piti Ongmongkolkul
more robust docstring parsing and docstring parser test...
r9167 pass
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 elif not (inspect.isfunction(obj) or inspect.ismethod(obj)):
ville
initialization (no svn history)
r988 if inspect.isclass(obj):
Piti Ongmongkolkul
__call__ should not use class docstring
r9168 #for cython embededsignature=True the constructor docstring
#belongs to the object itself not __init__
ret += self._default_arguments_from_docstring(
Piti Ongmongkolkul
cache regexp and PEP8
r9169 getattr(obj, '__doc__', ''))
Piti Ongmongkolkul
more robust docstring parsing and docstring parser test...
r9167 # for classes, check for __init__,__new__
Piti Ongmongkolkul
cache regexp and PEP8
r9169 call_obj = (getattr(obj, '__init__', None) or
getattr(obj, '__new__', None))
ville
initialization (no svn history)
r988 # for all others, check if they are __call__able
elif hasattr(obj, '__call__'):
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 call_obj = obj.__call__
Piti Ongmongkolkul
more robust docstring parsing and docstring parser test...
r9167 ret += self._default_arguments_from_docstring(
Piti Ongmongkolkul
cache regexp and PEP8
r9169 getattr(call_obj, '__doc__', ''))
Thomas A Caswell
FIX: correct handling of built-ins
r21642
if PY3:
Thomas A Caswell
MNT: use back-ported signature on py2
r21645 _keeps = (inspect.Parameter.KEYWORD_ONLY,
inspect.Parameter.POSITIONAL_OR_KEYWORD)
signature = inspect.signature
Thomas A Caswell
FIX: correct handling of built-ins
r21642 else:
Thomas A Caswell
MNT: use back-ported signature on py2
r21645 import IPython.utils.signatures
_keeps = (IPython.utils.signatures.Parameter.KEYWORD_ONLY,
IPython.utils.signatures.Parameter.POSITIONAL_OR_KEYWORD)
signature = IPython.utils.signatures.signature
try:
sig = signature(call_obj)
ret.extend(k for k, v in sig.parameters.items() if
v.kind in _keeps)
except ValueError:
pass
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165 return list(set(ret))
ville
initialization (no svn history)
r988
def python_func_kw_matches(self,text):
"""Match named parameters (kwargs) of the last open function"""
Thomas A Caswell
FIX: correct handling of built-ins
r21642
ville
initialization (no svn history)
r988 if "." in text: # a parameter cannot be dotted
return []
try: regexp = self.__funcParamsRegex
except AttributeError:
regexp = self.__funcParamsRegex = re.compile(r'''
Jez Ng
Make completer recognize escaped quotes in strings.
r7507 '.*?(?<!\\)' | # single quoted strings or
".*?(?<!\\)" | # double quoted strings or
\w+ | # identifier
\S # other characters
ville
initialization (no svn history)
r988 ''', re.VERBOSE | re.DOTALL)
# 1. find the nearest identifier that comes before an unclosed
piti118
FIX: python_func_kw_matches fails when the cursor is in the middle of complete parentheses
r6510 # parenthesis before the cursor
# e.g. for "foo (1+bar(x), pa<cursor>,a=1)", the candidate is "foo"
tokens = regexp.findall(self.text_until_cursor)
Tamir Bahar
Only suggest a named argument once....
r22926 iterTokens = reversed(tokens); openPar = 0
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165
ville
initialization (no svn history)
r988 for token in iterTokens:
if token == ')':
openPar -= 1
elif token == '(':
openPar += 1
if openPar > 0:
# found the last unclosed parenthesis
break
else:
return []
# 2. Concatenate dotted names ("foo.bar" for "foo.bar(x, pa" )
ids = []
isId = re.compile(r'\w+$').match
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165
ville
initialization (no svn history)
r988 while True:
try:
Bradley M. Froehle
Apply 2to3 `next` fix....
r7847 ids.append(next(iterTokens))
ville
initialization (no svn history)
r988 if not isId(ids[-1]):
ids.pop(); break
Bradley M. Froehle
Apply 2to3 `next` fix....
r7847 if not next(iterTokens) == '.':
ville
initialization (no svn history)
r988 break
except StopIteration:
break
Tamir Bahar
Only suggest a named argument once....
r22926
# Find all named arguments already assigned to, as to avoid suggesting
# them again
usedNamedArgs = set()
par_level = -1
Tamir Bahar
Changed `itertools.izip` to `zip`...
r22929 for token, next_token in zip(tokens, tokens[1:]):
Tamir Bahar
Only suggest a named argument once....
r22926 if token == '(':
par_level += 1
elif token == ')':
par_level -= 1
if par_level != 0:
continue
if next_token != '=':
continue
usedNamedArgs.add(token)
ville
initialization (no svn history)
r988 # lookup the candidate callable matches either using global_matches
# or attr_matches for dotted names
if len(ids) == 1:
callableMatches = self.global_matches(ids[0])
else:
callableMatches = self.attr_matches('.'.join(ids[::-1]))
argMatches = []
for callableMatch in callableMatches:
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 try:
namedArgs = self._default_arguments(eval(callableMatch,
Piti Ongmongkolkul
docstring for _default_arguments_from_docstring
r9166 self.namespace))
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 except:
continue
Piti Ongmongkolkul
complete builtin and cython with embededsignature=True...
r9165
Tamir Bahar
Only suggest a named argument once....
r22926 # Remove used named arguments from the list, no need to show twice
for namedArg in set(namedArgs) - usedNamedArgs:
ville
initialization (no svn history)
r988 if namedArg.startswith(text):
Matthias Bussonnier
Force completion results to be unicode....
r22243 argMatches.append(u"%s=" %namedArg)
ville
initialization (no svn history)
r988 return argMatches
Joel Nothman
Add dict key completion in IPCompleter
r15766 def dict_key_matches(self, text):
Thomas Kluyver
Add docstring for dict_key_matches
r17138 "Match string keys in a dictionary, after e.g. 'foo[' "
Joel Nothman
Add dict key completion in IPCompleter
r15766 def get_keys(obj):
Thomas Kluyver
Allow objects to define their own key completions...
r22145 # Objects can define their own completions by defining an
# _ipy_key_completions_() method.
Thomas Kluyver
Consolidate code to check for method in IPython.utils.dir2
r22148 method = get_real_method(obj, '_ipython_key_completions_')
if method is not None:
return method()
Thomas Kluyver
Allow objects to define their own key completions...
r22145
# Special case some common in-memory dict-like types
Joel Nothman
Safe isinstance for unloaded modules
r16468 if isinstance(obj, dict) or\
_safe_isinstance(obj, 'pandas', 'DataFrame'):
Joel Nothman
Add dict key completion in IPCompleter
r15766 try:
return list(obj.keys())
except Exception:
return []
mbyt
Allow completion on the numpy struct itself
r19790 elif _safe_isinstance(obj, 'numpy', 'ndarray') or\
_safe_isinstance(obj, 'numpy', 'void'):
Joel Nothman
Make dict key completion more conservative...
r16466 return obj.dtype.names or []
return []
Joel Nothman
Add dict key completion in IPCompleter
r15766
try:
regexps = self.__dict_key_regexps
except AttributeError:
dict_key_re_fmt = r'''(?x)
( # match dict-referring expression wrt greedy setting
%s
)
\[ # open bracket
\s* # and optional whitespace
([uUbB]? # string prefix (r not handled)
(?: # unclosed string
'(?:[^']|(?<!\\)\\')*
|
"(?:[^"]|(?<!\\)\\")*
)
)?
$
'''
regexps = self.__dict_key_regexps = {
False: re.compile(dict_key_re_fmt % '''
# identifiers separated by .
(?!\d)\w+
(?:\.(?!\d)\w+)*
'''),
True: re.compile(dict_key_re_fmt % '''
.+
''')
}
match = regexps[self.greedy].search(self.text_until_cursor)
if match is None:
return []
expr, prefix = match.groups()
try:
obj = eval(expr, self.namespace)
except Exception:
try:
obj = eval(expr, self.global_namespace)
except Exception:
return []
keys = get_keys(obj)
if not keys:
return keys
Jeff Hussmann
makes dictionary key completion use same delimiters as splitter
r21294 closing_quote, token_offset, matches = match_dict_keys(keys, prefix, self.splitter.delims)
MinRK
fix some issues in dict key completion...
r16564 if not matches:
return matches
# get the cursor position of
# - the text being completed
# - the start of the key text
# - the start of the completion
text_start = len(self.text_until_cursor) - len(text)
if prefix:
key_start = match.start(2)
completion_start = key_start + token_offset
else:
key_start = completion_start = match.end()
# grab the leading prefix, to make sure all completions start with `text`
if text_start > key_start:
leading = ''
else:
leading = text[text_start:completion_start]
# the index of the `[` character
bracket_idx = match.end(1)
Joel Nothman
Add dict key completion in IPCompleter
r15766
# append closing quote and bracket as appropriate
MinRK
fix some issues in dict key completion...
r16564 # this is *not* appropriate if the opening quote or bracket is outside
# the text given to this method
suf = ''
Joel Nothman
Add dict key completion in IPCompleter
r15766 continuation = self.line_buffer[len(self.text_until_cursor):]
MinRK
fix some issues in dict key completion...
r16564 if key_start > text_start and closing_quote:
# quotes were opened inside text, maybe close them
if continuation.startswith(closing_quote):
continuation = continuation[len(closing_quote):]
else:
suf += closing_quote
if bracket_idx > text_start:
# brackets were opened inside text, maybe close them
if not continuation.startswith(']'):
suf += ']'
return [leading + k + suf for k in matches]
Joel Nothman
Add dict key completion in IPCompleter
r15766
Matthias Bussonnier
worjk latex completion and back
r21101 def unicode_name_matches(self, text):
u"""Match Latex-like syntax for unicode characters base
on the name of the character.
This does \\GREEK SMALL LETTER ETA -> η
Works only on valid python 3 identifier, or on combining characters that
will combine to form a valid identifier.
Used on Python 3 only.
"""
slashpos = text.rfind('\\')
if slashpos > -1:
s = text[slashpos+1:]
try :
unic = unicodedata.lookup(s)
# allow combining chars
if ('a'+unic).isidentifier():
return '\\'+s,[unic]
Min RK
adopt traitlets 4.2 API...
r22340 except KeyError:
Matthias Bussonnier
worjk latex completion and back
r21101 pass
return u'', []
Brian E. Granger
Initial messing around....
r17700 def latex_matches(self, text):
Thomas Kluyver
Add docstring for latex_matches method
r17810 u"""Match Latex syntax for unicode characters.
This does both \\alp -> \\alpha and \\alpha -> α
Used on Python 3 only.
"""
Brian E. Granger
Initial implementation of latex->unicode completer.
r17738 slashpos = text.rfind('\\')
if slashpos > -1:
s = text[slashpos:]
if s in latex_symbols:
Brian E. Granger
Adding partial latex completions.
r17740 # Try to complete a full latex symbol to unicode
# \\alpha -> α
Brian E. Granger
Initial implementation of latex->unicode completer.
r17738 return s, [latex_symbols[s]]
Brian E. Granger
Adding partial latex completions.
r17740 else:
# If a user has partially typed a latex symbol, give them
# a full list of options \al -> [\aleph, \alpha]
matches = [k for k in latex_symbols if k.startswith(s)]
return s, matches
Brian E. Granger
Initial implementation of latex->unicode completer.
r17738 return u'', []
Brian E. Granger
Initial messing around....
r17700
Fernando Perez
Restored major default completer functionality (cd, import, run)....
r2959 def dispatch_custom_completer(self, text):
Thomas Kluyver
Integrate completion into debugger
r22388 if not self.custom_completers:
return
Bernardo B. Marques
remove all trailling spaces
r4872 line = self.line_buffer
ville
initialization (no svn history)
r988 if not line.strip():
return None
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Restored major default completer functionality (cd, import, run)....
r2959 # Create a little structure to pass all the relevant information about
# the current completion to any custom completer.
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 event = Bunch()
ville
initialization (no svn history)
r988 event.line = line
event.symbol = text
cmd = line.split(None,1)[0]
event.command = cmd
Fernando Perez
Restored major default completer functionality (cd, import, run)....
r2959 event.text_until_cursor = self.text_until_cursor
Bernardo B. Marques
remove all trailling spaces
r4872
ville
initialization (no svn history)
r988 # for foo etc, try also to find completer for %foo
if not cmd.startswith(self.magic_escape):
try_magic = self.custom_completers.s_matches(
Bernardo B. Marques
remove all trailling spaces
r4872 self.magic_escape + cmd)
ville
initialization (no svn history)
r988 else:
Bernardo B. Marques
remove all trailling spaces
r4872 try_magic = []
Fernando Perez
Fix tab-completion for magics, other completion cleanup and fixes....
r2365 for c in itertools.chain(self.custom_completers.s_matches(cmd),
Fernando Perez
Reorganization of readline and completion dependent code....
r2956 try_magic,
self.custom_completers.flat_matches(self.text_until_cursor)):
ville
initialization (no svn history)
r988 try:
res = c(event)
Fernando Perez
Fix tab-completion crash when no results were returned.
r3064 if res:
# first, try case sensitive match
Matthias Bussonnier
Force completion results to be unicode....
r22243 withcase = [cast_unicode_py2(r) for r in res if r.startswith(text)]
Fernando Perez
Fix tab-completion crash when no results were returned.
r3064 if withcase:
return withcase
# if none, then case insensitive ones are ok too
text_low = text.lower()
Matthias Bussonnier
Force completion results to be unicode....
r22243 return [cast_unicode_py2(r) for r in res if r.lower().startswith(text_low)]
Brian Granger
Continuing a massive refactor of everything.
r2205 except TryNext:
ville
initialization (no svn history)
r988 pass
Bernardo B. Marques
remove all trailling spaces
r4872
ville
initialization (no svn history)
r988 return None
Bernardo B. Marques
remove all trailling spaces
r4872
Matthias Bussonnier
Do not emit single completions trailing with space....
r22632 @_strip_single_trailing_space
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857 def complete(self, text=None, line_buffer=None, cursor_pos=None):
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 """Find completions for the given text and line context.
ville
initialization (no svn history)
r988
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857 Note that both the text and the line_buffer are optional, but at least
one of them must be given.
Fernando Perez
Multiple improvements to tab completion....
r2839 Parameters
----------
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857 text : string, optional
Text to perform the completion on. If not given, the line buffer
is split using the instance's CompletionSplitter object.
Fernando Perez
Multiple improvements to tab completion....
r2839
line_buffer : string, optional
If not given, the completer attempts to obtain the current line
buffer via readline. This keyword allows clients which are
requesting for text completions in non-readline contexts to inform
the completer of the entire text.
cursor_pos : int, optional
Index of the cursor in the full line buffer. Should be provided by
remote frontends where kernel has no access to frontend state.
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184
Returns
-------
text : str
Text that was actually used in the completion.
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 matches : list
A list of completion matches.
ville
initialization (no svn history)
r988 """
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857 # if the cursor position isn't given, the only sane assumption we can
# make is that it's at the end of the line (the common case)
if cursor_pos is None:
cursor_pos = len(line_buffer) if text is None else len(text)
Chilaka Ramakrishna <Chilaka Ramakrishna>
added namespace condition in IPcompleter
r22855 if self.use_main_ns:
self.namespace = __main__.__dict__
Brian E. Granger
Cleaning up and only doing latex completion for python 3.
r17739 if PY3:
Brian E. Granger
Initial implementation of latex->unicode completer.
r17738
Matthias Bussonnier
worjk latex completion and back
r21101 base_text = text if not line_buffer else line_buffer[:cursor_pos]
latex_text, latex_matches = self.latex_matches(base_text)
if latex_matches:
return latex_text, latex_matches
name_text = ''
name_matches = []
Matthias Bussonnier
use function, and tests
r21103 for meth in (self.unicode_name_matches, back_latex_name_matches, back_unicode_name_matches):
name_text, name_matches = meth(base_text)
if name_text:
return name_text, name_matches
Matthias Bussonnier
worjk latex completion and back
r21101
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857 # if text is either None or an empty string, rely on the line buffer
if not text:
text = self.splitter.split_line(line_buffer, cursor_pos)
# If no line buffer is given, assume the input text is all there was
if line_buffer is None:
line_buffer = text
Bernardo B. Marques
remove all trailling spaces
r4872
Fernando Perez
Reorganization of readline and completion dependent code....
r2956 self.line_buffer = line_buffer
self.text_until_cursor = self.line_buffer[:cursor_pos]
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857
Fernando Perez
Multiple improvements to tab completion....
r2839 # Start with a clean slate of completions
self.matches[:] = []
custom_res = self.dispatch_custom_completer(text)
if custom_res is not None:
# did custom completers produce something?
self.matches = custom_res
ville
initialization (no svn history)
r988 else:
Fernando Perez
Multiple improvements to tab completion....
r2839 # Extend the list of completions with the results of each
# matcher, so we return results to the user from all
# namespaces.
if self.merge_completions:
self.matches = []
for matcher in self.matchers:
Fernando Perez
Prevent completer errors from crashing kernel.
r3073 try:
self.matches.extend(matcher(text))
except:
# Show the ugly traceback if the matcher causes an
# exception, but do NOT crash the kernel!
Fernando Perez
Fix bug in tab completion of filenames when quotes are present....
r3184 sys.excepthook(*sys.exc_info())
Fernando Perez
Multiple improvements to tab completion....
r2839 else:
for matcher in self.matchers:
self.matches = matcher(text)
if self.matches:
break
# FIXME: we should extend our api to return a dict with completions for
# different types of objects. The rlcomplete() method could then
# simply collapse the dict into a list for readline, but we'd have
# richer completion semantics in other evironments.
Thomas Kluyver
Improve order of completions
r21917 self.matches = sorted(set(self.matches), key=completions_sorting_key)
David P. Sanders
Reimplemented penalize_magics_key
r13343
Fernando Perez
Improvements to tab completion in Qt GUI with new api....
r2857 return text, self.matches