xonsh/xonsh/completer.py

47 lines
1.6 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-03-01 22:59:44 -06:00
"""A (tab-)completer for xonsh."""
import builtins
import collections.abc as cabc
2015-03-01 22:59:44 -06:00
class Completer(object):
"""This provides a list of optional completions for the xonsh shell."""
def complete(self, prefix, line, begidx, endidx, ctx=None):
2015-09-30 01:30:03 -04:00
"""Complete the string, given a possible execution context.
2015-03-01 22:59:44 -06:00
Parameters
----------
prefix : str
The string to match
line : str
The line that prefix appears on.
begidx : int
The index in line that prefix starts on.
endidx : int
The index in line that prefix ends on.
ctx : Iterable of str (ie dict, set, etc), optional
Names in the current execution context.
Returns
-------
rtn : list of str
Possible completions of prefix, sorted alphabetically.
lprefix : int
2017-02-04 21:25:40 -05:00
Length of the prefix to be replaced in the completion.
2015-03-01 22:59:44 -06:00
"""
2015-04-03 18:25:44 -04:00
ctx = ctx or {}
2016-06-03 12:57:52 -04:00
for func in builtins.__xonsh_completers__.values():
try:
out = func(prefix, line, begidx, endidx, ctx)
except StopIteration:
return set(), len(prefix)
if isinstance(out, cabc.Sequence):
2016-06-03 12:57:52 -04:00
res, lprefix = out
2015-04-06 08:14:53 -04:00
else:
2016-06-03 12:57:52 -04:00
res = out
2016-05-28 13:57:13 -04:00
lprefix = len(prefix)
2016-06-04 15:44:08 -04:00
if res is not None and len(res) != 0:
def sortkey(s): return s.lstrip(''''"''').lower()
return tuple(sorted(res, key=sortkey)), lprefix
2016-05-31 17:50:05 -04:00
return set(), lprefix