@@ -27,6 +27,7 @@ import (
2727 "github.com/prometheus-community/promql-langserver/langserver/cache"
2828 promql "github.com/prometheus/prometheus/promql/parser"
2929 "github.com/prometheus/prometheus/util/strutil"
30+ "github.com/sahilm/fuzzy"
3031)
3132
3233// Completion is required by the protocol.Server interface.
@@ -102,21 +103,26 @@ func (s *server) completeMetricName(ctx context.Context, completions *[]protocol
102103 return err
103104 }
104105
105- for name , metadata := range allMetadata {
106- if strings .HasPrefix (name , metricName ) {
107- item := protocol.CompletionItem {
108- Label : name ,
109- SortText : "__3__" + name ,
110- Kind : 12 , //Value
111- Documentation : metadata [0 ].Help ,
112- Detail : string (metadata [0 ].Type ),
113- TextEdit : & protocol.TextEdit {
114- Range : editRange ,
115- NewText : name ,
116- },
117- }
118- * completions = append (* completions , item )
106+ names := make ([]string , len (allMetadata ))
107+ i := 0
108+ for name := range allMetadata {
109+ names [i ] = name
110+ i ++
111+ }
112+ matches := fuzzy .Find (metricName , names )
113+ for _ , match := range matches {
114+ item := protocol.CompletionItem {
115+ Label : match .Str ,
116+ SortText : fmt .Sprintf ("__3__%d" , match .Score ),
117+ Kind : 12 , //Value
118+ Documentation : allMetadata [match .Str ][0 ].Help ,
119+ Detail : string (allMetadata [match .Str ][0 ].Type ),
120+ TextEdit : & protocol.TextEdit {
121+ Range : editRange ,
122+ NewText : match .Str ,
123+ },
119124 }
125+ * completions = append (* completions , item )
120126 }
121127
122128 queries , err := location .Doc .GetQueries ()
0 commit comments