some completion tools

This commit is contained in:
Anthony Scopatz 2015-04-05 15:42:24 -05:00
parent 44cd415dc5
commit 3aa94f43fb
4 changed files with 35 additions and 2 deletions

View file

@ -97,5 +97,6 @@ def test_ensure_list_of_strs():
obs = ensure_list_of_strs(inp)
yield assert_equal, exp, obs
if __name__ == '__main__':
nose.runmodule()

View file

@ -5,7 +5,7 @@ import nose
from nose.tools import assert_equal
from xonsh.lexer import Lexer
from xonsh.tools import subproc_toks
from xonsh.tools import subproc_toks, subexpr_from_unbalanced
LEXER = Lexer()
LEXER.build()
@ -136,6 +136,16 @@ def test_subproc_toks_comment():
obs = subproc_toks('# I am a comment', lexer=LEXER, returnline=True)
assert_equal(exp, obs)
def test_subexpr_from_unbalanced_parens():
cases = [
('f(x.', 'x.'),
('f(1,x.', 'x.'),
('f((1,10),x.y', 'x.y'),
]
for expr, exp in cases:
obs = subexpr_from_unbalanced(expr, '(', ')')
yield assert_equal, exp, obs
if __name__ == '__main__':
nose.runmodule()

View file

@ -67,6 +67,7 @@ class Completer(object):
"""
space = ' ' # intern some strings for faster appending
slash = '/'
dot = '.'
ctx = ctx or {}
cmd = line.split(' ', 1)[0]
if begidx == 0:
@ -90,7 +91,10 @@ class Completer(object):
rtn = set()
rtn |= {s for s in XONSH_TOKENS if s.startswith(prefix)}
if ctx is not None:
rtn |= {s for s in ctx if s.startswith(prefix)}
if dot in prefix:
rtn |= self.attr_complete(prefix, ctx)
else:
rtn |= {s for s in ctx if s.startswith(prefix)}
rtn |= {s for s in dir(builtins) if s.startswith(prefix)}
rtn |= {s + space for s in builtins.aliases if s.startswith(prefix)}
rtn |= self.path_complete(prefix)

View file

@ -89,6 +89,24 @@ def subproc_toks(line, mincol=-1, maxcol=None, lexer=None, returnline=False):
return rtn
def subexpr_from_unbalanced(expr, ltok, rtok):
"""Attmpts to pull out a valid subexpression for unbalanced grouping,
based on opening tokens, eg. '(', and closing tokens, eg. ')'. This
does not do full tokenization, but should be good enough for tab
completeion.
"""
lcnt = expr.count(ltok)
if lcnt == 0:
return expr
rcnt = expr.count(rtok)
if lcnt == rcnt:
return expr
subexpr = expr.rsplit(ltok, 1)[-1]
subexpr = subexpr.rsplit(',', 1)[-1]
subexpr = subexpr.rsplit(':', 1)[-1]
return subexpr
def decode(s, encoding=None):
encoding = encoding or DEFAULT_ENCODING
return s.decode(encoding, "replace")