@@ -13,6 +13,7 @@ import (
13
13
"strings"
14
14
15
15
"github.com/c-bata/go-prompt"
16
+ "github.com/scaleway/scaleway-cli/v2/internal/cache"
16
17
"github.com/scaleway/scaleway-cli/v2/internal/interactive"
17
18
"github.com/spf13/cobra"
18
19
)
@@ -182,37 +183,6 @@ func sortOptions(meta *meta, args []string, toSuggest string, suggestions []stri
182
183
return suggests
183
184
}
184
185
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
-
216
186
// Complete returns the list of suggestion based on prompt content
217
187
func (c * Completer ) Complete (d prompt.Document ) []prompt.Suggest {
218
188
// 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 {
234
204
235
205
leftWords := append ([]string {"scw" }, leftArgs ... )
236
206
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 )
245
208
246
209
suggestions := []prompt.Suggest (nil )
247
-
248
210
rawSuggestions := []string (acr .Suggestions )
249
211
250
212
// 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
289
251
return
290
252
}
291
253
254
+ autoCompleteCache .Update (meta .command .Namespace )
255
+
292
256
printErr := printer .Print (meta .result , meta .command .getHumanMarshalerOpt ())
293
257
if printErr != nil {
294
258
_ , _ = fmt .Fprintln (os .Stderr , printErr )
@@ -309,6 +273,7 @@ func getShellCommand(rootCmd *cobra.Command) *cobra.Command {
309
273
310
274
// RunShell will run an interactive shell that runs cobra commands
311
275
func RunShell (ctx context.Context , printer * Printer , meta * meta , rootCmd * cobra.Command , args []string ) {
276
+ autoCompleteCache = cache .New ()
312
277
completer := NewShellCompleter (ctx )
313
278
314
279
shellCobraCommand := getShellCommand (rootCmd )
0 commit comments