From d17f61321e3a2e4f8bdbbf9d6d10653e910ed074 Mon Sep 17 00:00:00 2001 From: akerele abraham Date: Thu, 28 May 2020 15:09:35 +0100 Subject: [PATCH] fuzzy completion matching Signed-off-by: akerele abraham --- go.mod | 1 + go.sum | 3 +++ langserver/completion.go | 34 ++++++++++++++++++++-------------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 6f6eba79..0d02cb28 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 9a31c928..721a5c0c 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/langserver/completion.go b/langserver/completion.go index ef60ff63..109c0ed4 100644 --- a/langserver/completion.go +++ b/langserver/completion.go @@ -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. @@ -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()