Skip to content

Commit

Permalink
feat(command): support zsh fpath completion (#703)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewthauer authored May 16, 2024
1 parent 4e62e39 commit f770de2
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 138 deletions.
19 changes: 13 additions & 6 deletions command/completions/_zsh_completions_generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ export class ZshCompletionsGenerator {
? ` v${this.cmd.getVersion()}`
: "";

return `#!/usr/bin/env zsh
return `#compdef ${this.name}
# zsh completion support for ${path}${version}
autoload -U is-at-least
Expand Down Expand Up @@ -65,9 +66,12 @@ function __${replaceSpecialChars(this.name)}_complete {
${this.generateCompletions(this.name, this.cmd).trim()}
# _${replaceSpecialChars(path)} "\${@}"
compdef _${replaceSpecialChars(path)} ${path}`;
# shellcheck disable=SC2154
if [ "\${funcstack[1]}" = "_${this.name}" ]; then
_${replaceSpecialChars(this.name)} "\${@}"
else
compdef _${replaceSpecialChars(path)} ${path};
fi`;
}

/** Generates zsh completions method for given command and child commands. */
Expand All @@ -86,8 +90,11 @@ compdef _${replaceSpecialChars(path)} ${path}`;
path = (path ? path + " " : "") + name;

return `# shellcheck disable=SC2154
(( $+functions[_${replaceSpecialChars(path)}] )) ||
function _${replaceSpecialChars(path)}() {` +
` +
(command.getParent()
? `(( $+functions[_${replaceSpecialChars(path)}] )) || `
: "") +
`_${replaceSpecialChars(path)}() {` +
(!command.getParent()
? `
local state`
Expand Down
78 changes: 34 additions & 44 deletions command/test/command/__snapshots__/completion_test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ export const snapshot = {};

snapshot[`should handle file type completions in options > zsh 1`] = `
stdout:
\`#!/usr/bin/env zsh
\`#compdef completions-test
# zsh completion support for completions-test v1.0.0
autoload -U is-at-least
Expand Down Expand Up @@ -31,8 +32,7 @@ function __completions_test_complete {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test] )) ||
function _completions_test() {
_completions_test() {
local state
function _commands() {
Expand Down Expand Up @@ -70,17 +70,15 @@ function _completions_test() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_foo] )) ||
function _completions_test_foo() {
(( \$+functions[_completions_test_foo] )) || _completions_test_foo() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
'(-h --help -f --file1)'{-f,--file1}'[...]:path:_files'
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_bar] )) ||
function _completions_test_bar() {
(( \$+functions[_completions_test_bar] )) || _completions_test_bar() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -92,17 +90,15 @@ function _completions_test_bar() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_beep] )) ||
function _completions_test_beep() {
(( \$+functions[_completions_test_beep] )) || _completions_test_beep() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
'(-h --help -f --file3)'{-f,--file3}'[...]:path:_files'
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_boop] )) ||
function _completions_test_boop() {
(( \$+functions[_completions_test_boop] )) || _completions_test_boop() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -114,8 +110,7 @@ function _completions_test_boop() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions] )) ||
function _completions_test_completions() {
(( \$+functions[_completions_test_completions] )) || _completions_test_completions() {
function _commands() {
local -a commands
Expand Down Expand Up @@ -147,8 +142,7 @@ function _completions_test_completions() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions_bash] )) ||
function _completions_test_completions_bash() {
(( \$+functions[_completions_test_completions_bash] )) || _completions_test_completions_bash() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -160,8 +154,7 @@ function _completions_test_completions_bash() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions_fish] )) ||
function _completions_test_completions_fish() {
(( \$+functions[_completions_test_completions_fish] )) || _completions_test_completions_fish() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -173,8 +166,7 @@ function _completions_test_completions_fish() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions_zsh] )) ||
function _completions_test_completions_zsh() {
(( \$+functions[_completions_test_completions_zsh] )) || _completions_test_completions_zsh() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -185,17 +177,21 @@ function _completions_test_completions_zsh() {
esac
}
# _completions_test "\${@}"
compdef _completions_test completions-test
# shellcheck disable=SC2154
if [ "\${funcstack[1]}" = "_completions-test" ]; then
_completions_test "\${@}"
else
compdef _completions_test completions-test;
fi
\`
stderr:
""
`;

snapshot[`should generate file type completions in arguments > zsh 1`] = `
stdout:
\`#!/usr/bin/env zsh
\`#compdef completions-test
# zsh completion support for completions-test v1.0.0
autoload -U is-at-least
Expand Down Expand Up @@ -224,8 +220,7 @@ function __completions_test_complete {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test] )) ||
function _completions_test() {
_completions_test() {
local state
function _commands() {
Expand Down Expand Up @@ -263,8 +258,7 @@ function _completions_test() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_foo] )) ||
function _completions_test_foo() {
(( \$+functions[_completions_test_foo] )) || _completions_test_foo() {
function _commands() {
_files
Expand All @@ -276,8 +270,7 @@ function _completions_test_foo() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_bar] )) ||
function _completions_test_bar() {
(( \$+functions[_completions_test_bar] )) || _completions_test_bar() {
function _commands() {
}
Expand All @@ -289,8 +282,7 @@ function _completions_test_bar() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_beep] )) ||
function _completions_test_beep() {
(( \$+functions[_completions_test_beep] )) || _completions_test_beep() {
function _commands() {
_files
Expand All @@ -302,8 +294,7 @@ function _completions_test_beep() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_boop] )) ||
function _completions_test_boop() {
(( \$+functions[_completions_test_boop] )) || _completions_test_boop() {
function _commands() {
}
Expand All @@ -319,8 +310,7 @@ function _completions_test_boop() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions] )) ||
function _completions_test_completions() {
(( \$+functions[_completions_test_completions] )) || _completions_test_completions() {
function _commands() {
local -a commands
Expand Down Expand Up @@ -352,8 +342,7 @@ function _completions_test_completions() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions_bash] )) ||
function _completions_test_completions_bash() {
(( \$+functions[_completions_test_completions_bash] )) || _completions_test_completions_bash() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -365,8 +354,7 @@ function _completions_test_completions_bash() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions_fish] )) ||
function _completions_test_completions_fish() {
(( \$+functions[_completions_test_completions_fish] )) || _completions_test_completions_fish() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -378,8 +366,7 @@ function _completions_test_completions_fish() {
}
# shellcheck disable=SC2154
(( \$+functions[_completions_test_completions_zsh] )) ||
function _completions_test_completions_zsh() {
(( \$+functions[_completions_test_completions_zsh] )) || _completions_test_completions_zsh() {
_arguments -w -s -S -C \\\\
'(- *)'{-h,--help}'[Show this help.]' \\\\
Expand All @@ -390,9 +377,12 @@ function _completions_test_completions_zsh() {
esac
}
# _completions_test "\${@}"
compdef _completions_test completions-test
# shellcheck disable=SC2154
if [ "\${funcstack[1]}" = "_completions-test" ]; then
_completions_test "\${@}"
else
compdef _completions_test completions-test;
fi
\`
stderr:
""
Expand Down
Loading

0 comments on commit f770de2

Please sign in to comment.