Repository: rsteube/carapace (go)

Issue:
positional completion for word containing space fails
```sh
# 'positional with space'
example action 'positional with <TAB>
```


Pull request description:
fix positional completion for word ending with space
fix #566

Patch:
diff --git a/carapace.go b/carapace.go
index 9d2958a..1ef598c 100644
--- a/carapace.go
+++ b/carapace.go
@@ -341,11 +341,6 @@ func findAction(targetCmd *cobra.Command, targetArgs []string) Action {
 	if len(targetArgs) == 0 {
 		return storage.getPositional(targetCmd, 0)
 	}
-
-	lastArg := targetArgs[len(targetArgs)-1]
-	if strings.HasSuffix(lastArg, " ") { // TODO is this still correct/needed?
-		return storage.getPositional(targetCmd, len(targetArgs))
-	}
 	return storage.getPositional(targetCmd, len(targetArgs)-1)
 }
 
diff --git a/example/cmd/action.go b/example/cmd/action.go
index 3e32e51..461c7f9 100644
--- a/example/cmd/action.go
+++ b/example/cmd/action.go
@@ -99,7 +99,7 @@ func init() {
 	})
 
 	carapace.Gen(actionCmd).PositionalCompletion(
-		carapace.ActionValues("positional1", "p1"),
+		carapace.ActionValues("positional1", "p1", "positional1 with space"),
 		carapace.ActionValues("positional2", "p2"),
 	)
 


Test patch:
diff --git a/carapace_test.go b/carapace_test.go
index ce03b6a..9f779ae 100644
--- a/carapace_test.go
+++ b/carapace_test.go
@@ -270,3 +270,17 @@ func TestCompleteSnippet(t *testing.T) {
 		t.Error(s)
 	}
 }
+
+func TestCompletePositionalWithSpace(t *testing.T) {
+	cmd := &cobra.Command{
+		Use: "test",
+	}
+
+	Gen(cmd).PositionalCompletion(
+		ActionValues("positional with space"),
+	)
+
+	if s, err := complete(cmd, []string{"elvish", "_", "positional "}); err != nil || s != `[{"Value":"positional with space","Display":"positional with space","Description":"","DescriptionStyle":"dim white","CodeSuffix":" ","Style":"default"}]` {
+		t.Error(s)
+	}
+}
diff --git a/example/main_test.go b/example/main_test.go
index 6c3426c..a31b2f6 100644
--- a/example/main_test.go
+++ b/example/main_test.go
@@ -59,13 +59,15 @@ func doComplete(t *testing.T, shell string, cmdline string, contained ...string)
 }
 
 var tests = map[string]string{
-	`example -ap `:              "action",
-	`example action `:           "p",
-	`example action -z`:         "unknown",
-	`example action --fail `:    "unknown",
-	`example --fail acti`:       "unknown",
-	`example -z acti`:           "unknown",
-	`example action positional`: "positional1",
+	`example -ap `:               "action",
+	`example action `:            "p",
+	`example action -z`:          "unknown",
+	`example action --fail `:     "unknown",
+	`example --fail acti`:        "unknown",
+	`example -z acti`:            "unknown",
+	`example action positional`:  "positional1",
+	`example action positional1`: "positional1 with space",
+	//`example action "positional1 `: "positional1 with space", // TODO this test does not yet work with bash as it's missing quote handling in the snippet
 	//`example action --`:                                            "--values_described", // weird: causes regex match in expect/xonsh not to work
 	`example action --optarg `:           "p",
 	`example action --optarg positional`: "positional1",