From a3adbf86deeef7ebfa2c389b21925ee6367af092 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Mon, 30 Jan 2023 11:07:56 +0000 Subject: [PATCH 01/10] added keep order to bash, zsh, powershell & fish --- bash_completionsV2.go | 20 +++++++++++++------- completions.go | 7 +++++++ fish_completions.go | 30 ++++++++++++++++++++++++++---- powershell_completions.go | 10 ++++++++-- shell_completions.md | 4 ++++ zsh_completions.go | 14 ++++++++++---- 6 files changed, 68 insertions(+), 17 deletions(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 78f43b91a..2bbed7e21 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -101,6 +101,7 @@ __%[1]s_process_completion_results() { local shellCompDirectiveNoFileComp=%[5]d local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterDirs=%[7]d + local shellCompDirectiveKeepOrder=%[8]d if (((directive & shellCompDirectiveError) != 0)); then # Error code. No completion. @@ -115,6 +116,14 @@ __%[1]s_process_completion_results() { __%[1]s_debug "No space directive not supported in this version of bash" fi fi + if (((directive & shellCompDirectiveKeepOrder) != 0)); then + if [[ $(type -t compopt) == builtin ]]; then + __%[1]s_debug "Activating keep order" + compopt -o nosort + else + __%[1]s_debug "No sort directive not supported in this version of bash" + fi + fi if (((directive & shellCompDirectiveNoFileComp) != 0)); then if [[ $(type -t compopt) == builtin ]]; then __%[1]s_debug "Activating no file completion" @@ -183,7 +192,7 @@ __%[1]s_process_completion_results() { # Separate activeHelp lines from real completions. # Fills the $activeHelp and $completions arrays. __%[1]s_extract_activeHelp() { - local activeHelpMarker="%[8]s" + local activeHelpMarker="%[9]s" local endIndex=${#activeHelpMarker} while IFS='' read -r comp; do @@ -351,16 +360,13 @@ __start_%[1]s() __%[1]s_process_completion_results } -if [[ $(type -t compopt) = "builtin" ]]; then - complete -o default -F __start_%[1]s %[1]s -else - complete -o default -o nospace -F __start_%[1]s %[1]s -fi +# compopts are already set, we don't have to specify them here +complete -F __start_%[1]s %[1]s # ex: ts=4 sw=4 et filetype=sh `, name, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpMarker)) } diff --git a/completions.go b/completions.go index 8341959ab..c7b457eb2 100644 --- a/completions.go +++ b/completions.go @@ -77,6 +77,10 @@ const ( // obtain the same behavior but only for flags. ShellCompDirectiveFilterDirs + // ShellCompDirectiveKeepOrder indicates that the shell should preserve the order + // in which the completions are provided + ShellCompDirectiveKeepOrder + // =========================================================================== // All directives using iota should be above this one. @@ -159,6 +163,9 @@ func (d ShellCompDirective) string() string { if d&ShellCompDirectiveFilterDirs != 0 { directives = append(directives, "ShellCompDirectiveFilterDirs") } + if d&ShellCompDirectiveKeepOrder != 0 { + directives = append(directives, "ShellCompDirectiveKeepOrder") + } if len(directives) == 0 { directives = append(directives, "ShellCompDirectiveDefault") } diff --git a/fish_completions.go b/fish_completions.go index 97112a17b..214037f83 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) @@ -89,6 +89,25 @@ function __%[1]s_perform_completion printf "%%s\n" "$directiveLine" end +function __%[1]s_doesnt_requires_order_preservation + __%[1]s_debug "" + __%[1]s_debug "========= checking if order preservation is required ==========" + + set -l results (__%[1]s_perform_completion) + if test -z "$results" + __%[1]s_debug "Error determining if order preservation is required" + return 1 + end + + set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) %% 2) + + if test "$keeporder" -ne 0 + __%[1]s_debug "This does require order preservation" + return 1 + end +end + + # This function does two things: # - Obtain the completions and store them in the global __%[1]s_comp_results # - Return false if file completion should be performed @@ -119,6 +138,7 @@ function __%[1]s_prepare_completions set -l shellCompDirectiveNoFileComp %[6]d set -l shellCompDirectiveFilterFileExt %[7]d set -l shellCompDirectiveFilterDirs %[8]d + set -l shellCompDirectiveKeepOrder %[9]d if test -z "$directive" set directive 0 @@ -207,11 +227,13 @@ complete -c %[2]s -e # The call to __%[1]s_prepare_completions will setup __%[1]s_comp_results # which provides the program's completion choices. -complete -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' - +# if this doesn't require order preservation, we dont use the -k flag +complete -c %[2]s -n '__%[1]s_doesnt_requires_order_preservation' -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' +# otherwise we use the -k flag +complete -k -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, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name))) } // GenFishCompletion generates fish completion file and writes to the passed writer. diff --git a/powershell_completions.go b/powershell_completions.go index c9d47e614..c766fc785 100644 --- a/powershell_completions.go +++ b/powershell_completions.go @@ -77,6 +77,7 @@ filter __%[1]s_escapeStringWithSpecialChars { $ShellCompDirectiveNoFileComp=%[6]d $ShellCompDirectiveFilterFileExt=%[7]d $ShellCompDirectiveFilterDirs=%[8]d + $ShellCompDirectiveKeepOrder=%[9]d # Prepare the command to request completions for the program. # Split the command at the first space to separate the program and arguments. @@ -112,7 +113,7 @@ filter __%[1]s_escapeStringWithSpecialChars { __%[1]s_debug "Calling $RequestComp" # First disable ActiveHelp which is not supported for Powershell - $env:%[9]s=0 + $env:%[10]s=0 #call the command store the output in $out and redirect stderr and stdout to null # $Out is an array contains each line per element @@ -182,6 +183,11 @@ filter __%[1]s_escapeStringWithSpecialChars { } } + # we sort the values in ascending order by name if keep order isn't passed + if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) { + $Values = $Values | Sort-Object -Property Name + } + if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) { __%[1]s_debug "ShellCompDirectiveNoFileComp is called" @@ -267,7 +273,7 @@ filter __%[1]s_escapeStringWithSpecialChars { Register-ArgumentCompleter -CommandName '%[1]s' -ScriptBlock $__%[2]sCompleterBlock `, name, nameForVar, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name))) } func (c *Command) genPowerShellCompletion(w io.Writer, includeDesc bool) error { diff --git a/shell_completions.md b/shell_completions.md index f3285e310..efbbab84e 100644 --- a/shell_completions.md +++ b/shell_completions.md @@ -237,6 +237,10 @@ ShellCompDirectiveFilterFileExt // return []string{"themes"}, ShellCompDirectiveFilterDirs // ShellCompDirectiveFilterDirs + +// ShellCompDirectiveKeepOrder indicates that the shell should preserve the order +// in which the completions are provided +ShellCompDirectiveKeepOrder ``` ***Note***: When using the `ValidArgsFunction`, Cobra will call your registered function after having parsed all flags and arguments provided in the command-line. You therefore don't need to do this parsing yourself. For example, when a user calls `helm status --namespace my-rook-ns [tab][tab]`, Cobra will call your registered `ValidArgsFunction` after having parsed the `--namespace` flag, as it would have done when calling the `RunE` function. diff --git a/zsh_completions.go b/zsh_completions.go index 84cec76fd..29bcb570a 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 shellCompDirectiveKeepOrder=%[8]d - local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace + local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder 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 @@ -227,6 +228,11 @@ _%[1]s() noSpace="-S ''" fi + if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then + __%[1]s_debug "Activating keep order." + keepOrder="-V" + fi + if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then # File extension filtering local filteringCmd @@ -262,7 +268,7 @@ _%[1]s() return $result else __%[1]s_debug "Calling _describe" - if eval _describe "completions" completions $flagPrefix $noSpace; then + if eval _describe $keepOrder "completions" completions $flagPrefix $noSpace; then __%[1]s_debug "_describe found some completions" # Return the success of having called _describe @@ -296,6 +302,6 @@ if [ "$funcstack[1]" = "_%[1]s" ]; then fi `, name, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpMarker)) } From bc561bcbf34f40e0f71b851470af064b2d34e3e7 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Mon, 30 Jan 2023 18:35:48 +0000 Subject: [PATCH 02/10] fix a tiny bug with bash --- bash_completionsV2.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 2bbed7e21..aa387b292 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -360,8 +360,11 @@ __start_%[1]s() __%[1]s_process_completion_results } -# compopts are already set, we don't have to specify them here -complete -F __start_%[1]s %[1]s +if [[ $(type -t compopt) = "builtin" ]]; then + complete -o default -F __start_%[1]s %[1]s +else + complete -F __start_%[1]s %[1]s +fi # ex: ts=4 sw=4 et filetype=sh `, name, compCmd, From 6e475b66672d9f3a563165f04639ac7602bcea84 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Mon, 30 Jan 2023 18:39:41 +0000 Subject: [PATCH 03/10] fix indentation --- fish_completions.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index 214037f83..1ff575976 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -101,10 +101,10 @@ function __%[1]s_doesnt_requires_order_preservation set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) %% 2) - if test "$keeporder" -ne 0 - __%[1]s_debug "This does require order preservation" - return 1 - end + if test "$keeporder" -ne 0 + __%[1]s_debug "This does require order preservation" + return 1 + end end From 87cd9437e12b95810770f02ec1e55bcd774c2256 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Mon, 30 Jan 2023 19:13:15 +0000 Subject: [PATCH 04/10] added keep order to default bash completions --- bash_completions.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index a9f4e4f2b..5adcbb77e 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -81,6 +81,7 @@ __%[1]s_handle_go_custom_completion() local shellCompDirectiveNoFileComp=%[5]d local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterDirs=%[7]d + local shellCompDirectiveKeepOrder=%[8]d local out requestComp lastParam lastChar comp directive args @@ -88,7 +89,7 @@ __%[1]s_handle_go_custom_completion() # Calling ${words[0]} instead of directly %[1]s allows to handle aliases args=("${words[@]:1}") # Disable ActiveHelp which is not supported for bash completion v1 - requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}" + requestComp="%[9]s=0 ${words[0]} %[2]s ${args[*]}" lastParam=${words[$((${#words[@]}-1))]} lastChar=${lastParam:$((${#lastParam}-1)):1} @@ -127,6 +128,12 @@ __%[1]s_handle_go_custom_completion() compopt -o nospace fi fi + if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then + if [[ $(type -t compopt) = "builtin" ]]; then + __%[1]s_debug "${FUNCNAME[0]}: activating keep order" + compopt -o nosort + fi + fi if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then if [[ $(type -t compopt) = "builtin" ]]; then __%[1]s_debug "${FUNCNAME[0]}: activating no file completion" @@ -398,7 +405,7 @@ __%[1]s_handle_word() `, name, ShellCompNoDescRequestCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name))) } func writePostscript(buf io.StringWriter, name string) { @@ -437,7 +444,7 @@ func writePostscript(buf io.StringWriter, name string) { WriteStringAndCheck(buf, fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then complete -o default -F __start_%s %s else - complete -o default -o nospace -F __start_%s %s + complete -F __start_%s %s fi `, name, name, name, name)) From 3fc2de1ba4f3470e470cb2d8aedbfe420be26e48 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Thu, 2 Feb 2023 16:28:13 +0000 Subject: [PATCH 05/10] removed changes that arent necessary for new bash --- bash_completions.go | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/bash_completions.go b/bash_completions.go index 5adcbb77e..a9f4e4f2b 100644 --- a/bash_completions.go +++ b/bash_completions.go @@ -81,7 +81,6 @@ __%[1]s_handle_go_custom_completion() local shellCompDirectiveNoFileComp=%[5]d local shellCompDirectiveFilterFileExt=%[6]d local shellCompDirectiveFilterDirs=%[7]d - local shellCompDirectiveKeepOrder=%[8]d local out requestComp lastParam lastChar comp directive args @@ -89,7 +88,7 @@ __%[1]s_handle_go_custom_completion() # Calling ${words[0]} instead of directly %[1]s allows to handle aliases args=("${words[@]:1}") # Disable ActiveHelp which is not supported for bash completion v1 - requestComp="%[9]s=0 ${words[0]} %[2]s ${args[*]}" + requestComp="%[8]s=0 ${words[0]} %[2]s ${args[*]}" lastParam=${words[$((${#words[@]}-1))]} lastChar=${lastParam:$((${#lastParam}-1)):1} @@ -128,12 +127,6 @@ __%[1]s_handle_go_custom_completion() compopt -o nospace fi fi - if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then - if [[ $(type -t compopt) = "builtin" ]]; then - __%[1]s_debug "${FUNCNAME[0]}: activating keep order" - compopt -o nosort - fi - fi if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then if [[ $(type -t compopt) = "builtin" ]]; then __%[1]s_debug "${FUNCNAME[0]}: activating no file completion" @@ -405,7 +398,7 @@ __%[1]s_handle_word() `, name, ShellCompNoDescRequestCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, - ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name))) + ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, activeHelpEnvVar(name))) } func writePostscript(buf io.StringWriter, name string) { @@ -444,7 +437,7 @@ func writePostscript(buf io.StringWriter, name string) { WriteStringAndCheck(buf, fmt.Sprintf(`if [[ $(type -t compopt) = "builtin" ]]; then complete -o default -F __start_%s %s else - complete -F __start_%s %s + complete -o default -o nospace -F __start_%s %s fi `, name, name, name, name)) From 22ae670a3ed1b68a5eb3cce30b7203e1f83536de Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Tue, 7 Feb 2023 09:03:58 +0000 Subject: [PATCH 06/10] fix fish --- fish_completions.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index 1ff575976..1331e785c 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -99,12 +99,20 @@ function __%[1]s_doesnt_requires_order_preservation return 1 end + set -l directive (string sub --start 2 $results[-1]) + __%[1]s_debug "Directive is: $directive" + + set -l shellCompDirectiveKeepOrder %[9]d set -l keeporder (math (math --scale 0 $directive / $shellCompDirectiveKeepOrder) %% 2) + __%[1]s_debug "Keeporder is: $keeporder" - if test "$keeporder" -ne 0 + if test $keeporder -ne 0 __%[1]s_debug "This does require order preservation" return 1 end + + __%[1]s_debug "This doesn't require order preservation" + return 0 end @@ -138,7 +146,6 @@ function __%[1]s_prepare_completions set -l shellCompDirectiveNoFileComp %[6]d set -l shellCompDirectiveFilterFileExt %[7]d set -l shellCompDirectiveFilterDirs %[8]d - set -l shellCompDirectiveKeepOrder %[9]d if test -z "$directive" set directive 0 @@ -230,7 +237,7 @@ complete -c %[2]s -e # if this doesn't require order preservation, we dont use the -k flag complete -c %[2]s -n '__%[1]s_doesnt_requires_order_preservation' -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' # otherwise we use the -k flag -complete -k -c %[2]s -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' +complete -k -c %[2]s -n 'not __%[1]s_doesnt_requires_order_preservation' -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' `, nameForVar, name, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name))) From cbcd050e4f0d44e84d7509d48c0182fc8b645d60 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Mon, 13 Feb 2023 10:09:38 +0000 Subject: [PATCH 07/10] worked on some reviews --- fish_completions.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index 1331e785c..956d0e83c 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -89,7 +89,7 @@ function __%[1]s_perform_completion printf "%%s\n" "$directiveLine" end -function __%[1]s_doesnt_requires_order_preservation +function __%[1]s_requires_order_preservation __%[1]s_debug "" __%[1]s_debug "========= checking if order preservation is required ==========" @@ -108,11 +108,11 @@ function __%[1]s_doesnt_requires_order_preservation if test $keeporder -ne 0 __%[1]s_debug "This does require order preservation" - return 1 + return 0 end __%[1]s_debug "This doesn't require order preservation" - return 0 + return 1 end @@ -234,10 +234,10 @@ complete -c %[2]s -e # The call to __%[1]s_prepare_completions will setup __%[1]s_comp_results # which provides the program's completion choices. -# if this doesn't require order preservation, we dont use the -k flag -complete -c %[2]s -n '__%[1]s_doesnt_requires_order_preservation' -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' +# If this doesn't require order preservation, we don't use the -k flag +complete -c %[2]s -n 'not __%[1]s_requires_order_preservation && __%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' # otherwise we use the -k flag -complete -k -c %[2]s -n 'not __%[1]s_doesnt_requires_order_preservation' -n '__%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' +complete -k -c %[2]s -n '__%[1]s_requires_order_preservation && __%[1]s_prepare_completions' -f -a '$__%[1]s_comp_results' `, nameForVar, name, compCmd, ShellCompDirectiveError, ShellCompDirectiveNoSpace, ShellCompDirectiveNoFileComp, ShellCompDirectiveFilterFileExt, ShellCompDirectiveFilterDirs, ShellCompDirectiveKeepOrder, activeHelpEnvVar(name))) From d115eb62494a45289f88d104d9605a6b127b9077 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Mon, 13 Feb 2023 10:16:58 +0000 Subject: [PATCH 08/10] support bash3 --- bash_completionsV2.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index aa387b292..555ecfb8b 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -363,7 +363,7 @@ __start_%[1]s() if [[ $(type -t compopt) = "builtin" ]]; then complete -o default -F __start_%[1]s %[1]s else - complete -F __start_%[1]s %[1]s + complete -o default -o nospace -F __start_%[1]s %[1]s fi # ex: ts=4 sw=4 et filetype=sh From fcd84a98d16af70fcd249c27f99ae8690adab443 Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Mon, 13 Feb 2023 13:01:42 +0000 Subject: [PATCH 09/10] added a comment --- bash_completionsV2.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/bash_completionsV2.go b/bash_completionsV2.go index 555ecfb8b..5a38e38d5 100644 --- a/bash_completionsV2.go +++ b/bash_completionsV2.go @@ -118,8 +118,13 @@ __%[1]s_process_completion_results() { fi if (((directive & shellCompDirectiveKeepOrder) != 0)); then if [[ $(type -t compopt) == builtin ]]; then - __%[1]s_debug "Activating keep order" - compopt -o nosort + # no sort isn't supported for bash less than < 4.4 + if [[ ${BASH_VERSINFO[0]} -lt 4 || ( ${BASH_VERSINFO[0]} -eq 4 && ${BASH_VERSINFO[1]} -lt 4 ) ]]; then + __%[1]s_debug "No sort directive not supported in this version of bash" + else + __%[1]s_debug "Activating keep order" + compopt -o nosort + fi else __%[1]s_debug "No sort directive not supported in this version of bash" fi From 3b1d173f299938e930964108963ec96193d4421a Mon Sep 17 00:00:00 2001 From: Gyanendra Mishra Date: Tue, 14 Feb 2023 10:41:20 +0000 Subject: [PATCH 10/10] added logic to reduce calls to underlying program --- fish_completions.go | 45 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 8 deletions(-) diff --git a/fish_completions.go b/fish_completions.go index 956d0e83c..32c4bea0a 100644 --- a/fish_completions.go +++ b/fish_completions.go @@ -89,17 +89,44 @@ function __%[1]s_perform_completion printf "%%s\n" "$directiveLine" end +# this function limits calls to __%[1]s_perform_completion, by caching the result behind $__%[1]s_perform_completion_once_result +function __%[1]s_perform_completion_once + __%[1]s_debug "Starting __%[1]s_perform_completion_once" + + if test -n "$__%[1]s_perform_completion_once_result" + __%[1]s_debug "Seems like a valid result already exists, skipping __%[1]s_perform_completion" + return 0 + end + + set --global __%[1]s_perform_completion_once_result (__%[1]s_perform_completion) + if test -z "$__%[1]s_perform_completion_once_result" + __%[1]s_debug "No completions, probably due to a failure" + return 1 + end + + __%[1]s_debug "Performed completions and set __%[1]s_perform_completion_once_result" + return 0 +end + +# this function is used to clear the $__%[1]s_perform_completion_once_result variable after completions are run +function __%[1]s_clear_perform_completion_once_result + __%[1]s_debug "" + __%[1]s_debug "========= clearing previously set __%[1]s_perform_completion_once_result variable ==========" + set --erase __%[1]s_perform_completion_once_result + __%[1]s_debug "Succesfully erased the variable __%[1]s_perform_completion_once_result" +end + function __%[1]s_requires_order_preservation __%[1]s_debug "" __%[1]s_debug "========= checking if order preservation is required ==========" - set -l results (__%[1]s_perform_completion) - if test -z "$results" + __%[1]s_perform_completion_once + if test -z "$__%[1]s_perform_completion_once_result" __%[1]s_debug "Error determining if order preservation is required" return 1 end - set -l directive (string sub --start 2 $results[-1]) + set -l directive (string sub --start 2 $__%[1]s_perform_completion_once_result[-1]) __%[1]s_debug "Directive is: $directive" set -l shellCompDirectiveKeepOrder %[9]d @@ -126,17 +153,17 @@ function __%[1]s_prepare_completions # Start fresh set --erase __%[1]s_comp_results - set -l results (__%[1]s_perform_completion) - __%[1]s_debug "Completion results: $results" + __%[1]s_perform_completion_once + __%[1]s_debug "Completion results: $__%[1]s_perform_completion_once_result" - if test -z "$results" + if test -z "$__%[1]s_perform_completion_once_result" __%[1]s_debug "No completion, probably due to a failure" # Might as well do file completion, in case it helps return 1 end - set -l directive (string sub --start 2 $results[-1]) - set --global __%[1]s_comp_results $results[1..-2] + set -l directive (string sub --start 2 $__%[1]s_perform_completion_once_result[-1]) + set --global __%[1]s_comp_results $__%[1]s_perform_completion_once_result[1..-2] __%[1]s_debug "Completions are: $__%[1]s_comp_results" __%[1]s_debug "Directive is: $directive" @@ -232,6 +259,8 @@ end # Remove any pre-existing completions for the program since we will be handling all of them. complete -c %[2]s -e +# this will get called after the two calls below and clear the $__%[1]s_perform_completion_once_result global +complete -c %[2]s -n '__%[1]s_clear_perform_completion_once_result' # The call to __%[1]s_prepare_completions will setup __%[1]s_comp_results # which provides the program's completion choices. # If this doesn't require order preservation, we don't use the -k flag