1
1
mirror of https://github.com/wader/fq.git synced 2024-12-24 22:05:31 +03:00

cli: Only compelete at end or whitespace

This commit is contained in:
Mattias Wadman 2021-08-18 18:22:49 +02:00
parent 03af2b5046
commit 394e2b3837
2 changed files with 33 additions and 27 deletions

View File

@ -109,8 +109,7 @@ func transformToCompletionQuery(q *gojq.Query) (*gojq.Query, CompletionType, str
} }
func completeTrampoline(ctx context.Context, completeFn string, c interface{}, i *Interp, line string, pos int) (newLine []string, shared int, err error) { func completeTrampoline(ctx context.Context, completeFn string, c interface{}, i *Interp, line string, pos int) (newLine []string, shared int, err error) {
lineStr := line[0:pos] vs, err := i.EvalFuncValues(ctx, CompletionMode, c, completeFn, []interface{}{line, pos}, DiscardOutput{Ctx: ctx})
vs, err := i.EvalFuncValues(ctx, CompletionMode, c, completeFn, []interface{}{lineStr}, DiscardOutput{Ctx: ctx})
if err != nil { if err != nil {
return nil, pos, err return nil, pos, err
} }
@ -122,7 +121,7 @@ func completeTrampoline(ctx context.Context, completeFn string, c interface{}, i
return nil, pos, vErr return nil, pos, vErr
} }
// {abc: 123, abd: 123} | complete(".ab") will return {prefix: "ab", names: ["abc", "abd"]} // {abc: 123, abd: 123} | complete(".ab"; 3) will return {prefix: "ab", names: ["abc", "abd"]}
var names []string var names []string
var prefix string var prefix string

View File

@ -38,34 +38,41 @@ def _exit_code_expr_error: 5;
# TODO: completionMode # TODO: completionMode
# TODO: return escaped identifier, not sure current readline implementation supports # TODO: return escaped identifier, not sure current readline implementation supports
# completions that needs to change previous input, ex: .a\t -> ."a \" b" etc # completions that needs to change previous input, ex: .a\t -> ."a \" b" etc
def _complete($e): def _complete($e; $pos):
def _is_internal: startswith("_") or startswith("$_"); def _is_internal: startswith("_") or startswith("$_");
( ( $e | _complete_query) as {$type, $query, $prefix} # only complete if at end of there is a whitespace for now
| { if ($e[$pos] | . == "" or . == " ") then
prefix: $prefix, ( ( $e[0:$pos] | _complete_query) as {$type, $query, $prefix}
names: ( | {
( if $type == "function" or $type == "variable" then prefix: $prefix,
[.[] | eval($query) | scope] | add names: (
elif $type == "index" then ( if $type == "function" or $type == "variable" then
[.[] | eval($query) | keys?, _extkeys?] | add [.[] | eval($query) | scope] | add
else elif $type == "index" then
[] [.[] | eval($query) | keys?, _extkeys?] | add
end else
| ($prefix | _is_internal) as $prefix_is_internal []
| map( end
select( | ($prefix | _is_internal) as $prefix_is_internal
strings and | map(
(_is_ident or $type == "variable") and select(
((_is_internal | not) or $prefix_is_internal or $type == "index") and strings and
startswith($prefix) (_is_ident or $type == "variable") and
((_is_internal | not) or $prefix_is_internal or $type == "index") and
startswith($prefix)
)
) )
| unique
| sort
) )
| unique
| sort
) )
) }
} )
); else
{prefix: "", names: []}
end;
# for convenience when testing
def _complete($e): _complete($e; $e | length);
def _obj_to_csv_kv: def _obj_to_csv_kv:
[to_entries[] | [.key, .value] | join("=")] | join(","); [to_entries[] | [.key, .value] | join("=")] | join(",");