Restore comparability of completion metadata

This commit is contained in:
2026-11-03 13:12:22 +00:00
parent b83c0a0dfc
commit f79f446726
3 changed files with 26 additions and 4 deletions

View File

@@ -61,7 +61,12 @@ type CompletionChoice struct {
// value requests no value suggestions; hints do not constrain accepted input.
type CompletionHint struct {
kind completionHintKind
choices []CompletionChoice
choices *completionChoicesData
}
// Keep the public metadata structs comparable without exposing mutable choices.
type completionChoicesData struct {
values []CompletionChoice
}
type completionHintKind uint8
@@ -78,7 +83,11 @@ const (
// values and values containing a newline, tab, or NUL cannot be presented as
// suggestions, but remain valid input if the command's converter accepts them.
func CompleteChoices(choices ...CompletionChoice) CompletionHint {
return CompletionHint{kind: completionChoices, choices: append([]CompletionChoice(nil), choices...)}
hint := CompletionHint{kind: completionChoices}
if len(choices) != 0 {
hint.choices = &completionChoicesData{values: append([]CompletionChoice(nil), choices...)}
}
return hint
}
// CompleteFiles requests shell filename completion.
@@ -95,7 +104,10 @@ func bridgeCompletion(hint CompletionHint) commandmeta.CompletionHint {
switch hint.kind {
case completionChoices:
result.Kind = commandmeta.CompletionChoices
for _, choice := range hint.choices {
if hint.choices == nil {
break
}
for _, choice := range hint.choices.values {
result.Choices = append(result.Choices, commandmeta.CompletionChoice{Value: choice.Value, Description: choice.Description})
}
case completionFiles: