Skip to content

Commit b2be2c0

Browse files
authored
Merge pull request #156 from haibeey/master
fuzzy completion matching for metric names
2 parents 009670a + d17f613 commit b2be2c0

File tree

3 files changed

+24
-14
lines changed

3 files changed

+24
-14
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
github.com/prometheus/common v0.10.0
1313
github.com/prometheus/prometheus v1.8.2-0.20200507164740-ecee9c8abfd1
1414
github.com/rakyll/statik v0.1.7
15+
github.com/sahilm/fuzzy v0.1.0
1516
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
1617
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
1718
)

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,6 +463,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
463463
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
464464
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
465465
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
466+
github.com/kylelemons/godebug v0.0.0-20160406211939-eadb3ce320cb h1:iiMILPl9HQFqdFXIuwfYT73NYtH0KApnCmyF7y5wYhs=
466467
github.com/kylelemons/godebug v0.0.0-20160406211939-eadb3ce320cb/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
467468
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
468469
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
@@ -632,6 +633,8 @@ github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
632633
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
633634
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
634635
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
636+
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
637+
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
635638
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da h1:p3Vo3i64TCLY7gIfzeQaUJ+kppEO5WQG3cL8iE8tGHU=
636639
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
637640
github.com/satori/go.uuid v0.0.0-20160603004225-b111a074d5ef/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=

langserver/completion.go

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)