mirror of
https://github.com/xonsh/xonsh.git
synced 2025-03-04 08:24:40 +01:00
some completion tools
This commit is contained in:
parent
44cd415dc5
commit
3aa94f43fb
4 changed files with 35 additions and 2 deletions
|
@ -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()
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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")
|
||||
|
|
Loading…
Add table
Reference in a new issue