MCPcopy Index your code
hub / github.com/git-bug/git-bug / BugAndLabelsCompletion

Function BugAndLabelsCompletion

commands/bug/completion.go:44–98  ·  view source on GitHub ↗

BugAndLabelsCompletion complete either a bug ID or a label if we know about the bug

(env *execenv.Env, addOrRemove bool)

Source from the content-addressed store, hash-verified

42
43// BugAndLabelsCompletion complete either a bug ID or a label if we know about the bug
44func BugAndLabelsCompletion(env *execenv.Env, addOrRemove bool) completion.ValidArgsFunction {
45 return func(cmd *cobra.Command, args []string, toComplete string) (completions []string, directives cobra.ShellCompDirective) {
46 if err := execenv.LoadBackend(env)(cmd, args); err != nil {
47 return completion.HandleError(err)
48 }
49 defer func() {
50 _ = env.Backend.Close()
51 }()
52
53 b, cleanArgs, err := ResolveSelected(env.Backend, args)
54 if _select.IsErrNoValidId(err) {
55 // we need a bug first to complete labels
56 return bugWithBackend(env.Backend, toComplete)
57 }
58 if err != nil {
59 return completion.HandleError(err)
60 }
61
62 snap := b.Snapshot()
63
64 seenLabels := map[common.Label]bool{}
65 for _, label := range cleanArgs {
66 seenLabels[common.Label(label)] = addOrRemove
67 }
68
69 var labels []common.Label
70 if addOrRemove {
71 for _, label := range snap.Labels {
72 seenLabels[label] = true
73 }
74
75 allLabels := env.Backend.Bugs().ValidLabels()
76 labels = make([]common.Label, 0, len(allLabels))
77 for _, label := range allLabels {
78 if !seenLabels[label] {
79 labels = append(labels, label)
80 }
81 }
82 } else {
83 labels = make([]common.Label, 0, len(snap.Labels))
84 for _, label := range snap.Labels {
85 if seenLabels[label] {
86 labels = append(labels, label)
87 }
88 }
89 }
90
91 completions = make([]string, len(labels))
92 for i, label := range labels {
93 completions[i] = string(label) + "\t" + "Label"
94 }
95
96 return completions, cobra.ShellCompDirectiveNoFileComp
97 }
98}

Callers 2

newBugLabelNewCommandFunction · 0.85
newBugLabelRmCommandFunction · 0.85

Calls 9

LoadBackendFunction · 0.92
HandleErrorFunction · 0.92
LabelTypeAlias · 0.92
ResolveSelectedFunction · 0.85
bugWithBackendFunction · 0.85
SnapshotMethod · 0.80
BugsMethod · 0.80
CloseMethod · 0.65
ValidLabelsMethod · 0.65

Tested by

no test coverage detected