diff --git a/src/autocomplete/zsh.ts b/src/autocomplete/zsh.ts index 038e12e3..aa72c10a 100644 --- a/src/autocomplete/zsh.ts +++ b/src/autocomplete/zsh.ts @@ -205,12 +205,8 @@ _${this.config.bin} const cflags = this.commands.find(c => c.id === id)?.flags if (cflags) { - valuesBlock += ' \\' - this.genZshFlagArguments(cflags) - .split('\n') - .forEach(f => { - valuesBlock += `\n ${f}` - }) + // eslint-disable-next-line no-template-curly-in-string + valuesBlock += ' \\\n "${flags[@]}"' } } @@ -234,7 +230,7 @@ _${this.config.bin} summary: t.description, }) - argsBlock += util.format('"%s")\n %s\n ;;', subArg, `_${this.config.bin}_${underscoreSepId}_${subArg}`) + argsBlock += util.format('\n "%s")\n %s\n ;;', subArg, `_${this.config.bin}_${underscoreSepId}_${subArg}`) }) for (const c of this.commands.filter(c => c.id.startsWith(id + ':') && c.id.split(':').length === depth + 1)) { @@ -246,30 +242,50 @@ _${this.config.bin} summary: c.summary, }) - argsBlock += util.format('"%s")\n %s\n ;;', subArg, this.genZshFlagArgumentsBlock(c.flags)) + const block = this.genZshFlagArgumentsBlock(c.flags) + argsBlock += util.format('\n "%s")\n %s\n ;;', subArg, block) } } - const topicCompFunc = -`_${this.config.bin}_${underscoreSepId}() { + let flags = '' + + if (id) { + const cflags = this.commands.find(c => c.id === id)?.flags + + if (cflags) { + flags += '\n'; + (this.genZshFlagArguments(cflags)).split('\n').forEach(f => { + flags += ` ${f}\n` + }) + flags += ' ' + } + } + + const topicCompFunc = `_${this.config.bin}_${underscoreSepId}() { local context state state_descr line typeset -A opt_args _arguments -C "1: :->cmds" "*::arg:->args" + local -a flags=(%s) + case "$state" in cmds) %s ;; args) - case $line[1] in - %s + case $line[1] in%s + *) + _arguments -S \\ + "\${flags[@]}" + ;; esac ;; esac } ` - return util.format(topicCompFunc, this.genZshValuesBlock({id, subArgs}), argsBlock) + + return util.format(topicCompFunc, flags, this.genZshValuesBlock({id, subArgs}), argsBlock) } private getCoTopics(): string[] { @@ -287,7 +303,8 @@ _${this.config.bin} } private getTopics(): Topic[] { - const topics = this.config.topics.filter((topic: Interfaces.Topic) => { + const topics = this.config.topics + .filter((topic: Interfaces.Topic) => { // it is assumed a topic has a child if it has children const hasChild = this.config.topics.some(subTopic => subTopic.name.includes(`${topic.name}:`)) return hasChild @@ -359,4 +376,3 @@ _${this.config.bin} return cmds } } - diff --git a/test/autocomplete/zsh.test.ts b/test/autocomplete/zsh.test.ts index 3785b8bc..179088a0 100644 --- a/test/autocomplete/zsh.test.ts +++ b/test/autocomplete/zsh.test.ts @@ -186,6 +186,8 @@ _test-cli_app() { _arguments -C "1: :->cmds" "*::arg:->args" + local -a flags=() + case "$state" in cmds) _values "completions" \\ @@ -196,6 +198,10 @@ _test-cli_app() { "execute") _test-cli_app_execute ;; + *) + _arguments -S \\ + "\${flags[@]}" + ;; esac ;; esac @@ -207,6 +213,8 @@ _test-cli_app_execute() { _arguments -C "1: :->cmds" "*::arg:->args" + local -a flags=() + case "$state" in cmds) _values "completions" \\ @@ -219,6 +227,10 @@ _test-cli_app_execute() { --help"[Show help for command]" \\ "*: :_files" ;; + *) + _arguments -S \\ + "\${flags[@]}" + ;; esac ;; esac @@ -230,16 +242,20 @@ _test-cli_deploy() { _arguments -C "1: :->cmds" "*::arg:->args" + local -a flags=( + "*"{-m,--metadata}"[]:file:_files" \\ + "(-a --api-version)"{-a,--api-version}"[]:file:_files" \\ + --json"[Format output as json.]" \\ + "(-i --ignore-errors)"{-i,--ignore-errors}"[Ignore errors.]" \\ + --help"[Show help for command]" \\ + "*: :_files" + ) + case "$state" in cmds) _values "completions" \\ "functions[Deploy a function.]" \\ - "*"{-m,--metadata}"[]:file:_files" \\ - "(-a --api-version)"{-a,--api-version}"[]:file:_files" \\ - --json"[Format output as json.]" \\ - "(-i --ignore-errors)"{-i,--ignore-errors}"[Ignore errors.]" \\ - --help"[Show help for command]" \\ - "*: :_files" + "\${flags[@]}" ;; args) case $line[1] in @@ -249,6 +265,10 @@ _test-cli_deploy() { --help"[Show help for command]" \\ "*: :_files" ;; + *) + _arguments -S \\ + "\${flags[@]}" + ;; esac ;; esac