feat(comp): Simplify Fish completion script

Signed-off-by: Marc Khouzam <marc.khouzam@montreal.ca>
pull/7690/head
Marc Khouzam 6 years ago
parent 1be0c9a236
commit 79f7e10864

@ -258,38 +258,38 @@ func runCompletionFish(out io.Writer, cmd *cobra.Command) error {
fishScript := fmt.Sprintf(`# fish completion for helm -*- shell-script -*- fishScript := fmt.Sprintf(`# fish completion for helm -*- shell-script -*-
function __helm_debug function __helm_debug
set -l file "$BASH_COMP_DEBUG_FILE" set file "$BASH_COMP_DEBUG_FILE"
if test -n "$file" if test -n "$file"
echo "$argv" >> $file echo "$argv" >> $file
end end
end end
function __helm_perform_completion function __helm_comp_perform
__helm_debug "Starting __helm_perform_completion with: $argv" __helm_debug "Starting __helm_comp_perform with: $argv"
set -l args (string split -- " " "$argv") set args (string split -- " " "$argv")
set -l lastArg "$args[-1]" set lastArg "$args[-1]"
__helm_debug "args: $args" __helm_debug "args: $args"
__helm_debug "last arg: $lastArg" __helm_debug "last arg: $lastArg"
set -l emptyArg "" set emptyArg ""
if test -z "$lastArg" if test -z "$lastArg"
__helm_debug "Setting emptyArg" __helm_debug "Setting emptyArg"
set emptyArg \"\" set emptyArg \"\"
end end
__helm_debug "emptyArg: $emptyArg" __helm_debug "emptyArg: $emptyArg"
set -l requestComp "$args[1] %[1]s $args[2..-1] $emptyArg" set requestComp "$args[1] %[1]s $args[2..-1] $emptyArg"
__helm_debug "Calling: $requestComp" __helm_debug "Calling: $requestComp"
set -l results (eval $requestComp 2> /dev/null) set results (eval $requestComp 2> /dev/null)
set -l comps $results[1..-2] set comps $results[1..-2]
set -l directiveLine $results[-1] set directiveLine $results[-1]
# For Fish, when completing a flag with an = (e.g., helm -n=<TAB>) # For Fish, when completing a flag with an = (e.g., helm -n=<TAB>)
# completions must be prefixed with the flag # completions must be prefixed with the flag
set -l flagPrefix (string match -r -- '-.*=' "$lastArg") set flagPrefix (string match -r -- '-.*=' "$lastArg")
__helm_debug "Comps are: $comps" __helm_debug "Comps are: $comps"
__helm_debug "DirectiveLine is: $directiveLine" __helm_debug "DirectiveLine is: $directiveLine"
@ -301,68 +301,93 @@ function __helm_perform_completion
printf "%%s\n" "$directiveLine" printf "%%s\n" "$directiveLine"
end end
function __helm_get_completions # This function does three things:
# Use the cache if possible # 1- Obtain the completions and store them in the global __helm_comp_results
if not set -q __helm_cache_completions # 2- Set the __helm_comp_do_file_comp flag if file completion should be performed
if not set -q __helm_comp_command_line # and unset it otherwise
set -g __helm_comp_command_line (commandline) # 3- Return true if the completion results are not empty
__helm_debug "Setting commandline to: $__helm_comp_command_line" function __helm_comp_prepare
# Start fresh
set --erase __helm_comp_do_file_comp
set --erase __helm_comp_results
# Check if the command-line is already provided. This is useful for testing.
if not set --query __helm_comp_commandLine
set __helm_comp_commandLine (commandline)
end end
__helm_debug "commandLine is: $__helm_comp_commandLine"
set -g __helm_cache_completions (__helm_perform_completion "$__helm_comp_command_line") set results (__helm_comp_perform "$__helm_comp_commandLine")
set -e __helm_comp_command_line set --erase __helm_comp_commandLine
__helm_debug "Populated completion cache with: $__helm_cache_completions" __helm_debug "Completion results: $results"
end
if test -z "$__helm_cache_completions" if test -z "$results"
__helm_debug "No completion, probably due to a failure" __helm_debug "No completion, probably due to a failure"
# Might as well do file completion, in case it helps
set --global __helm_comp_do_file_comp 1
return 0 return 0
end end
set -l directive (string sub --start 2 $__helm_cache_completions[-1]) set directive (string sub --start 2 $results[-1])
set -l comps $__helm_cache_completions[1..-2] set --global __helm_comp_results $results[1..-2]
__helm_debug "Completions are: $comps" __helm_debug "Completions are: $__helm_comp_results"
__helm_debug "Directive is: $directive" __helm_debug "Directive is: $directive"
set -l compErr (math (math $directive / %[2]d) %% 2) if test -z "$directive"
set directive 0
end
set compErr (math (math --scale 0 $directive / %[2]d) %% 2)
if test $compErr -eq 1 if test $compErr -eq 1
__helm_debug "Received error directive: aborting." __helm_debug "Received error directive: aborting."
# Might as well do file completion, in case it helps
set --global __helm_comp_do_file_comp 1
return 0 return 0
end end
set -l nospace (math (math $directive / %[3]d) %% 2) set nospace (math (math --scale 0 $directive / %[3]d) %% 2)
set -l nofiles (math (math $directive / %[4]d) %% 2) set nofiles (math (math --scale 0 $directive / %[4]d) %% 2)
__helm_debug "nospace: $nospace, nofiles: $nofiles" __helm_debug "nospace: $nospace, nofiles: $nofiles"
for comp in $comps # Important not to quote the variable for count to work
printf "%%s\n" $comp set numComps (count $__helm_comp_results)
end __helm_debug "numComps: $numComps"
if test (count $comps) -eq 1; and test $nospace -ne 0 if test $numComps -eq 1; and test $nospace -ne 0
# To support the "nospace" directive we trick the shell # To support the "nospace" directive we trick the shell
# by outputting an extra, longer completion. # by outputting an extra, longer completion.
printf "%%s\n" $comps[1]. __helm_debug "Adding second completion to perform nospace directive"
set --append __helm_comp_results $__helm_comp_results[1].
end
if test $numComps -eq 0; and test $nofiles -eq 0
__helm_debug "Requesting file completion"
set --global __helm_comp_do_file_comp 1
end end
# Return true if no file completion should be done # If we don't want file completion, we must return true even if there
test (count $comps) -gt 0; or test "$nofiles" -ne 0 # are no completions found. This is because fish will perform the last
# completion command, even if its condition is false, if no other
# completion command was triggered
return (not set --query __helm_comp_do_file_comp)
end end
# Remove any pre-existing helm completions since we will be handling all of them # Remove any pre-existing helm completions since we will be handling all of them
complete -c helm -e complete -c helm -e
# The order in which the below two lines are defined is very important so that the cache gets # The order in which the below two lines are defined is very important so that __helm_comp_prepare
# properly cleared at the very beginning of completions processing # is called first. It is __helm_comp_prepare that sets up the __helm_comp_do_file_comp variable.
# #
# This completion will be run second as complete commands are added FILO. It should use the cache. # This completion will be run second as complete commands are added FILO.
# It provides file completion choices when appropriate. # It triggers file completion choices when __helm_comp_do_file_comp is set.
complete -c helm -n 'not __helm_get_completions' complete -c helm -n 'set --query __helm_comp_do_file_comp'
# This completion will be run first as complete commands are added FILO. It first clears the cache. # This completion will be run first as complete commands are added FILO.
# The call to __helm_comp_prepare will setup both __helm_comp_results abd __helm_comp_do_file_comp.
# It provides the program's completion choices. # It provides the program's completion choices.
complete -c helm -n 'set -e __helm_cache_completions; __helm_get_completions' -f -a '(__helm_get_completions)' complete -c helm -n '__helm_comp_prepare' -f -a '$__helm_comp_results'
`, compCmd, completion.BashCompDirectiveError, completion.BashCompDirectiveNoSpace, completion.BashCompDirectiveNoFileComp) `, compCmd, completion.BashCompDirectiveError, completion.BashCompDirectiveNoSpace, completion.BashCompDirectiveNoFileComp)

Loading…
Cancel
Save