Fix multiple fish completion issues (#1249)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
* Fix fish for ShellDirectiveNoSpace and file comp For fish shell we achieve ShellDirectiveNoSpace by outputing a fake second completion with an extra character. However, this extra character was being added after the description string, instead of before. This commit fixes that. It also cleans up the script of useless code, now that fish completion details are better understood. Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca> * Handle case when completion starts with a space Fixes #1303 Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca> * Support fish completion with env vars in the path Fixes https://github.com/spf13/cobra/issues/1214 Fixes https://github.com/spf13/cobra/issues/1306 Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca> * Update based on review 1- We use `set -l` for local variable to make sure there are no conflicts with global variables 2- We use `commandline -opc` which: a) splits the command line into tokens (-o) b) only considers the current command (-p) (e.g., echo hello; helm <TAB>) c) stops at the cursor (-c) 3- We extract the last arg with `commandline -ct` and escape it to handle the case where it is a space, or unmatched quote. 4- We avoid looping when filtering on prefix. 5- We don't add a fake comp for ShellCompDirectiveNoSpace when the completion ends with any of @=/:., as fish won't add a space
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
1 file+81 added−82 removed
- other:+81 added−82 removed
Provenance
- Commit
c2e21bd- Tree
c914a74- Author
Marc Khouzam <marc.khouzam@montreal.ca>- Committer
GitHub <noreply@github.com>- Parent
95d23d2
1 file changed+81 added−82 removed
modified:fish_completions.go+81 added-82 removed
19 unmodified lines20 }21 WriteStringAndCheck(buf, fmt.Sprintf("# fish completion for %-36s -*- shell-script -*-\n", name))22 WriteStringAndCheck(buf, fmt.Sprintf(`23 function __%[1]s_debug24- set file "$BASH_COMP_DEBUG_FILE"24+ set -l file "$BASH_COMP_DEBUG_FILE"25 if test -n "$file"26 echo "$argv" >> $file27 end28 end29 30 function __%[1]s_perform_completion31- __%[1]s_debug "Starting __%[1]s_perform_completion with: $argv"31+ __%[1]s_debug "Starting __%[1]s_perform_completion"32 33- set args (string split -- " " "$argv")34- set lastArg "$args[-1]"33+ # Extract all args except the last one34+ set -l args (commandline -opc)35+ # Extract the last arg and escape it in case it is a space36+ set -l lastArg (string escape -- (commandline -ct))37 38 __%[1]s_debug "args: $args"39 __%[1]s_debug "last arg: $lastArg"40 39- set emptyArg ""40- if test -z "$lastArg"41- __%[1]s_debug "Setting emptyArg"42- set emptyArg \"\"43- end44- __%[1]s_debug "emptyArg: $emptyArg"41+ set -l requestComp "$args[1] %[3]s $args[2..-1] $lastArg"42 46- if not type -q "$args[1]"47- # This can happen when "complete --do-complete %[2]s" is called when running this script.48- __%[1]s_debug "Cannot find $args[1]. No completions."49- return50- end51-52- set requestComp "$args[1] %[3]s $args[2..-1] $emptyArg"43 __%[1]s_debug "Calling $requestComp"44+ set -l results (eval $requestComp 2> /dev/null)45 55- set results (eval $requestComp 2> /dev/null)56-46 # Some programs may output extra empty lines after the directive.47 # Let's ignore them or else it will break completion.48 # Ref: https://github.com/spf13/cobra/issues/127949 for line in $results[-1..1]4 unmodified lines54 # Found non-empty line, we have our proper output55 break56 end57 end69- set comps $results[1..-2]70- set directiveLine $results[-1]58 59+ set -l comps $results[1..-2]60+ set -l directiveLine $results[-1]61+62 # For Fish, when completing a flag with an = (e.g., <program> -n=<TAB>)63 # completions must be prefixed with the flag74- set flagPrefix (string match -r -- '-.*=' "$lastArg")64+ set -l flagPrefix (string match -r -- '-.*=' "$lastArg")65 66 __%[1]s_debug "Comps: $comps"67 __%[1]s_debug "DirectiveLine: $directiveLine"68 __%[1]s_debug "flagPrefix: $flagPrefix"4 unmodified lines73 74 printf "%%s\n" "$directiveLine"75 end76 87-# This function does three things:88-# 1- Obtain the completions and store them in the global __%[1]s_comp_results89-# 2- Set the __%[1]s_comp_do_file_comp flag if file completion should be performed90-# and unset it otherwise91-# 3- Return true if the completion results are not empty77+# This function does two things:78+# - Obtain the completions and store them in the global __%[1]s_comp_results79+# - Return false if file completion should be performed80 function __%[1]s_prepare_completions81+ __%[1]s_debug ""82+ __%[1]s_debug "========= starting completion logic =========="83+84 # Start fresh94- set --erase __%[1]s_comp_do_file_comp85 set --erase __%[1]s_comp_results86 97- # Check if the command-line is already provided. This is useful for testing.98- if not set --query __%[1]s_comp_commandLine99- # Use the -c flag to allow for completion in the middle of the line100- set __%[1]s_comp_commandLine (commandline -c)101- end102- __%[1]s_debug "commandLine is: $__%[1]s_comp_commandLine"103-104- set results (__%[1]s_perform_completion "$__%[1]s_comp_commandLine")105- set --erase __%[1]s_comp_commandLine87+ set -l results (__%[1]s_perform_completion)88 __%[1]s_debug "Completion results: $results"89 90 if test -z "$results"91 __%[1]s_debug "No completion, probably due to a failure"92 # Might as well do file completion, in case it helps111- set --global __%[1]s_comp_do_file_comp 193 return 194 end95 115- set directive (string sub --start 2 $results[-1])96+ set -l directive (string sub --start 2 $results[-1])97 set --global __%[1]s_comp_results $results[1..-2]98 99 __%[1]s_debug "Completions are: $__%[1]s_comp_results"100 __%[1]s_debug "Directive is: $directive"101 121- set shellCompDirectiveError %[4]d122- set shellCompDirectiveNoSpace %[5]d123- set shellCompDirectiveNoFileComp %[6]d124- set shellCompDirectiveFilterFileExt %[7]d125- set shellCompDirectiveFilterDirs %[8]d102+ set -l shellCompDirectiveError %[4]d103+ set -l shellCompDirectiveNoSpace %[5]d104+ set -l shellCompDirectiveNoFileComp %[6]d105+ set -l shellCompDirectiveFilterFileExt %[7]d106+ set -l shellCompDirectiveFilterDirs %[8]d107 108 if test -z "$directive"109 set directive 0110 end111 131- set compErr (math (math --scale 0 $directive / $shellCompDirectiveError) %% 2)112+ set -l compErr (math (math --scale 0 $directive / $shellCompDirectiveError) %% 2)113 if test $compErr -eq 1114 __%[1]s_debug "Received error directive: aborting."115 # Might as well do file completion, in case it helps135- set --global __%[1]s_comp_do_file_comp 1116 return 1117 end118 139- set filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) %% 2)140- set dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) %% 2)119+ set -l filefilter (math (math --scale 0 $directive / $shellCompDirectiveFilterFileExt) %% 2)120+ set -l dirfilter (math (math --scale 0 $directive / $shellCompDirectiveFilterDirs) %% 2)121 if test $filefilter -eq 1; or test $dirfilter -eq 1122 __%[1]s_debug "File extension filtering or directory filtering not supported"123 # Do full file completion instead144- set --global __%[1]s_comp_do_file_comp 1124 return 1125 end126 148- set nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) %% 2)149- set nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) %% 2)127+ set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) %% 2)128+ set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) %% 2)129 130 __%[1]s_debug "nospace: $nospace, nofiles: $nofiles"131 153- # Important not to quote the variable for count to work154- set numComps (count $__%[1]s_comp_results)155- __%[1]s_debug "numComps: $numComps"132+ # If we want to prevent a space, or if file completion is NOT disabled,133+ # we need to count the number of valid completions.134+ # To do so, we will filter on prefix as the completions we have received135+ # may not already be filtered so as to allow fish to match on different136+ # criteria than the prefix.137+ if test $nospace -ne 0; or test $nofiles -eq 0138+ set -l prefix (commandline -t | string escape --style=regex)139+ __%[1]s_debug "prefix: $prefix"140 157- if test $numComps -eq 1; and test $nospace -ne 0158- # To support the "nospace" directive we trick the shell159- # by outputting an extra, longer completion.160- __%[1]s_debug "Adding second completion to perform nospace directive"161- set --append __%[1]s_comp_results $__%[1]s_comp_results[1].162- end141+ set -l completions (string match -r -- "^$prefix.*" $__%[1]s_comp_results)142+ set --global __%[1]s_comp_results $completions143+ __%[1]s_debug "Filtered completions are: $__%[1]s_comp_results"144 164- if test $numComps -eq 0; and test $nofiles -eq 0165- __%[1]s_debug "Requesting file completion"166- set --global __%[1]s_comp_do_file_comp 1145+ # Important not to quote the variable for count to work146+ set -l numComps (count $__%[1]s_comp_results)147+ __%[1]s_debug "numComps: $numComps"148+149+ if test $numComps -eq 1; and test $nospace -ne 0150+ # We must first split on \t to get rid of the descriptions to be151+ # able to check what the actual completion will be.152+ # We don't need descriptions anyway since there is only a single153+ # real completion which the shell will expand immediately.154+ set -l split (string split --max 1 \t $__%[1]s_comp_results[1])155+ 156+ # Fish won't add a space if the completion ends with any157+ # of the following characters: @=/:.,158+ set -l lastChar (string sub -s -1 -- $split)159+ if not string match -r -q "[@=/:.,]" -- "$lastChar" 160+ # In other cases, to support the "nospace" directive we trick the shell161+ # by outputting an extra, longer completion.162+ __%[1]s_debug "Adding second completion to perform nospace directive"163+ set --global __%[1]s_comp_results $split[1] $split[1].164+ __%[1]s_debug "Completions are now: $__%[1]s_comp_results"165+ end166+ end167+168+ if test $numComps -eq 0; and test $nofiles -eq 0169+ # To be consistent with bash and zsh, we only trigger file170+ # completion when there are no other completions171+ __%[1]s_debug "Requesting file completion"172+ return 1173+ end174 end175 169- # If we don't want file completion, we must return true even if there170- # are no completions found. This is because fish will perform the last171- # completion command, even if its condition is false, if no other172- # completion command was triggered173- return (not set --query __%[1]s_comp_do_file_comp)176+ return 0177 end178 179 # Since Fish completions are only loaded once the user triggers them, we trigger them ourselves180 # so we can properly delete any completions provided by another script.178-# The space after the the program name is essential to trigger completion for the program179-# and not completion of the program name itself.180-complete --do-complete "%[2]s " > /dev/null 2>&1181-# Using '> /dev/null 2>&1' since '&>' is not supported in older versions of fish.181+# Only do this if the program can be found, or else fish may print some errors; besides,182+# the existing completions will only be loaded if the program can be found.183+if type -q "%[2]s"184+ # The space after the program name is essential to trigger completion for the program185+ # and not completion of the program name itself.186+ # Also, we use '> /dev/null 2>&1' since '&>' is not supported in older versions of fish.187+ complete --do-complete "%[2]s " > /dev/null 2>&1188+end189 190 # Remove any pre-existing completions for the program since we will be handling all of them.191 complete -c %[2]s -e192 186-# The order in which the below two lines are defined is very important so that __%[1]s_prepare_completions187-# is called first. It is __%[1]s_prepare_completions that sets up the __%[1]s_comp_do_file_comp variable.188-#189-# This completion will be run second as complete commands are added FILO.190-# It triggers file completion choices when __%[1]s_comp_do_file_comp is set.191-complete -c %[2]s -n 'set --query __%[1]s_comp_do_file_comp'192-193-# This completion will be run first as complete commands are added FILO.194-# The call to __%[1]s_prepare_completions will setup both __%[1]s_comp_results and __%[1]s_comp_do_file_comp.195-# It provides the program's completion choices.193+# The call to __%[1]s_prepare_completions will setup __%[1]s_comp_results194+# which provides the program's completion choices.195 complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'196 197 `, nameForVar, name, compCmd,198 ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,21 unmodified lines