Skip to content

Commit

Permalink
Merge pull request #156 from haibeey/master
Browse files Browse the repository at this point in the history
fuzzy completion matching for metric names
  • Loading branch information
slrtbtfs authored May 28, 2020
2 parents 009670a + d17f613 commit b2be2c0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/prometheus/common v0.10.0
github.com/prometheus/prometheus v1.8.2-0.20200507164740-ecee9c8abfd1
github.com/rakyll/statik v0.1.7
github.com/sahilm/fuzzy v0.1.0
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kylelemons/godebug v0.0.0-20160406211939-eadb3ce320cb h1:iiMILPl9HQFqdFXIuwfYT73NYtH0KApnCmyF7y5wYhs=
github.com/kylelemons/godebug v0.0.0-20160406211939-eadb3ce320cb/go.mod h1:B69LEHPfb2qLo0BaaOLcbitczOKLWTsrBG9LczfCD4k=
github.com/lib/pq v1.0.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
github.com/lightstep/lightstep-tracer-common/golang/gogo v0.0.0-20190605223551-bc2310a04743/go.mod h1:qklhhLq1aX+mtWk9cPHPzaBjWImj5ULL6C7HFJtXQMM=
Expand Down Expand Up @@ -632,6 +633,8 @@ github.com/rs/cors v1.6.0/go.mod h1:gFx+x8UowdsKA9AchylcLynDq+nNFfI8FkUZdN/jGCU=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/sahilm/fuzzy v0.1.0 h1:FzWGaw2Opqyu+794ZQ9SYifWv2EIXpwP4q8dY1kDAwI=
github.com/sahilm/fuzzy v0.1.0/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da h1:p3Vo3i64TCLY7gIfzeQaUJ+kppEO5WQG3cL8iE8tGHU=
github.com/samuel/go-zookeeper v0.0.0-20190923202752-2cc03de413da/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
github.com/satori/go.uuid v0.0.0-20160603004225-b111a074d5ef/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
Expand Down
34 changes: 20 additions & 14 deletions langserver/completion.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/prometheus-community/promql-langserver/langserver/cache"
promql "github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/util/strutil"
"github.com/sahilm/fuzzy"
)

// Completion is required by the protocol.Server interface.
Expand Down Expand Up @@ -102,21 +103,26 @@ func (s *server) completeMetricName(ctx context.Context, completions *[]protocol
return err
}

for name, metadata := range allMetadata {
if strings.HasPrefix(name, metricName) {
item := protocol.CompletionItem{
Label: name,
SortText: "__3__" + name,
Kind: 12, //Value
Documentation: metadata[0].Help,
Detail: string(metadata[0].Type),
TextEdit: &protocol.TextEdit{
Range: editRange,
NewText: name,
},
}
*completions = append(*completions, item)
names := make([]string, len(allMetadata))
i := 0
for name := range allMetadata {
names[i] = name
i++
}
matches := fuzzy.Find(metricName, names)
for _, match := range matches {
item := protocol.CompletionItem{
Label: match.Str,
SortText: fmt.Sprintf("__3__%d", match.Score),
Kind: 12, //Value
Documentation: allMetadata[match.Str][0].Help,
Detail: string(allMetadata[match.Str][0].Type),
TextEdit: &protocol.TextEdit{
Range: editRange,
NewText: match.Str,
},
}
*completions = append(*completions, item)
}

queries, err := location.Doc.GetQueries()
Expand Down

0 comments on commit b2be2c0

Please sign in to comment.