@@ -22,17 +22,19 @@ type Suggestion struct {
2222
2323// Completer is an auto-complete helper for the shell
2424type Completer struct {
25- schema resources.ResourcesSchema
26- features cnquery.Features
27- connectedProviders [] string
25+ schema resources.ResourcesSchema
26+ features cnquery.Features
27+ sortFn func ( a , b * llx. Documentation ) int
2828}
2929
3030// NewCompleter creates a new Mondoo completer object
3131func NewCompleter (schema resources.ResourcesSchema , features cnquery.Features , connectedProviders []string ) * Completer {
32+ sortFn := byProviderSortFn (connectedProviders )
33+
3234 return & Completer {
33- schema : schema ,
34- features : features ,
35- connectedProviders : connectedProviders ,
35+ schema : schema ,
36+ features : features ,
37+ sortFn : sortFn ,
3638 }
3739}
3840
@@ -61,16 +63,7 @@ func (c *Completer) Complete(text string) []Suggestion {
6163 bundle , _ := mqlc .Compile (text , nil , mqlc .NewConfig (c .schema , c .features ))
6264 if bundle != nil && len (bundle .Suggestions ) > 0 {
6365 // reorder suggestions to put the ones from connected providers first
64- slices .SortFunc (bundle .Suggestions , func (a , b * llx.Documentation ) int {
65- aConnected := stringx .Contains (c .connectedProviders , a .Provider )
66- bConnected := stringx .Contains (c .connectedProviders , b .Provider )
67- if aConnected && ! bConnected {
68- return - 1
69- } else if ! aConnected && bConnected {
70- return 1
71- }
72- return 0
73- })
66+ slices .SortFunc (bundle .Suggestions , c .sortFn )
7467
7568 // add suggestions from the compiler
7669 for i := range bundle .Suggestions {
@@ -84,3 +77,16 @@ func (c *Completer) Complete(text string) []Suggestion {
8477
8578 return suggestions
8679}
80+
81+ func byProviderSortFn (connectedProviders []string ) func (a , b * llx.Documentation ) int {
82+ return func (a , b * llx.Documentation ) int {
83+ aConnected := stringx .Contains (connectedProviders , a .Provider )
84+ bConnected := stringx .Contains (connectedProviders , b .Provider )
85+ if aConnected && ! bConnected {
86+ return - 1
87+ } else if ! aConnected && bConnected {
88+ return 1
89+ }
90+ return 0
91+ }
92+ }
0 commit comments