OILS / builtin / completion_ysh.py View on Github | oilshell.org

69 lines, 44 significant
1#!/usr/bin/env python2
2from __future__ import print_function
3
4from _devbuild.gen import arg_types
5from _devbuild.gen.syntax_asdl import loc
6from core import completion
7from core.error import e_usage
8from core import vm
9from data_lang import j8
10from mycpp import mops
11from mycpp import mylib
12from mycpp.mylib import log
13from frontend import flag_util
14from frontend import args
15
16from typing import TYPE_CHECKING
17if TYPE_CHECKING:
18 from _devbuild.gen.runtime_asdl import cmd_value
19
20
21class CompExport(vm._Builtin):
22
23 def __init__(self, root_comp):
24 # type: (completion.RootCompleter) -> None
25 self.root_comp = root_comp
26
27 def Run(self, cmd_val):
28 # type: (cmd_value.Argv) -> int
29 arg_r = args.Reader(cmd_val.argv, cmd_val.arg_locs)
30 arg_r.Next()
31
32 attrs = flag_util.ParseMore('compexport', arg_r)
33 arg = arg_types.compexport(attrs.attrs)
34
35 if arg.c is None:
36 e_usage('expected a -c string, like sh -c', loc.Missing)
37
38 arg_begin = mops.BigTruncate(arg.begin)
39 arg_end = mops.BigTruncate(arg.end)
40
41 begin = 0 if arg_begin == -1 else arg_begin
42 end = len(arg.c) if arg_end == -1 else arg_end
43
44 #log('%r begin %d end %d', arg.c, begin, end)
45
46 # Copied from completion.ReadlineCallback
47 comp = completion.Api(line=arg.c, begin=begin, end=end)
48 it = self.root_comp.Matches(comp)
49
50 #print(comp)
51 #print(self.root_comp)
52
53 comp_matches = list(it)
54 comp_matches.reverse()
55
56 if arg.format == 'jlines':
57 buf = mylib.BufWriter()
58 for m in comp_matches:
59 # Note: everything is quoted, that seems simpler.
60 j8.EncodeString(m, buf)
61 print(buf.getvalue())
62 buf.clear()
63
64 elif arg.format == 'tsv8':
65 log('TSV8 format not implemented')
66 else:
67 raise AssertionError()
68
69 return 0