Word completion for IPython.
This module started as fork of the rlcompleter module in the Python standard library. The original enhancements made to rlcompleter have been sent upstream and were accepted as of Python 2.3,
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
delimsattribute (this is a property that internally automatically builds the necessary regular expression)
Initialize self. See help(type(self)) for accurate signature.
Return the string of delimiter characters.
Split a line of text with a cursor at the given position.
Completer(namespace=None, global_namespace=None, **kwargs)¶
__init__(namespace=None, global_namespace=None, **kwargs)¶
Create a new completer for the command line.
Completer(namespace=ns, global_namespace=ns2) -> completer instance.
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:
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.
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’.
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.
IPCompleter(shell=None, namespace=None, global_namespace=None, use_readline=True, config=None, **kwargs)¶
Extension of the completer class with IPython-specific features
__init__(shell=None, namespace=None, global_namespace=None, use_readline=True, config=None, **kwargs)¶
IPCompleter() -> completer
Return a completer object suitable for use by the readline library via readline.set_completer().
- shell: a pointer to the ipython shell itself. This is needed because this completer knows about magic functions, and those can only be accessed via the ipython instance.
- namespace: an optional dict where completions are performed.
- global_namespace: secondary optional dict for completions, to handle cases (such as IPython embedded inside functions) where both Python scopes are visible.
- use_readline : bool, optional
- If true, use the readline library. This completer can still function without readline, though in that case callers must provide some extra information on each call about the current line.
Wrapper around the complete method for the benefit of emacs.
complete(text=None, line_buffer=None, cursor_pos=None)¶
Find completions for the given text and line context.
Note that both the text and the line_buffer are optional, but at least one of them must be given.
- text (string, optional) – Text to perform the completion on. If not given, the line buffer is split using the instance’s CompletionSplitter object.
- 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.
- text (str) – Text that was actually used in the completion.
- matches (list) – A list of completion matches.
Match string keys in a dictionary, after e.g. ‘foo[‘
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.
Match Latex syntax for unicode characters.
This does both alp -> alpha and alpha -> α
Used on Python 3 only.
Match named parameters (kwargs) of the last open function
Match attributes or global python names
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.
Return whether a string has open quotes.
This simply counts whether the number of quote characters of either type in the string is odd.
- If there is an open quote, the quote character is returned. Else, return
Escape a string to protect certain characters.
Expand ‘~’-style usernames in strings.
This is similar to
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. 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.
compress_user(path, tilde_expand, tilde_val)¶
Does the opposite of expand_user, with its outputs.
key for sorting completions
This does several things:
- 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
returns the strings in the __all__ attribute
match_dict_keys(keys, prefix, delims)¶
Used by dict_key_matches, matching the prefix to a list of keys
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.
This will not either back-complete standard sequences like n, b …
Used on Python 3 only.
Match latex characters back to unicode name
This does ->sqrt
Used on Python 3 only.