MCPcopy
hub / github.com/spf13/cobra / genZshComp

Function genZshComp

zsh_completions.go:87–308  ·  view source on GitHub ↗
(buf io.StringWriter, name string, includeDesc bool)

Source from the content-addressed store, hash-verified

85}
86
87func genZshComp(buf io.StringWriter, name string, includeDesc bool) {
88 compCmd := ShellCompRequestCmd
89 if !includeDesc {
90 compCmd = ShellCompNoDescRequestCmd
91 }
92 WriteStringAndCheck(buf, fmt.Sprintf(`#compdef %[1]s
93compdef _%[1]s %[1]s
94
95# zsh completion for %-36[1]s -*- shell-script -*-
96
97__%[1]s_debug()
98{
99 local file="$BASH_COMP_DEBUG_FILE"
100 if [[ -n ${file} ]]; then
101 echo "$*" >> "${file}"
102 fi
103}
104
105_%[1]s()
106{
107 local shellCompDirectiveError=%[3]d
108 local shellCompDirectiveNoSpace=%[4]d
109 local shellCompDirectiveNoFileComp=%[5]d
110 local shellCompDirectiveFilterFileExt=%[6]d
111 local shellCompDirectiveFilterDirs=%[7]d
112 local shellCompDirectiveKeepOrder=%[8]d
113
114 local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
115 local -a completions
116
117 __%[1]s_debug "\n========= starting completion logic =========="
118 __%[1]s_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
119
120 # The user could have moved the cursor backwards on the command-line.
121 # We need to trigger completion from the $CURRENT location, so we need
122 # to truncate the command-line ($words) up to the $CURRENT location.
123 # (We cannot use $CURSOR as its value does not work when a command is an alias.)
124 words=("${=words[1,CURRENT]}")
125 __%[1]s_debug "Truncated words[*]: ${words[*]},"
126
127 lastParam=${words[-1]}
128 lastChar=${lastParam[-1]}
129 __%[1]s_debug "lastParam: ${lastParam}, lastChar: ${lastChar}"
130
131 # For zsh, when completing a flag with an = (e.g., %[1]s -n=<TAB>)
132 # completions must be prefixed with the flag
133 setopt local_options BASH_REMATCH
134 if [[ "${lastParam}" =~ '-.*=' ]]; then
135 # We are dealing with a flag with an =
136 flagPrefix="-P ${BASH_REMATCH}"
137 fi
138
139 # Prepare the command to obtain completions
140 requestComp="${words[1]} %[2]s ${words[2,-1]}"
141 if [ "${lastChar}" = "" ]; then
142 # If the last parameter is complete (there is a space following it)
143 # We add an extra empty parameter so we can indicate this to the go completion code.
144 __%[1]s_debug "Adding extra empty parameter"

Callers 1

genZshCompletionMethod · 0.85

Calls 1

WriteStringAndCheckFunction · 0.85

Tested by

no test coverage detected