builtin commandline: fix "-x" spuriously including redirection targets

completions frequently use

	argparse ... -- (commandline -xpc)

The "commandline -xpc" output
contains only string tokens.

A syntactically-valid process ("-p") consistes of only string tokens
and redirection tokens.  We skip all non-string tokens, but we do include
redirection targets, which are always strings.  This is weird, and confuses
completion scripts such as the one above.  Leave out redirection targets too.

Part of #11084
This commit is contained in:
Johannes Altmanninger
2025-05-05 13:31:23 +02:00
parent 58af4fa34c
commit 83f74f9332
3 changed files with 27 additions and 1 deletions

View File

@@ -218,10 +218,16 @@ fn write_part(
add_token(buff);
} else {
let mut tok = Tokenizer::new(buff, TOK_ACCEPT_UNFINISHED);
let mut in_redirection = false;
while let Some(token) = tok.next() {
if cut_at_cursor && token.end() >= pos {
break;
}
let is_redirection_target = in_redirection;
in_redirection = token.type_ == TokenType::redirect;
if is_redirection_target && token.type_ == TokenType::string {
continue;
}
if token.type_ != TokenType::string {
continue;
}

View File

@@ -32,3 +32,21 @@ commandline --insert-smart '$ echo 123' --current-token
# CHECKERR: commandline --insert-smart '$ echo 123' --current-token
# CHECKERR: ^
# CHECKERR: (Type 'help commandline' for related documentation)
commandline --input "echo {arg1,arg2} <in >out" --tokens-expanded
# CHECK: echo
# CHECK: arg1
# CHECK: arg2
commandline --input "echo <" --tokens-expanded
# CHECK: echo
commandline --input "echo >" --tokens-expanded
# CHECK: echo
commandline --input "echo > > arg" --tokens-expanded
# CHECK: echo
commandline --input "echo > {a,b}" --tokens-expanded
# CHECK: echo
commandline --input "echo {arg1,arg2} <in >out" --tokens-raw
# CHECK: echo
# CHECK: {arg1,arg2}

View File

@@ -470,12 +470,14 @@ end
complete -C 'crookshanks '
# CHECK: +pet
# Custom completion works with variable overrides.
# Custom completion works with variable overrides and redirections.
complete cmd_with_fancy_completion -xa '(commandline -xpc | count)'
complete -C"a=1 b=2 cmd_with_fancy_completion "
# CHECK: 1
complete -C"a=1 b=2 cmd_with_fancy_completion 1 "
# CHECK: 2
complete -C"cmd_with_fancy_completion </dev/null >/dev/null 2>>/dev/null >?/dev/null &>/dev/null "
# CHECK: 1
complete -c thing -x -F
# CHECKERR: complete: invalid option combination, '--exclusive' and '--force-files'