From fbd01ec6c3118865121df0ec07f7e925fa6f75b2 Mon Sep 17 00:00:00 2001 From: Raphael Vigee Date: Thu, 20 Oct 2022 13:23:44 +0100 Subject: [PATCH] Implement completion NoMatching --- completions.go | 6 ++++++ fish_completions.go | 21 ++++++++++++++------- zsh_completions.go | 15 +++++++++++---- 3 files changed, 31 insertions(+), 11 deletions(-) diff --git a/completions.go b/completions.go index e8a0206db..6508d07ce 100644 --- a/completions.go +++ b/completions.go @@ -77,6 +77,9 @@ const ( // obtain the same behavior but only for flags. 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. @@ -159,6 +162,9 @@ func (d ShellCompDirective) string() string { if d&ShellCompDirectiveFilterDirs != 0 { directives = append(directives, "ShellCompDirectiveFilterDirs") } + if d&ShellCompDirectiveNoMatching != 0 { + directives = append(directives, "ShellCompDirectiveNoMatching") + } if len(directives) == 0 { directives = append(directives, "ShellCompDirectiveDefault") } diff --git a/fish_completions.go b/fish_completions.go index 97112a17b..8f362afff 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -53,7 +53,7 @@ function __%[1]s_perform_completion __%[1]s_debug "last arg: $lastArg" # 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" set -l results (eval $requestComp 2> /dev/null) @@ -119,6 +119,7 @@ function __%[1]s_prepare_completions set -l shellCompDirectiveNoFileComp %[6]d set -l shellCompDirectiveFilterFileExt %[7]d set -l shellCompDirectiveFilterDirs %[8]d + set -l shellCompDirectiveNoMatching %[9]d if test -z "$directive" set directive 0 @@ -141,8 +142,9 @@ function __%[1]s_prepare_completions set -l nospace (math (math --scale 0 $directive / $shellCompDirectiveNoSpace) %% 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, # 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 # criteria than the prefix. if test $nospace -ne 0; or test $nofiles -eq 0 - 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) + if test $nomatching -ne 0 + set -l completions $__%[1]s_comp_results + else + 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 __%[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, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveNoMatching, + activeHelpEnvVar(name))) } // GenFishCompletion generates fish completion file and writes to the passed writer. diff --git a/zsh_completions.go b/zsh_completions.go index 84cec76fd..33e96e81b 100644 --- a/zsh_completions.go +++ b/zsh_completions.go @@ -108,8 +108,9 @@ _%[1]s() local shellCompDirectiveNoFileComp=%[5]d local shellCompDirectiveFilterFileExt=%[6]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 __%[1]s_debug "\n========= starting completion logic ==========" @@ -177,7 +178,7 @@ _%[1]s() return fi - local activeHelpMarker="%[8]s" + local activeHelpMarker="%[9]s" local endIndex=${#activeHelpMarker} local startIndex=$((${#activeHelpMarker}+1)) local hasActiveHelp=0 @@ -261,8 +262,14 @@ _%[1]s() fi return $result 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" - if eval _describe "completions" completions $flagPrefix $noSpace; then + if eval _describe "completions" completions $noMatching $flagPrefix $noSpace; then __%[1]s_debug "_describe found some completions" # Return the success of having called _describe @@ -296,6 +303,6 @@ if [ "$funcstack[1]" = "_%[1]s" ]; then fi `, name, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveNoMatching, activeHelpMarker)) }