Skip to content

Commit ac82a9c

Browse files
committed
Fix list completions
1 parent b38d589 commit ac82a9c

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

example/opts/options.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package opts
22

33
import (
44
"reflect"
5-
"strings"
65

76
"github.com/carapace-sh/carapace"
87
"github.com/carapace-sh/carapace-bin/pkg/actions/net/ssh"
@@ -32,13 +31,18 @@ type Machine string
3231

3332
// Complete provides user@host completions.
3433
func (m *Machine) Complete(ctx carapace.Context) carapace.Action {
35-
if strings.Contains(ctx.Value, "@") {
36-
prefix := strings.SplitN(ctx.Value, "@", 2)[0]
37-
38-
return ssh.ActionHosts().Invoke(ctx).Prefix(prefix + "@").ToA()
39-
} else {
40-
return os.ActionUsers().Suffix("@").NoSpace('@')
41-
}
34+
action := carapace.ActionMultiParts("@", func(c carapace.Context) carapace.Action {
35+
switch len(c.Parts) {
36+
case 0:
37+
return os.ActionUsers().Invoke(ctx).Suffix("@").ToA().NoSpace('@')
38+
case 1:
39+
return ssh.ActionHosts()
40+
default:
41+
return carapace.ActionValues()
42+
}
43+
})
44+
45+
return action
4246
}
4347

4448
func (m *Machine) String() string {

internal/completions/completion.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func getCompletionAction(name, value, desc string) carapace.Action {
9595
// it then checks the slice's element type.
9696
func typeCompleter(val reflect.Value) (carapace.CompletionCallback, bool, bool) {
9797
var callback carapace.CompletionCallback
98-
isRepeatable := (val.Type().Kind() == reflect.Slice)
98+
isRepeatable := val.Type().Kind() == reflect.Slice || val.Type().Kind() == reflect.Map
9999
itemsImplement := false
100100

101101
// Always check that the type itself does implement, even if
@@ -117,7 +117,6 @@ func typeCompleter(val reflect.Value) (carapace.CompletionCallback, bool, bool)
117117
itemsImplement = true
118118
callback = impl.Complete
119119
} else if val.CanAddr() {
120-
isRepeatable = true
121120
if impl, ok := val.Addr().Interface().(interfaces.Completer); ok && impl != nil {
122121
itemsImplement = true
123122
callback = impl.Complete

internal/gen/group.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,12 @@ func buildFlagCompleter(flag *parser.Flag, opts *parser.Opts) (carapace.Action,
170170
// Then, and irrespectively of where the completer comes from,
171171
// we adapt it considering the kind of type we're dealing with.
172172
if isRepeatable {
173-
174173
// List separator
175174
separator := ","
176175
if flag.Separator != nil && *flag.Separator != "none" {
177176
separator = *flag.Separator
178177
}
179178
action = action.UniqueList(separator)
180-
181179
}
182180

183181
return action, true

0 commit comments

Comments
 (0)