Skip to content

Commit

Permalink
completion: Better and _internal handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wader committed Sep 12, 2021
1 parent d7dbe7c commit 1436fdc
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ require (

replace github.com/chzyer/readline => github.com/wader/readline v0.0.0-20210708114437-6e459499aaf5

replace github.com/itchyny/gojq => github.com/wader/gojq v0.12.1-0.20210816134134-2dd0226659aa
replace github.com/itchyny/gojq => github.com/wader/gojq v0.12.1-0.20210816164421-ae196e531d8c
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ github.com/nbutton23/zxcvbn-go v0.0.0-20210217022336-fa2cb2858354/go.mod h1:KSVJ
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.1.4/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/wader/gojq v0.12.1-0.20210816134134-2dd0226659aa h1:xpMYSryVu0AewEf49Yons7f6tPyH1z22ztu4sJXQzuk=
github.com/wader/gojq v0.12.1-0.20210816134134-2dd0226659aa/go.mod h1:EQUSKgW/YaOxmXpAwGiowFDO4i2Rmtk5+9dFyeiymAg=
github.com/wader/gojq v0.12.1-0.20210816164421-ae196e531d8c h1:4GlOuPBD+PW0cHJVqPmYtIyofQcbWaDv44QoDsmIgNo=
github.com/wader/gojq v0.12.1-0.20210816164421-ae196e531d8c/go.mod h1:EQUSKgW/YaOxmXpAwGiowFDO4i2Rmtk5+9dFyeiymAg=
github.com/wader/readline v0.0.0-20210708114437-6e459499aaf5 h1:lNbk3zDwMc1TaNGWiKrSxCep6R8qqu9CGWBqSFD/9sE=
github.com/wader/readline v0.0.0-20210708114437-6e459499aaf5/go.mod h1:1n88xxtpWULehIaVFcn5JQJy3RUe/5pvJznoiKWfKng=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand Down
5 changes: 5 additions & 0 deletions pkg/interp/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ func transformToCompletionQuery(q *gojq.Query) (*gojq.Query, CompletionType, str
last.Bind.Body = r
return q, ct, prefix
}
if last.Index != nil && last.Index.Name != "" {
prefix := last.Index.Name
last.Index = nil
return q, CompletionTypeIndex, prefix
}
}

switch q.Term.Type { //nolint:exhaustive
Expand Down
20 changes: 10 additions & 10 deletions pkg/interp/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,20 +208,20 @@ func makeHashFn(fn func() (hash.Hash, error)) func(c interface{}, a []interface{
}

func (i *Interp) readline(c interface{}, a []interface{}) interface{} {
var ok bool
var err error
completeFn := ""
prompt := ""

if len(a) > 0 {
prompt, ok = a[0].(string)
if !ok {
return fmt.Errorf("%v: prompt is not a string", a[1])
prompt, err = toString(a[0])
if err != nil {
return fmt.Errorf("prompt: %w", err)
}
}
if len(a) > 1 {
completeFn, ok = a[1].(string)
if !ok {
return fmt.Errorf("%v: complete function name is not a string", a[0])
completeFn, err = toString(a[1])
if err != nil {
return fmt.Errorf("complete function: %w", err)
}
}

Expand All @@ -245,9 +245,9 @@ func (i *Interp) readline(c interface{}, a []interface{}) interface{} {
}

func (i *Interp) eval(c interface{}, a []interface{}) gojq.Iter {
src, ok := a[0].(string)
if !ok {
return gojq.NewIter(fmt.Errorf("%v: src is not a string", a[0]))
src, err := toString(a[0])
if err != nil {
return gojq.NewIter(fmt.Errorf("src: %w", err))
}

iter, err := i.Eval(i.evalContext.ctx, ScriptMode, c, src, i.evalContext.stdout)
Expand Down
9 changes: 6 additions & 3 deletions pkg/interp/interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ func toString(v interface{}) (string, error) {
switch v := v.(type) {
case string:
return v, nil
case gojq.JQValue:
return toString(v.JQValueToGoJQ())
default:
b, err := toBytes(v)
if err != nil {
Expand Down Expand Up @@ -735,7 +737,7 @@ func (i *Interp) Eval(ctx context.Context, mode RunMode, c interface{}, src stri
func (i *Interp) EvalFunc(ctx context.Context, mode RunMode, c interface{}, name string, args []interface{}, stdout Output) (gojq.Iter, error) {
var argsExpr []string
for i := range args {
argsExpr = append(argsExpr, fmt.Sprintf("$args[%d]", i))
argsExpr = append(argsExpr, fmt.Sprintf("$_args[%d]", i))
}
argExpr := ""
if len(argsExpr) > 0 {
Expand All @@ -746,8 +748,9 @@ func (i *Interp) EvalFunc(ctx context.Context, mode RunMode, c interface{}, name
"input": c,
"args": args,
}
// {input: ..., args: [...]} | .args as $args | .input | name[($args[0]; ...)]
trampolineExpr := fmt.Sprintf(". as {$args} | .input | %s%s", name, argExpr)
/// _args to mark variable as internal and hide it from completion
// {input: ..., args: [...]} | .args as {args: $_args} | .input | name[($_args[0]; ...)]
trampolineExpr := fmt.Sprintf(". as {args: $_args} | .input | %s%s", name, argExpr)
iter, err := i.Eval(ctx, mode, trampolineInput, trampolineExpr, stdout)
if err != nil {
return nil, err
Expand Down
11 changes: 10 additions & 1 deletion pkg/interp/interp.jq
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def _exit_code_expr_error: 5;
# TODO: return escaped identifier, not sure current readline implementation supports
# completions that needs to change previous input, ex: .a\t -> ."a \" b" etc
def _complete($e):
def _is_internal: startswith("_") or startswith("$_");
( ( $e | _complete_query) as {$type, $query, $prefix}
| {
prefix: $prefix,
Expand All @@ -50,7 +51,15 @@ def _complete($e):
else
[]
end
| map(select(strings and _is_ident and startswith($prefix)))
| ($prefix | _is_internal) as $prefix_is_internal
| map(
select(
strings and
(_is_ident or $type == "variable") and
((_is_internal | not) or $prefix_is_internal or $type == "index") and
startswith($prefix)
)
)
| unique
| sort
)
Expand Down
10 changes: 10 additions & 0 deletions pkg/interp/testdata/completion.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ hevc_dcr
hevc_nalu
hex
hexdump
null> _is_ide\t
_is_ident
null> {aa: 123} | var("test")
null> $\t
$ENV
$test
null> $test.a\t
aa
null> {bb: 123} as $aa | $aa.b\t
bb
null> {aa: 123, ab: "a"} | .a\t
aa
ab
Expand Down

0 comments on commit 1436fdc

Please sign in to comment.