Implement completion NoMatching

This commit is contained in:
Raphael Vigee 2022-10-20 13:23:44 +01:00
parent badcce14f8
commit fbd01ec6c3
3 changed files with 31 additions and 11 deletions

View file

@ -77,6 +77,9 @@ const (
// obtain the same behavior but only for flags. // obtain the same behavior but only for flags.
ShellCompDirectiveFilterDirs ShellCompDirectiveFilterDirs
// ShellCompDirectiveNoMatching indicates that the provided completions will be
// passed to the user as-is, without additional matching (zsh and fish only).
ShellCompDirectiveNoMatching
// =========================================================================== // ===========================================================================
// All directives using iota should be above this one. // All directives using iota should be above this one.
@ -159,6 +162,9 @@ func (d ShellCompDirective) string() string {
if d&ShellCompDirectiveFilterDirs != 0 { if d&ShellCompDirectiveFilterDirs != 0 {
directives = append(directives, "ShellCompDirectiveFilterDirs") directives = append(directives, "ShellCompDirectiveFilterDirs")
} }
if d&ShellCompDirectiveNoMatching != 0 {
directives = append(directives, "ShellCompDirectiveNoMatching")
}
if len(directives) == 0 { if len(directives) == 0 {
directives = append(directives, "ShellCompDirectiveDefault") directives = append(directives, "ShellCompDirectiveDefault")
} }

View file

@ -53,7 +53,7 @@ function __%[1]s_perform_completion
__%[1]s_debug "last arg: $lastArg" __%[1]s_debug "last arg: $lastArg"
# Disable ActiveHelp which is not supported for fish shell # Disable ActiveHelp which is not supported for fish shell
set -l requestComp "%[9]s=0 $args[1] %[3]s $args[2..-1] $lastArg" set -l requestComp "%[10]s=0 $args[1] %[3]s $args[2..-1] $lastArg"
__%[1]s_debug "Calling $requestComp" __%[1]s_debug "Calling $requestComp"
set -l results (eval $requestComp 2> /dev/null) set -l results (eval $requestComp 2> /dev/null)
@ -119,6 +119,7 @@ function __%[1]s_prepare_completions
set -l shellCompDirectiveNoFileComp %[6]d set -l shellCompDirectiveNoFileComp %[6]d
set -l shellCompDirectiveFilterFileExt %[7]d set -l shellCompDirectiveFilterFileExt %[7]d
set -l shellCompDirectiveFilterDirs %[8]d set -l shellCompDirectiveFilterDirs %[8]d
set -l shellCompDirectiveNoMatching %[9]d
if test -z "$directive" if test -z "$directive"
set directive 0 set directive 0
@ -141,8 +142,9 @@ function __%[1]s_prepare_completions
set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) %% 2) set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) %% 2)
set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) %% 2) set -l nofiles (math (math --scale 0 $directive / $shellCompDirectiveNoFileComp) %% 2)
set -l nomatching (math (math --scale 0 $directive / $shellCompDirectiveNoMatching) %% 2)
__%[1]s_debug "nospace: $nospace, nofiles: $nofiles" __%[1]s_debug "nospace: $nospace, nofiles: $nofiles, nomatching: $nomatching"
# If we want to prevent a space, or if file completion is NOT disabled, # If we want to prevent a space, or if file completion is NOT disabled,
# we need to count the number of valid completions. # we need to count the number of valid completions.
@ -150,10 +152,14 @@ function __%[1]s_prepare_completions
# may not already be filtered so as to allow fish to match on different # may not already be filtered so as to allow fish to match on different
# criteria than the prefix. # criteria than the prefix.
if test $nospace -ne 0; or test $nofiles -eq 0 if test $nospace -ne 0; or test $nofiles -eq 0
set -l prefix (commandline -t | string escape --style=regex) if test $nomatching -ne 0
__%[1]s_debug "prefix: $prefix" set -l completions $__%[1]s_comp_results
else
set -l completions (string match -r -- "^$prefix.*" $__%[1]s_comp_results) set -l prefix (commandline -t | string escape --style=regex)
__%[1]s_debug "prefix: $prefix"
set -l completions (string match -r -- "^$prefix.*" $__%[1]s_comp_results)
end
set --global __%[1]s_comp_results $completions set --global __%[1]s_comp_results $completions
__%[1]s_debug "Filtered completions are: $__%[1]s_comp_results" __%[1]s_debug "Filtered completions are: $__%[1]s_comp_results"
@ -211,7 +217,8 @@ complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results'
`, nameForVar, name, compCmd, `, nameForVar, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveNoMatching,
activeHelpEnvVar(name)))
} }
// GenFishCompletion generates fish completion file and writes to the passed writer. // GenFishCompletion generates fish completion file and writes to the passed writer.

View file

@ -108,8 +108,9 @@ _%[1]s()
local shellCompDirectiveNoFileComp=%[5]d local shellCompDirectiveNoFileComp=%[5]d
local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterFileExt=%[6]d
local shellCompDirectiveFilterDirs=%[7]d local shellCompDirectiveFilterDirs=%[7]d
local shellCompDirectiveNoMatching=%[8]d
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace noMatching
local -a completions local -a completions
__%[1]s_debug "\n========= starting completion logic ==========" __%[1]s_debug "\n========= starting completion logic =========="
@ -177,7 +178,7 @@ _%[1]s()
return return
fi fi
local activeHelpMarker="%[8]s" local activeHelpMarker="%[9]s"
local endIndex=${#activeHelpMarker} local endIndex=${#activeHelpMarker}
local startIndex=$((${#activeHelpMarker}+1)) local startIndex=$((${#activeHelpMarker}+1))
local hasActiveHelp=0 local hasActiveHelp=0
@ -261,8 +262,14 @@ _%[1]s()
fi fi
return $result return $result
else else
if [ $((directive & shellCompDirectiveNoMatching)) -ne 0 ]; then
__heph_debug "Activating no matching."
compstate[insert]=automenu
noMatching="-U -o nosort"
fi
__%[1]s_debug "Calling _describe" __%[1]s_debug "Calling _describe"
if eval _describe "completions" completions $flagPrefix $noSpace; then if eval _describe "completions" completions $noMatching $flagPrefix $noSpace; then
__%[1]s_debug "_describe found some completions" __%[1]s_debug "_describe found some completions"
# Return the success of having called _describe # Return the success of having called _describe
@ -296,6 +303,6 @@ if [ "$funcstack[1]" = "_%[1]s" ]; then
fi fi
`, name, compCmd, `, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveNoMatching,
activeHelpMarker)) activeHelpMarker))
} }