Skip to content

Commit

Permalink
fix positional completion for word ending with space
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 24, 2022
1 parent fedca09 commit 5152891
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 13 deletions.
5 changes: 0 additions & 5 deletions carapace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
14 changes: 14 additions & 0 deletions carapace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
2 changes: 1 addition & 1 deletion example/cmd/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
)

Expand Down
16 changes: 9 additions & 7 deletions example/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5152891

Please sign in to comment.