Skip to content

Commit

Permalink
chore: update
Browse files Browse the repository at this point in the history
  • Loading branch information
jayree committed Feb 24, 2023
1 parent 85a9aed commit ec14c7e
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 21 deletions.
46 changes: 31 additions & 15 deletions src/autocomplete/zsh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[@]}"'
}
}

Expand All @@ -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)) {
Expand All @@ -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[] {
Expand All @@ -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
Expand Down Expand Up @@ -359,4 +376,3 @@ _${this.config.bin}
return cmds
}
}

32 changes: 26 additions & 6 deletions test/autocomplete/zsh.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ _test-cli_app() {
_arguments -C "1: :->cmds" "*::arg:->args"
local -a flags=()
case "$state" in
cmds)
_values "completions" \\
Expand All @@ -196,6 +198,10 @@ _test-cli_app() {
"execute")
_test-cli_app_execute
;;
*)
_arguments -S \\
"\${flags[@]}"
;;
esac
;;
esac
Expand All @@ -207,6 +213,8 @@ _test-cli_app_execute() {
_arguments -C "1: :->cmds" "*::arg:->args"
local -a flags=()
case "$state" in
cmds)
_values "completions" \\
Expand All @@ -219,6 +227,10 @@ _test-cli_app_execute() {
--help"[Show help for command]" \\
"*: :_files"
;;
*)
_arguments -S \\
"\${flags[@]}"
;;
esac
;;
esac
Expand All @@ -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
Expand All @@ -249,6 +265,10 @@ _test-cli_deploy() {
--help"[Show help for command]" \\
"*: :_files"
;;
*)
_arguments -S \\
"\${flags[@]}"
;;
esac
;;
esac
Expand Down

0 comments on commit ec14c7e

Please sign in to comment.