Skip to content
Myr

Fix zsh for DirectiveNoSpace and DirectiveNoFileComp (#1213)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fixes #1211 When handling ShellCompDirectiveNoSpace we must still properly handle descriptions. To do so we cannot simply use 'compadd', but must use zsh's '_describe' function. Also, when handling ShellCompDirectiveNoSpace we cannot assume that only a single completion will be given to the script. In fact, ValidArgsFunction can return multiple completions, even if they don't match the 'toComplete' argument prefix. Therefore, we cannot use the number of completions received in the completion script to determine if we should activate the "no space" directive. Instead, we can leave it all to the '_describe' function. Fixes #1212 When handling ShellCompDirectiveNoFileComp we cannot base ourself on the script receiving no valid completion. In fact, ValidArgsFunction can return multiple completions, even if they don't match the 'toComplete' argument prefix at all. Therefore, we cannot use the number of completions received by the completion script to determine if we should activate the "no file comp" directive. Instead, we can check if the '_describe' function has found any completions. Finally, it is important for the script to return the return code of the called zsh functions (_describe, _arguments). This tells zsh if completions were found or not, which if not, will trigger different matching attempts, such as matching what the user typed with the the content of possible completions (instead of just as the prefix).

authored by Marc Khouzam · committed by GitHub · parent 2d94892 · Browse files · 2 files+34 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>

Diff stats

2 files+34 added−20 removed

  • other:+34 added−20 removed

2 files+34 added−20 removed

modified:completions.go-4 removed
173 unmodified lines
174 // Print each possible completion to stdout for the completion script to consume.175 fmt.Fprintln(finalCmd.OutOrStdout(), comp)176 }177178 if directive >= shellCompDirectiveMaxValue {179 directive = ShellCompDirectiveDefault180 }181178 // As the last printout, print the completion directive for the completion script to parse.179 // The directive integer must be that last character following a single colon (:).180 // The completion script expects :<directive>181 fmt.Fprintf(finalCmd.OutOrStdout(), ":%d\n", directive)
560 unmodified lines
modified:zsh_completions.go+34 added-16 removed
93 unmodified lines
94 local shellCompDirectiveNoFileComp=%[5]d95 local shellCompDirectiveFilterFileExt=%[6]d96 local shellCompDirectiveFilterDirs=%[7]d9798 local lastParam lastChar flagPrefix requestComp out directive compCount comp lastComp98 local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace99 local -a completions100101 __%[1]s_debug "\n========= starting completion logic =========="102 __%[1]s_debug "CURRENT: ${CURRENT}, words[*]: ${words[*]}"
59 unmodified lines
162 __%[1]s_debug "Completion received error. Ignoring completions."163 return164 fi165166 compCount=0166 while IFS='\n' read -r comp; do167 if [ -n "$comp" ]; then168 # If requested, completions are returned with a description.169 # The description is preceded by a TAB character.
3 unmodified lines
173174 local tab=$(printf '\t')175 comp=${comp//$tab/:}176178 ((compCount++))177 __%[1]s_debug "Adding completion: ${comp}"178 completions+=${comp}179 lastComp=$comp180 fi181 done < <(printf "%%s\n" "${out[@]}")182183 if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then184 __%[1]s_debug "Activating nospace."185 noSpace="-S ''"186 fi187188 if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then189 # File extension filtering190 local filteringCmd191 filteringCmd='_files'
18 unmodified lines
210 else211 __%[1]s_debug "Listing directories in ."212 fi213214 local result215 _arguments '*:dirname:_files -/'" ${flagPrefix}"216 result=$?217 if [ -n "$subdir" ]; then218 popd >/dev/null 2>&1219 fi215 elif [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ] && [ ${compCount} -eq 1 ]; then216 __%[1]s_debug "Activating nospace."217 # We can use compadd here as there is no description when218 # there is only one completion.219 compadd -S '' "${lastComp}"220 elif [ ${compCount} -eq 0 ]; then221 if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then222 __%[1]s_debug "deactivating file completion"220 return $result221 else222 __%[1]s_debug "Calling _describe"223 if eval _describe "completions" completions $flagPrefix $noSpace; then224 __%[1]s_debug "_describe found some completions"225226 # Return the success of having called _describe227 return 0228 else229 __%[1]s_debug "_describe did not find completions."230 __%[1]s_debug "Checking if we should do file completion."231 if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then232 __%[1]s_debug "deactivating file completion"233234 # We must return an error code here to let zsh know that there were no235 # completions found by _describe; this is what will trigger other236 # matching algorithms to attempt to find completions.237 # For example zsh can match letters in the middle of words.238 return 1239 else224 # Perform file completion225 __%[1]s_debug "activating file completion"226 _arguments '*:filename:_files'" ${flagPrefix}"240 # Perform file completion241 __%[1]s_debug "Activating file completion"242243 # We must return the result of this command, so it must be the244 # last command, or else we must store its result to return it.245 _arguments '*:filename:_files'" ${flagPrefix}"246 fi247 fi228 else229 _describe "completions" completions $(echo $flagPrefix)248 fi249}250251# don't run the completion function when being source-ed or eval-ed
7 unmodified lines