Repository: rsteube/carapace
Pull request #106 (2020-11-10 20:02:35)

fix callbackvalue for positionalany


Patch:
diff --git a/elvish/snippet.go b/elvish/snippet.go
index 037243c..fd77dd0 100644
--- a/elvish/snippet.go
+++ b/elvish/snippet.go
@@ -38,6 +38,9 @@ edit:completion:arg-completer[%v] = [@arg]{
   fn _%v_callback [uid]{
     # TODO there is no 'eval' in elvish and '-source' needs a file so use a tempary one for callback 
     tmpfile=(mktemp -t carapace_%v_callback-XXXXX.elv)
+    if (eq $arg[-1] "") {
+        arg[-1] = "''"
+    }
     echo (str:join ' ' $arg) | xargs %v _carapace elvish $uid > $tmpfile
     -source $tmpfile
     rm $tmpfile
diff --git a/example/cmd/multiparts.go b/example/cmd/multiparts.go
index 560a08d..f49e9a1 100644
--- a/example/cmd/multiparts.go
+++ b/example/cmd/multiparts.go
@@ -36,6 +36,10 @@ func init() {
 			return carapace.ActionValuesDescribed("a", "first", "b", "second", "c", "third", "d", "fourth").Invoke(args).Filter(strings.Split(carapace.CallbackValue, "")).ToA()
 		}),
 	})
+
+	carapace.Gen(multipartsCmd).PositionalCompletion(ActionMultipartsTest(","))
+
+	carapace.Gen(multipartsCmd).PositionalAnyCompletion(ActionMultipartsTest("/"))
 }
 
 func ActionMultipartsTest(divider string) carapace.Action {
diff --git a/uid/uid.go b/uid/uid.go
index cbf9b62..7cece8b 100644
--- a/uid/uid.go
+++ b/uid/uid.go
@@ -68,9 +68,13 @@ func Value(cmd *cobra.Command, args []string, uid string) string {
 	} else if strings.Contains(uid, "#") {
 		split := strings.Split(uid, "#")
 		if index, err := strconv.Atoi(split[len(split)-1]); err == nil {
-			index = index - 1
-			if len(args)-1 >= index && index >= 0 {
-				return args[index]
+			if index > 0 {
+				index = index - 1
+				if len(args)-1 >= index && index >= 0 {
+					return args[index]
+				}
+			} else if len(args) > 0 && os.Args[len(os.Args)-1] != "" {
+				return args[len(args)-1]
 			}
 		}
 	}


Test patch:
diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go
index d1a3b62..d7382ea 100644
--- a/example/cmd/root_test.go
+++ b/example/cmd/root_test.go
@@ -286,6 +286,9 @@ edit:completion:arg-completer[example] = [@arg]{
   fn _example_callback [uid]{
     # TODO there is no 'eval' in elvish and '-source' needs a file so use a tempary one for callback 
     tmpfile=(mktemp -t carapace_example_callback-XXXXX.elv)
+    if (eq $arg[-1] "") {
+        arg[-1] = "''"
+    }
     echo (str:join ' ' $arg) | xargs example _carapace elvish $uid > $tmpfile
     -source $tmpfile
     rm $tmpfile
@@ -449,7 +452,9 @@ edit:complex-candidate 'invalid' &display='invalid' }]
         [&long='slash' &desc='multiparts with / as divider' &arg-required=$true &completer=[_]{ _example_callback '_example__multiparts##slash' }]
     ]
     arg-handlers = [
-
+      [_]{ _example_callback '_example__multiparts#1' }
+      [_]{ _example_callback '_example__multiparts#0' }
+      ...
     ]
     subargs = $arg[(subindex multiparts):] 
     if (> (count $subargs) 0) {
@@ -1348,7 +1353,8 @@ function _example__multiparts {
     "--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"
+    "1: :{_example_callback '_example__multiparts#1'}" \
+    "*: :{_example_callback '_example__multiparts#0'}"
 }
 if compquote '' 2>/dev/null; then _example; else compdef _example example; fi
 `