def _InitDefaultCompletions(ex, comp_lookup): # register builtins and words comp_builtins.Complete(['-E', '-A', 'command'], ex, comp_lookup) # register path completion comp_builtins.Complete(['-D', '-A', 'file'], ex, comp_lookup) # TODO: Move this into demo/slow-completion.sh if 1: # Something for fun, to show off. Also: test that you don't repeatedly hit # the file system / network / coprocess. A1 = completion.WordsAction(['foo.py', 'foo', 'bar.py']) A2 = completion.WordsAction(['m%d' % i for i in xrange(5)], delay=0.1) C1 = completion.ChainedCompleter([A1, A2]) comp_lookup.RegisterName('slowc', C1)
def _InitDefaultCompletions(ex, complete_builtin, comp_state): # register builtins and words complete_builtin(['-E', '-A', 'command']) # register path completion # Add -o filenames? Or should that be automatic? complete_builtin(['-D', '-A', 'file']) # TODO: Move this into demo/slow-completion.sh if 1: # Something for fun, to show off. Also: test that you don't repeatedly hit # the file system / network / coprocess. A1 = completion.WordsAction(['foo.py', 'foo', 'bar.py']) A2 = completion.WordsAction(['m%d' % i for i in xrange(5)], delay=0.1) C1 = completion.UserSpec([A1, A2], [], [], lambda candidate: True) comp_state.RegisterName('slowc', completion.Options([]), C1)
from core import cmd_exec_test from core import completion # module under test from core import legacy from core import lexer from core import state from core import test_lib from core import word_eval from core import ui from core.id_kind import Id from osh import ast_ as ast from osh import parse_lib assign_op_e = ast.assign_op_e A1 = completion.WordsAction(['foo.py', 'foo', 'bar.py']) C1 = completion.ChainedCompleter([A1]) status_lines = [ui.TestStatusLine()] * 10 # A bunch of dummies mem = state.Mem('', [], {}, None) exec_opts = state.ExecOpts(mem) STATUS = completion.StatusOutput(status_lines, exec_opts) V1 = completion.WordsAction(['$var1', '$var2', '$another_var']) EMPTY = completion.WordsAction(['grep', 'sed', 'test']) FIRST = completion.WordsAction(['grep', 'sed', 'test']) class CompletionTest(unittest.TestCase):
def _BuildUserSpec(argv, arg, comp_opts, ex, respect_x=True): """Given flags to complete/compgen, built a UserSpec. Args: respect_x: Stupid special case because bash doesn't respect -X in compgen. """ actions = [] # NOTE: bash doesn't actually check the name until completion time, but # obviously it's better to check here. if arg.F: func_name = arg.F func = ex.funcs.get(func_name) if func is None: raise args.UsageError('Function %r not found' % func_name) actions.append(completion.ShellFuncAction(ex, func)) # NOTE: We need completion for -A action itself!!! bash seems to have it. for name in arg.actions: if name == 'alias': a = _SortedWordsAction(ex.aliases) elif name == 'binding': # TODO: Where do we get this from? a = _SortedWordsAction(['vi-delete']) elif name == 'command': # compgen -A command in bash is SIX things: aliases, builtins, # functions, keywords, external commands relative to the current # directory, and external commands in $PATH. actions.append(_SortedWordsAction(builtin.BUILTIN_NAMES)) actions.append(_SortedWordsAction(ex.aliases)) actions.append(_SortedWordsAction(ex.funcs)) actions.append(_SortedWordsAction(lex.OSH_KEYWORD_NAMES)) actions.append(completion.FileSystemAction(exec_only=True)) # Look on the file system. a = completion.ExternalCommandAction(ex.mem) elif name == 'directory': a = completion.FileSystemAction(dirs_only=True) elif name == 'file': a = completion.FileSystemAction() elif name == 'function': a = _SortedWordsAction(ex.funcs) elif name == 'job': a = _SortedWordsAction(['jobs-not-implemented']) elif name == 'user': a = _UsersAction() elif name == 'variable': a = completion.VariablesAction(ex.mem) elif name == 'helptopic': a = _SortedWordsAction(osh_help.TOPIC_LOOKUP) elif name == 'setopt': a = _SortedWordsAction(state.SET_OPTION_NAMES) elif name == 'shopt': a = _SortedWordsAction(state.SHOPT_OPTION_NAMES) elif name == 'signal': a = _SortedWordsAction(['TODO:signals']) elif name == 'stopped': a = _SortedWordsAction(['jobs-not-implemented']) else: raise NotImplementedError(name) actions.append(a) # e.g. -W comes after -A directory if arg.W: # TODO: Split with IFS. Is that done at registration time or completion # time? actions.append(completion.WordsAction(arg.W.split())) extra_actions = [] if comp_opts.Get('plusdirs'): extra_actions.append(completion.FileSystemAction(dirs_only=True)) # These only happen if there were zero shown. else_actions = [] if comp_opts.Get('default'): else_actions.append(completion.FileSystemAction()) if comp_opts.Get('dirnames'): else_actions.append(completion.FileSystemAction(dirs_only=True)) if not actions and not else_actions: raise args.UsageError('No actions defined in completion: %s' % argv) p = lambda candidate: True # include all if respect_x and arg.X: filter_pat = arg.X if filter_pat.startswith('!'): p = completion.GlobPredicate(False, filter_pat[1:]) else: p = completion.GlobPredicate(True, filter_pat) return completion.UserSpec(actions, extra_actions, else_actions, p, prefix=arg.P or '', suffix=arg.S or '')
def _BuildCompletionChain(argv, arg, ex): """Given flags to complete/compgen, built a ChainedCompleter.""" actions = [] # NOTE: bash doesn't actually check the name until completion time, but # obviously it's better to check here. if arg.F: func_name = arg.F func = ex.funcs.get(func_name) if func is None: raise args.UsageError('Function %r not found' % func_name) actions.append(completion.ShellFuncAction(ex, func)) # NOTE: We need completion for -A action itself!!! bash seems to have it. for name in arg.actions: if name == 'alias': a = _SortedWordsAction(ex.aliases) elif name == 'binding': # TODO: Where do we get this from? a = _SortedWordsAction(['vi-delete']) elif name == 'command': # TODO: This needs keywords too actions.append(_SortedWordsAction(builtin.BUILTIN_NAMES)) a = completion.ExternalCommandAction(ex.mem) elif name == 'directory': a = completion.FileSystemAction(dirs_only=True) elif name == 'file': a = completion.FileSystemAction() elif name == 'function': a = _SortedWordsAction(ex.funcs) elif name == 'job': a = _SortedWordsAction(['jobs-not-implemented']) elif name == 'user': a = _UsersAction() elif name == 'variable': a = completion.VariablesAction(ex.mem) elif name == 'helptopic': a = _SortedWordsAction(osh_help.TOPIC_LOOKUP) elif name == 'setopt': a = _SortedWordsAction(state.SET_OPTION_NAMES) elif name == 'shopt': a = _SortedWordsAction(state.SHOPT_OPTION_NAMES) elif name == 'signal': a = _SortedWordsAction(['TODO:signals']) elif name == 'stopped': a = _SortedWordsAction(['jobs-not-implemented']) else: raise NotImplementedError(name) actions.append(a) # e.g. -W comes after -A directory if arg.W: # TODO: Split with IFS. Is that done at registration time or completion # time? actions.append(completion.WordsAction(arg.W.split())) if not actions: raise args.UsageError('No actions defined in completion: %s' % argv) chain = completion.ChainedCompleter( actions, prefix=arg.P or '', suffix=arg.S or '') return chain
from core import alloc from core import completion # module under test from core import test_lib from core import ui from core import util from core.meta import runtime_asdl, syntax_asdl from frontend import parse_lib from osh import state from testdata.completion import bash_oracle assign_op_e = syntax_asdl.assign_op_e value_e = runtime_asdl.value_e log = util.log A1 = completion.WordsAction(['foo.py', 'foo', 'bar.py']) U1 = completion.UserSpec([A1], [], [], lambda candidate: True) COMP_OPTS = completion.Options([]) mem = state.Mem('', [], {}, None) FIRST = completion.WordsAction(['grep', 'sed', 'test']) U2 = completion.UserSpec([FIRST], [], [], lambda candidate: True) def MockApi(line): """Match readline's get_begidx() / get_endidx().""" end = len(line) i = end - 1 while i > 0: