Repository: rsteube/carapace
Pull request #105 (2020-10-29 17:02:15)

fix completion
- zsh: use `{}` for callback to avoid flag arguments
- fish: invoke and remove autoloaded completion (https://github.com/fish-shell/fish-shell/issues/6716)


Patch:
diff --git a/fish/snippet.go b/fish/snippet.go
index c00d8bc..15badda 100644
--- a/fish/snippet.go
+++ b/fish/snippet.go
@@ -25,9 +25,10 @@ func snippetLazy(cmd *cobra.Command) string {
    %v _carapace fish | source
    complete --do-complete (commandline -cp)
 end
+complete --do-complete '%v ' > /dev/null
+complete -c %v -e
 complete -c %v -a '(_%v_completions)'
-complete -c %v -f
-`, cmd.Name(), cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name())
+`, cmd.Name(), cmd.Name(), uid.Executable(), cmd.Name(), cmd.Name(), cmd.Name(), cmd.Name())
 }
 
 func Snippet(cmd *cobra.Command, actions map[string]string, lazy bool) string {
diff --git a/zsh/action.go b/zsh/action.go
index 8f9e661..b782a5a 100644
--- a/zsh/action.go
+++ b/zsh/action.go
@@ -35,7 +35,7 @@ func Sanitize(values ...string) []string {
 }
 
 func Callback(prefix string, cuid string) string {
-	return fmt.Sprintf(`_%v_callback '%v'`, prefix, cuid)
+	return fmt.Sprintf(`{_%v_callback '%v'}`, prefix, cuid)
 }
 
 func ActionDirectories() string {
diff --git a/zsh/snippet.go b/zsh/snippet.go
index a08f7e8..4d66789 100644
--- a/zsh/snippet.go
+++ b/zsh/snippet.go
@@ -37,7 +37,7 @@ func Snippet(cmd *cobra.Command, actions map[string]string, lazy bool) string {
 	result := fmt.Sprintf("#compdef %v\n", cmd.Name())
 	result += fmt.Sprintf(`function _%v_callback {
   # shellcheck disable=SC2086
-  eval "$(%v _carapace zsh "$5" ${os_args})"
+  eval "$(%v _carapace zsh "$1" ${os_args})"
 }
 `, cmd.Name(), uid.Executable())
 	result += snippetFunctions(cmd, actions)


Test patch:
diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go
index 6ed115c..d1a3b62 100644
--- a/example/cmd/root_test.go
+++ b/example/cmd/root_test.go
@@ -1232,7 +1232,7 @@ func TestZsh(t *testing.T) {
 	expected := `#compdef example
 function _example_callback {
   # shellcheck disable=SC2086
-  eval "$(example _carapace zsh "$5" ${os_args})"
+  eval "$(example _carapace zsh "$1" ${os_args})"
 }
 function _example {
   local -a commands
@@ -1292,14 +1292,14 @@ function _example__action {
     _arguments -C \
     "--directories[files flag]: :_files -/" \
     "(-f --files)"{-f,--files}"[files flag]: :_files -g '*.go'" \
-    "(-g --groups)"{-g,--groups}"[groups flag]: :_example_callback '_example__action##groups'" \
-    "--hosts[hosts flag]: :_example_callback '_example__action##hosts'" \
+    "(-g --groups)"{-g,--groups}"[groups flag]: :{_example_callback '_example__action##groups'}" \
+    "--hosts[hosts flag]: :{_example_callback '_example__action##hosts'}" \
     "(-k --kill)"{-k,--kill}"[kill signals]: :{local _comp_desc=('ABRT (Abnormal termination)' 'ALRM (Virtual alarm clock)' 'BUS (BUS error)' 'CHLD (Child status has changed)' 'CONT (Continue stopped process)' 'FPE (Floating-point exception)' 'HUP (Hangup detected on controlling terminal)' 'ILL (Illegal instruction)' 'INT (Interrupt from keyboard)' 'KILL (Kill, unblockable)' 'PIPE (Broken pipe)' 'POLL (Pollable event occurred)' 'PROF (Profiling alarm clock timer expired)' 'PWR (Power failure restart)' 'QUIT (Quit from keyboard)' 'SEGV (Segmentation violation)' 'STKFLT (Stack fault on coprocessor)' 'STOP (Stop process, unblockable)' 'SYS (Bad system call)' 'TERM (Termination request)' 'TRAP (Trace/breakpoint trap)' 'TSTP (Stop typed at keyboard)' 'TTIN (Background read from tty)' 'TTOU (Background write to tty)' 'URG (Urgent condition on socket)' 'USR1 (User-defined signal 1)' 'USR2 (User-defined signal 2)' 'VTALRM (Virtual alarm clock)' 'WINCH (Window size change)' 'XCPU (CPU time limit exceeded)' 'XFSZ (File size limit exceeded)');compadd -S '' -d _comp_desc 'ABRT' 'ALRM' 'BUS' 'CHLD' 'CONT' 'FPE' 'HUP' 'ILL' 'INT' 'KILL' 'PIPE' 'POLL' 'PROF' 'PWR' 'QUIT' 'SEGV' 'STKFLT' 'STOP' 'SYS' 'TERM' 'TRAP' 'TSTP' 'TTIN' 'TTOU' 'URG' 'USR1' 'USR2' 'VTALRM' 'WINCH' 'XCPU' 'XFSZ'}" \
     "(-m --message)"{-m,--message}"[message flag]: :{local _comp_desc=('_' 'ERR (message example)');compadd -S '' -d _comp_desc '_' 'ERR'}" \
-    "(-n --net_interfaces)"{-n,--net_interfaces}"[net_interfaces flag]: :_example_callback '_example__action##net_interfaces'" \
+    "(-n --net_interfaces)"{-n,--net_interfaces}"[net_interfaces flag]: :{_example_callback '_example__action##net_interfaces'}" \
     "(-o --optarg)"{-o=-,--optarg=-}"[optional arg with default value blue]:: :{local _comp_desc=('blue' 'red' 'green' 'yellow');compadd -S '' -d _comp_desc 'blue' 'red' 'green' 'yellow'}" \
-    "--usergroup[user\:group flag]: :_example_callback '_example__action##usergroup'" \
-    "(-u --users)"{-u,--users}"[users flag]: :_example_callback '_example__action##users'" \
+    "--usergroup[user\:group flag]: :{_example_callback '_example__action##usergroup'}" \
+    "(-u --users)"{-u,--users}"[users flag]: :{_example_callback '_example__action##users'}" \
     "(-v --values)"{-v,--values}"[values flag]: :{local _comp_desc=('values' 'example');compadd -S '' -d _comp_desc 'values' 'example'}" \
     "(-d --values_described)"{-d,--values_described}"[values with description flag]: :{local _comp_desc=('values (valueDescription)' 'example (exampleDescription)');compadd -S '' -d _comp_desc 'values' 'example'}" \
     "1: :{local _comp_desc=('positional1' 'p1');compadd -S '' -d _comp_desc 'positional1' 'p1'}" \
@@ -1308,16 +1308,16 @@ function _example__action {
 
 function _example__callback {
     _arguments -C \
-    "(-c --callback)"{-c,--callback}"[Help message for callback]: :_example_callback '_example__callback##callback'" \
-    "1: :_example_callback '_example__callback#1'" \
-    "2: :_example_callback '_example__callback#2'" \
-    "*: :_example_callback '_example__callback#0'"
+    "(-c --callback)"{-c,--callback}"[Help message for callback]: :{_example_callback '_example__callback##callback'}" \
+    "1: :{_example_callback '_example__callback#1'}" \
+    "2: :{_example_callback '_example__callback#2'}" \
+    "*: :{_example_callback '_example__callback#0'}"
 }
 
 function _example__condition {
     _arguments -C \
     "(-r --required)"{-r,--required}"[required flag]: :{local _comp_desc=('valid' 'invalid');compadd -S '' -d _comp_desc 'valid' 'invalid'}" \
-    "1: :_example_callback '_example__condition#1'"
+    "1: :{_example_callback '_example__condition#1'}"
 }
 
 function _example__help {
@@ -1340,14 +1340,14 @@ function _example__injection {
 
 function _example__multiparts {
     _arguments -C \
-    "--at[multiparts with @ as divider]: :_example_callback '_example__multiparts##at'" \
-    "--colon[multiparts with \: as divider ]: :_example_callback '_example__multiparts##colon'" \
-    "--comma[multiparts with , as divider]: :_example_callback '_example__multiparts##comma'" \
-    "--dot[multiparts with . as divider]: :_example_callback '_example__multiparts##dot'" \
-    "--dotdotdot[multiparts with ... as divider]: :_example_callback '_example__multiparts##dotdotdot'" \
-    "--equals[multiparts with = as divider]: :_example_callback '_example__multiparts##equals'" \
-    "--none[multiparts without divider]: :_example_callback '_example__multiparts##none'" \
-    "--slash[multiparts with / as divider]: :_example_callback '_example__multiparts##slash'" \
+    "--at[multiparts with @ as divider]: :{_example_callback '_example__multiparts##at'}" \
+    "--colon[multiparts with \: as divider ]: :{_example_callback '_example__multiparts##colon'}" \
+    "--comma[multiparts with , as divider]: :{_example_callback '_example__multiparts##comma'}" \
+    "--dot[multiparts with . as divider]: :{_example_callback '_example__multiparts##dot'}" \
+    "--dotdotdot[multiparts with ... as divider]: :{_example_callback '_example__multiparts##dotdotdot'}" \
+    "--equals[multiparts with = as divider]: :{_example_callback '_example__multiparts##equals'}" \
+    "--none[multiparts without divider]: :{_example_callback '_example__multiparts##none'}" \
+    "--slash[multiparts with / as divider]: :{_example_callback '_example__multiparts##slash'}" \
     "*::arg:->args"
 }
 if compquote '' 2>/dev/null; then _example; else compdef _example example; fi