mirror of
https://github.com/wader/fq.git
synced 2024-12-24 13:52:02 +03:00
cli: Only compelete at end or whitespace
This commit is contained in:
parent
03af2b5046
commit
394e2b3837
@ -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) {
|
||||
lineStr := line[0:pos]
|
||||
vs, err := i.EvalFuncValues(ctx, CompletionMode, c, completeFn, []interface{}{lineStr}, DiscardOutput{Ctx: ctx})
|
||||
vs, err := i.EvalFuncValues(ctx, CompletionMode, c, completeFn, []interface{}{line, pos}, DiscardOutput{Ctx: ctx})
|
||||
if err != nil {
|
||||
return nil, pos, err
|
||||
}
|
||||
@ -122,7 +121,7 @@ func completeTrampoline(ctx context.Context, completeFn string, c interface{}, i
|
||||
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 prefix string
|
||||
|
@ -38,34 +38,41 @@ def _exit_code_expr_error: 5;
|
||||
# TODO: completionMode
|
||||
# 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 _complete($e; $pos):
|
||||
def _is_internal: startswith("_") or startswith("$_");
|
||||
( ( $e | _complete_query) as {$type, $query, $prefix}
|
||||
| {
|
||||
prefix: $prefix,
|
||||
names: (
|
||||
( if $type == "function" or $type == "variable" then
|
||||
[.[] | eval($query) | scope] | add
|
||||
elif $type == "index" then
|
||||
[.[] | eval($query) | keys?, _extkeys?] | add
|
||||
else
|
||||
[]
|
||||
end
|
||||
| ($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)
|
||||
# only complete if at end of there is a whitespace for now
|
||||
if ($e[$pos] | . == "" or . == " ") then
|
||||
( ( $e[0:$pos] | _complete_query) as {$type, $query, $prefix}
|
||||
| {
|
||||
prefix: $prefix,
|
||||
names: (
|
||||
( if $type == "function" or $type == "variable" then
|
||||
[.[] | eval($query) | scope] | add
|
||||
elif $type == "index" then
|
||||
[.[] | eval($query) | keys?, _extkeys?] | add
|
||||
else
|
||||
[]
|
||||
end
|
||||
| ($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
|
||||
)
|
||||
| unique
|
||||
| sort
|
||||
)
|
||||
)
|
||||
}
|
||||
);
|
||||
}
|
||||
)
|
||||
else
|
||||
{prefix: "", names: []}
|
||||
end;
|
||||
# for convenience when testing
|
||||
def _complete($e): _complete($e; $e | length);
|
||||
|
||||
def _obj_to_csv_kv:
|
||||
[to_entries[] | [.key, .value] | join("=")] | join(",");
|
||||
|
Loading…
Reference in New Issue
Block a user