2015-11-16 14:04:32 -08:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-03-01 22:59:44 -06:00
|
|
|
"""A (tab-)completer for xonsh."""
|
2016-05-28 19:16:41 -04:00
|
|
|
import builtins
|
2016-08-24 01:08:02 -04:00
|
|
|
import collections.abc as cabc
|
2016-05-28 19:16:41 -04:00
|
|
|
|
2015-08-14 23:01:46 +02:00
|
|
|
|
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.
|
2015-12-18 12:06:29 -05:00
|
|
|
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():
|
2016-06-07 23:41:31 -04:00
|
|
|
try:
|
|
|
|
out = func(prefix, line, begidx, endidx, ctx)
|
|
|
|
except StopIteration:
|
|
|
|
return set(), len(prefix)
|
2016-08-24 01:08:02 -04:00
|
|
|
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:
|
2016-06-16 14:26:49 +02:00
|
|
|
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
|