Skip to content

Commit

Permalink
fix(command): only first argument of rest arguments is completed with…
Browse files Browse the repository at this point in the history
… zsh completions (#359)
  • Loading branch information
c4spar committed Apr 17, 2022
1 parent 1784eef commit dc0c1de
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 33 deletions.
21 changes: 16 additions & 5 deletions command/completions/_zsh_completions_generator.ts
Expand Up @@ -187,11 +187,22 @@ function _${replaceSpecialChars(path)}() {` +
const type = command.getType(arg.type);
if (type && type.handler instanceof FileType) {
const fileCompletions = this.getFileCompletions(type);
args.push(
`${++argIndex}${
arg.optionalValue ? "::" : ":"
}${arg.name}:${fileCompletions}`,
);
if (arg.variadic) {
argIndex++;
for (let i = 0; i < 5; i++) {
args.push(
`${argIndex + i}${
arg.optionalValue ? "::" : ":"
}${arg.name}:${fileCompletions}`,
);
}
} else {
args.push(
`${++argIndex}${
arg.optionalValue ? "::" : ":"
}${arg.name}:${fileCompletions}`,
);
}
} else {
const completionsPath: string = path.split(" ").slice(1).join(" ");
const action = this.addAction(arg, completionsPath);
Expand Down
2 changes: 2 additions & 0 deletions command/test/integration/command.ts
Expand Up @@ -15,6 +15,8 @@ const cmd = new Command()
"Bar option.\nfoo bar baz. foo bar baz.\n\nfoo bar baz.\nfoo bar baz.",
)
.option("-c, --color <val:color>", "Color option.")
.option("-C, --colors <val...:color>", "Color option.")
.arguments("<color:color> [path...:file]")
.default("help")
// foo
.command(
Expand Down
Expand Up @@ -49,8 +49,8 @@ _completions_test() {
}

__completions_test() {
opts=(-h --help -V --version -g --global -m --main -c --color foo help completions)

opts=(-h --help -V --version -g --global -m --main -c --color -C --colors foo help completions)
_completions_test_complete color
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
return 0
fi
Expand All @@ -60,6 +60,7 @@ _completions_test() {
-g|--global) opts=(); _completions_test_complete boolean ;;
-m|--main) opts=(); _completions_test_complete boolean ;;
-c|--color) opts=(); _completions_test_complete color ;;
-C|--colors) opts=(); _completions_test_complete color ;;
esac
}

Expand Down
Expand Up @@ -23,11 +23,13 @@ function __fish_completions_test_using_command
return 1
end

complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a '(completions-test completions complete color )'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s V -l version -x -k -f -d 'Show the version number for this program.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s g -l global -k -f -r -a '(completions-test completions complete boolean )' -d 'Foo option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s m -l main -k -f -r -a '(completions-test completions complete boolean )' -d 'Bar option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s c -l color -k -f -r -a '(completions-test completions complete color )' -d 'Color option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -s C -l colors -k -f -r -a '(completions-test completions complete color )' -d 'Color option.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test' -k -f -a foo -d 'Foo command.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -s h -l help -x -k -f -d 'Show this help.'
complete -c completions-test -n '__fish_completions_test_using_command __completions_test_foo' -s g -l global -k -f -r -a '(completions-test completions complete boolean foo)' -d 'Foo option.'
Expand Down
Expand Up @@ -40,6 +40,7 @@ function _completions_test() {
'completions:Generate shell completions.'
)
_describe 'command' commands
__completions_test_complete color color
}

function _command_args() {
Expand All @@ -56,7 +57,13 @@ function _completions_test() {
'(-h --help -V --version -g --global)'{-g,--global}'[Foo option.]:val:->val-boolean' \
'(-h --help -V --version -m --main)'{-m,--main}'[Bar option.]:val:->val-boolean' \
'(-h --help -V --version -c --color)'{-c,--color}'[Color option.]:val:->val-color' \
'1:command:_commands' \
'(-h --help -V --version -C --colors)'{-C,--colors}'[Color option.]:val:->val-color' \
'1:command:_commands'\
'2::path:_files'\
'3::path:_files'\
'4::path:_files'\
'5::path:_files'\
'6::path:_files' \
'*::sub command:->command_args'

case "$state" in
Expand Down
15 changes: 8 additions & 7 deletions command/test/integration/fixtures/help_command.out
@@ -1,6 +1,6 @@

Usage: completions-test
Version: 1.0.0
Usage: completions-test <color> [path...]
Version: 1.0.0

meta1: value1
meta2: value2
Expand All @@ -12,11 +12,12 @@

Options:

-h, --help - Show this help.
-V, --version - Show the version number for this program.
-g, --global <val> - Foo option.
-m, --main <val> - Bar option.
-c, --color <val> - Color option. (Values: "blue", "yellow", "red")
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-g, --global <val> - Foo option.
-m, --main <val> - Bar option.
-c, --color <val> - Color option. (Values: "blue", "yellow", "red")
-C, --colors <val...> - Color option. (Values: "blue", "yellow", "red")

Commands:

Expand Down
23 changes: 12 additions & 11 deletions command/test/integration/fixtures/help_option_long.out
@@ -1,6 +1,6 @@

Usage: completions-test
Version: 1.0.0
Usage: completions-test <color> [path...]
Version: 1.0.0

meta1: value1
meta2: value2
Expand All @@ -12,15 +12,16 @@

Options:

-h, --help - Show this help.
-V, --version - Show the version number for this program.
-g, --global <val> - Foo option.
-m, --main <val> - Bar option.
foo bar baz. foo bar baz.

foo bar baz.
foo bar baz.
-c, --color <val> - Color option. (Values: "blue", "yellow", "red")
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-g, --global <val> - Foo option.
-m, --main <val> - Bar option.
foo bar baz. foo bar baz.

foo bar baz.
foo bar baz.
-c, --color <val> - Color option. (Values: "blue", "yellow", "red")
-C, --colors <val...> - Color option. (Values: "blue", "yellow", "red")

Commands:

Expand Down
15 changes: 8 additions & 7 deletions command/test/integration/fixtures/help_option_short.out
@@ -1,6 +1,6 @@

Usage: completions-test
Version: 1.0.0
Usage: completions-test <color> [path...]
Version: 1.0.0

meta1: value1
meta2: value2
Expand All @@ -12,11 +12,12 @@

Options:

-h, --help - Show this help.
-V, --version - Show the version number for this program.
-g, --global <val> - Foo option.
-m, --main <val> - Bar option.
-c, --color <val> - Color option. (Values: "blue", "yellow", "red")
-h, --help - Show this help.
-V, --version - Show the version number for this program.
-g, --global <val> - Foo option.
-m, --main <val> - Bar option.
-c, --color <val> - Color option. (Values: "blue", "yellow", "red")
-C, --colors <val...> - Color option. (Values: "blue", "yellow", "red")

Commands:

Expand Down

0 comments on commit dc0c1de

Please sign in to comment.