Skip to content

Commit 51244e8

Browse files
authored
Fix auto-completion logic for APIs that end with 'y' (#194)
* Fix auto-completion logic for APIs that end with 'y' * xtract shared pluralizeNoun helper for autocomplete heuristics * omit already plural apis
1 parent fad5ae6 commit 51244e8

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

‎cli/completer.go‎

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,22 @@ func findAPI(apiMap map[string][]*config.API, relatedNoun string) *config.API {
208208
return autocompleteAPI
209209
}
210210

211+
// pluralizeNoun applies simple English pluralization rules used by the
212+
// autocomplete heuristics below (e.g., policy -> policies, disk -> disks).
213+
func pluralizeNoun(noun string) string {
214+
switch {
215+
case strings.HasSuffix(noun, "ies"):
216+
return noun
217+
case strings.HasSuffix(noun, "y") && len(noun) > 1 && !strings.ContainsAny(string(noun[len(noun)-2]), "aeiou"):
218+
// Handle words ending in consonant + y (e.g., policy -> policies)
219+
return noun[:len(noun)-1] + "ies"
220+
case strings.HasSuffix(noun, "s") || strings.HasSuffix(noun, "x") || strings.HasSuffix(noun, "z") || strings.HasSuffix(noun, "ch") || strings.HasSuffix(noun, "sh"):
221+
return noun + "es"
222+
default:
223+
return noun + "s"
224+
}
225+
}
226+
211227
func findAutocompleteAPI(arg *config.APIArg, apiFound *config.API, apiMap map[string][]*config.API) *config.API {
212228
if arg.Type == "map" {
213229
return nil
@@ -220,8 +236,9 @@ func findAutocompleteAPI(arg *config.APIArg, apiFound *config.API, apiMap map[st
220236
case argName == "id" || argName == "ids":
221237
// Heuristic: user is trying to autocomplete for id/ids arg for a list API
222238
relatedNoun = apiFound.Noun
223-
if apiFound.Verb != "list" {
224-
relatedNoun += "s"
239+
if apiFound.Verb != "list" && findAPI(apiMap, relatedNoun) == nil {
240+
// Noun may already be plural (e.g. bulk ops like deleteAlerts)
241+
relatedNoun = pluralizeNoun(relatedNoun)
225242
}
226243
case argName == "account":
227244
// Heuristic: user is trying to autocomplete for accounts
@@ -249,12 +266,7 @@ func findAutocompleteAPI(arg *config.APIArg, apiFound *config.API, apiMap map[st
249266
}
250267
}
251268
}
252-
// Handle common cases where base ends with a vowel and needs "es"
253-
if strings.HasSuffix(base, "s") || strings.HasSuffix(base, "x") || strings.HasSuffix(base, "z") || strings.HasSuffix(base, "ch") || strings.HasSuffix(base, "sh") {
254-
relatedNoun = base + "es"
255-
} else {
256-
relatedNoun = base + "s"
257-
}
269+
relatedNoun = pluralizeNoun(base)
258270
}
259271

260272
config.Debug("Possible related noun for the arg: ", relatedNoun, " and type: ", arg.Type)

0 commit comments

Comments
 (0)