diff --git a/example/opts/options.go b/example/opts/options.go index a515848..bebd882 100644 --- a/example/opts/options.go +++ b/example/opts/options.go @@ -2,7 +2,6 @@ package opts import ( "reflect" - "strings" "github.com/carapace-sh/carapace" "github.com/carapace-sh/carapace-bin/pkg/actions/net/ssh" @@ -32,13 +31,18 @@ type Machine string // Complete provides user@host completions. func (m *Machine) Complete(ctx carapace.Context) carapace.Action { - if strings.Contains(ctx.Value, "@") { - prefix := strings.SplitN(ctx.Value, "@", 2)[0] - - return ssh.ActionHosts().Invoke(ctx).Prefix(prefix + "@").ToA() - } else { - return os.ActionUsers().Suffix("@").NoSpace('@') - } + action := carapace.ActionMultiParts("@", func(c carapace.Context) carapace.Action { + switch len(c.Parts) { + case 0: + return os.ActionUsers().Invoke(ctx).Suffix("@").ToA().NoSpace('@') + case 1: + return ssh.ActionHosts() + default: + return carapace.ActionValues() + } + }) + + return action } func (m *Machine) String() string { diff --git a/internal/completions/completion.go b/internal/completions/completion.go index ab6bc80..4c6e707 100644 --- a/internal/completions/completion.go +++ b/internal/completions/completion.go @@ -95,7 +95,7 @@ func getCompletionAction(name, value, desc string) carapace.Action { // it then checks the slice's element type. func typeCompleter(val reflect.Value) (carapace.CompletionCallback, bool, bool) { var callback carapace.CompletionCallback - isRepeatable := (val.Type().Kind() == reflect.Slice) + isRepeatable := val.Type().Kind() == reflect.Slice || val.Type().Kind() == reflect.Map itemsImplement := false // Always check that the type itself does implement, even if @@ -117,7 +117,6 @@ func typeCompleter(val reflect.Value) (carapace.CompletionCallback, bool, bool) itemsImplement = true callback = impl.Complete } else if val.CanAddr() { - isRepeatable = true if impl, ok := val.Addr().Interface().(interfaces.Completer); ok && impl != nil { itemsImplement = true callback = impl.Complete diff --git a/internal/gen/group.go b/internal/gen/group.go index cfb13a0..5c317d2 100644 --- a/internal/gen/group.go +++ b/internal/gen/group.go @@ -170,14 +170,12 @@ func buildFlagCompleter(flag *parser.Flag, opts *parser.Opts) (carapace.Action, // Then, and irrespectively of where the completer comes from, // we adapt it considering the kind of type we're dealing with. if isRepeatable { - // List separator separator := "," if flag.Separator != nil && *flag.Separator != "none" { separator = *flag.Separator } action = action.UniqueList(separator) - } return action, true