Skip to content

Commit

Permalink
fix(zsh): Use _default as zsh completion fallback
Browse files Browse the repository at this point in the history
That is for a ValueHint of Unknown. This is consistent with bash where
compgen -f is used in such cases. In long experience with completions
distributed with zsh, the worst thing you can do is break filename
completion as that's the minimum most user's expect.
  • Loading branch information
Oliver Kiddle committed Oct 28, 2024
1 parent 55a18f5 commit e40168c
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 33 deletions.
6 changes: 2 additions & 4 deletions clap_complete/src/aot/shells/zsh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,8 @@ fn value_completion(arg: &Arg) -> Option<String> {
// NB! If you change this, please also update the table in `ValueHint` documentation.
Some(
match arg.get_value_hint() {
ValueHint::Unknown => {
return None;
}
ValueHint::Other => "( )",
ValueHint::Unknown => "_default",
ValueHint::Other => "",
ValueHint::AnyPath => "_files",
ValueHint::FilePath => "_files",
ValueHint::DirPath => "_files -/",
Expand Down
10 changes: 5 additions & 5 deletions clap_complete/tests/snapshots/aliases.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ _my-app() {

local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" : \
'-o+[cmd option]: : ' \
'-O+[cmd option]: : ' \
'--option=[cmd option]: : ' \
'--opt=[cmd option]: : ' \
'-o+[cmd option]: :_default' \
'-O+[cmd option]: :_default' \
'--option=[cmd option]: :_default' \
'--opt=[cmd option]: :_default' \
'-f[cmd flag]' \
'-F[cmd flag]' \
'--flag[cmd flag]' \
Expand All @@ -27,7 +27,7 @@ _my-app() {
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::positional:' \
'::positional:_default' \
&& ret=0
}

Expand Down
2 changes: 1 addition & 1 deletion clap_complete/tests/snapshots/feature_sample.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _my-app() {
case $line[3] in
(test)
_arguments "${_arguments_options[@]}" : \
'--case=[the case to test]: : ' \
'--case=[the case to test]: :_default' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ _exhaustive() {
case $line[1] in
(action)
_arguments "${_arguments_options[@]}" : \
'--set=[value]: : ' \
'--set=[value]: :_default' \
'--choice=[enum]: :(first second)' \
'--set-true[bool]' \
'*--count[number]' \
Expand Down Expand Up @@ -187,15 +187,15 @@ esac
;;
(value)
_arguments "${_arguments_options[@]}" : \
'--delim=[]: : ' \
'--tuple=[]: : : : ' \
'--require-eq=[]: : ' \
'--delim=[]: :_default' \
'--tuple=[]: :_default: :_default' \
'--require-eq=[]: :_default' \
'--global[everywhere]' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'*;::term:' \
'*;::term:_default' \
&& ret=0
;;
(pacman)
Expand Down Expand Up @@ -272,16 +272,16 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::first:' \
'::free:' \
'::first:_default' \
'::free:_default' \
&& ret=0
;;
(alias)
_arguments "${_arguments_options[@]}" : \
'-o+[cmd option]: : ' \
'-O+[cmd option]: : ' \
'--option=[cmd option]: : ' \
'--opt=[cmd option]: : ' \
'-o+[cmd option]: :_default' \
'-O+[cmd option]: :_default' \
'--option=[cmd option]: :_default' \
'--opt=[cmd option]: :_default' \
'-f[cmd flag]' \
'-F[cmd flag]' \
'--flag[cmd flag]' \
Expand All @@ -291,14 +291,14 @@ _arguments "${_arguments_options[@]}" : \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'::positional:' \
'::positional:_default' \
&& ret=0
;;
(hint)
_arguments "${_arguments_options[@]}" : \
'--choice=[]: :(bash fish zsh)' \
'--unknown=[]: : ' \
'--other=[]: :( )' \
'--unknown=[]: :_default' \
'--other=[]: :' \
'-p+[]: :_files' \
'--path=[]: :_files' \
'-f+[]: :_files' \
Expand Down
6 changes: 3 additions & 3 deletions clap_complete/tests/snapshots/special_commands.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _my-app() {
case $line[3] in
(test)
_arguments "${_arguments_options[@]}" : \
'--case=[the case to test]: : ' \
'--case=[the case to test]: :_default' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
Expand All @@ -45,12 +45,12 @@ _arguments "${_arguments_options[@]}" : \
;;
(some_cmd)
_arguments "${_arguments_options[@]}" : \
'--config=[the other case to test]: : ' \
'--config=[the other case to test]: :_default' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
'--version[Print version]' \
'*::path:' \
'*::path:_default' \
&& ret=0
;;
(some-cmd-with-hyphens)
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/tests/snapshots/sub_subcommands.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ _my-app() {
case $line[3] in
(test)
_arguments "${_arguments_options[@]}" : \
'--case=[the case to test]: : ' \
'--case=[the case to test]: :_default' \
'-h[Print help]' \
'--help[Print help]' \
'-V[Print version]' \
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/tests/snapshots/subcommand_last.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _my-app() {
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
'::free:' \
'::free:_default' \
":: :_my-app_commands" \
"*::: :->my-app" \
&& ret=0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _my-app() {
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
'*::first -- first multi-valued argument:' \
'*::first -- first multi-valued argument:_default' \
&& ret=0
}

Expand Down
4 changes: 2 additions & 2 deletions clap_complete/tests/snapshots/value_hint.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ _my-app() {
local context curcontext="$curcontext" state line
_arguments "${_arguments_options[@]}" : \
'--choice=[]: :(bash fish zsh)' \
'--unknown=[]: : ' \
'--other=[]: :( )' \
'--unknown=[]: :_default' \
'--other=[]: :' \
'-p+[]: :_files' \
'--path=[]: :_files' \
'-f+[]: :_files' \
Expand Down
2 changes: 1 addition & 1 deletion clap_complete/tests/snapshots/value_terminator.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ _my-app() {
_arguments "${_arguments_options[@]}" : \
'-h[Print help]' \
'--help[Print help]' \
'*;::arguments -- multi-valued argument with a value terminator:' \
'*;::arguments -- multi-valued argument with a value terminator:_default' \
&& ret=0
}

Expand Down

0 comments on commit e40168c

Please sign in to comment.