Skip to content

Commit

Permalink
Implement completion NoMatching
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Oct 29, 2022
1 parent badcce1 commit 832f8c8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
6 changes: 6 additions & 0 deletions completions.go
Expand Up @@ -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.
Expand Down Expand Up @@ -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")
}
Expand Down
21 changes: 14 additions & 7 deletions fish_completions.go
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -141,19 +142,24 @@ 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.
# To do so, we will filter on prefix as the completions we have received
# 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 (string match -r -- "^.*" $__%[1]s_comp_results)
else
set -l prefix (commandline -t | string escape --style=regex)
__%[1]s_debug "prefix: $prefix"
set -l completions $__%[1]s_comp_results
end
set --global __%[1]s_comp_results $completions
__%[1]s_debug "Filtered completions are: $__%[1]s_comp_results"
Expand Down Expand Up @@ -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.
Expand Down
15 changes: 11 additions & 4 deletions zsh_completions.go
Expand Up @@ -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 =========="
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -296,6 +303,6 @@ if [ "$funcstack[1]" = "_%[1]s" ]; then
fi
`, name, compCmd,
ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs,
ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveNoMatching,
activeHelpMarker))
}

0 comments on commit 832f8c8

Please sign in to comment.