@@ -13,6 +13,7 @@ import (
1313 "strings"
1414
1515 "github.com/c-bata/go-prompt"
16+ "github.com/scaleway/scaleway-cli/v2/internal/cache"
1617 "github.com/scaleway/scaleway-cli/v2/internal/interactive"
1718 "github.com/spf13/cobra"
1819)
@@ -182,37 +183,6 @@ func sortOptions(meta *meta, args []string, toSuggest string, suggestions []stri
182183 return suggests
183184}
184185
185- // CompletionCache allows to keep last completion request in cache
186- // Useful to avoid request spamming when adding a character
187- type CompletionCache struct {
188- wordsSum string
189- arg string
190- LastResponse * AutocompleteResponse
191- }
192-
193- // completionCacheResetCharacterList is the list of character that will trigger cache reset
194- var completionCacheResetCharacterList = []string {"=" , "." }
195- var completionCache CompletionCache
196-
197- func (cache * CompletionCache ) HasChanged (leftWords []string , currentArg string , rightWords []string ) bool {
198- wordsSum := strings .Join (leftWords , "-" ) + "_" + strings .Join (rightWords , "-" )
199- if cache .wordsSum != wordsSum {
200- cache .wordsSum = wordsSum
201- cache .arg = currentArg
202- return true
203- }
204-
205- for _ , character := range completionCacheResetCharacterList {
206- if strings .Count (cache .arg , character ) != strings .Count (currentArg , character ) {
207- cache .arg = currentArg
208- return true
209- }
210- }
211-
212- cache .arg = currentArg
213- return false
214- }
215-
216186// Complete returns the list of suggestion based on prompt content
217187func (c * Completer ) Complete (d prompt.Document ) []prompt.Suggest {
218188 // shell lib can request duplicate Complete request with empty strings as text
@@ -234,17 +204,9 @@ func (c *Completer) Complete(d prompt.Document) []prompt.Suggest {
234204
235205 leftWords := append ([]string {"scw" }, leftArgs ... )
236206
237- var acr * AutocompleteResponse
238-
239- if completionCache .HasChanged (leftWords , currentArg , rightWords ) {
240- acr = AutoComplete (c .ctx , leftWords , currentArg , rightWords )
241- completionCache .LastResponse = acr
242- } else {
243- acr = completionCache .LastResponse
244- }
207+ acr := AutoComplete (c .ctx , leftWords , currentArg , rightWords )
245208
246209 suggestions := []prompt.Suggest (nil )
247-
248210 rawSuggestions := []string (acr .Suggestions )
249211
250212 // if first suggestion is an option, all suggestions should be options
@@ -289,6 +251,8 @@ func shellExecutor(rootCmd *cobra.Command, printer *Printer, meta *meta) func(s
289251 return
290252 }
291253
254+ autoCompleteCache .Update (meta .command .Namespace )
255+
292256 printErr := printer .Print (meta .result , meta .command .getHumanMarshalerOpt ())
293257 if printErr != nil {
294258 _ , _ = fmt .Fprintln (os .Stderr , printErr )
@@ -309,6 +273,7 @@ func getShellCommand(rootCmd *cobra.Command) *cobra.Command {
309273
310274// RunShell will run an interactive shell that runs cobra commands
311275func RunShell (ctx context.Context , printer * Printer , meta * meta , rootCmd * cobra.Command , args []string ) {
276+ autoCompleteCache = cache .New ()
312277 completer := NewShellCompleter (ctx )
313278
314279 shellCobraCommand := getShellCommand (rootCmd )
0 commit comments