From a44386fadb52a7416da4e6a9f28eb9d5e2e69f63 Mon Sep 17 00:00:00 2001 From: Danish Dua Date: Tue, 29 Sep 2020 18:30:21 -0400 Subject: [PATCH] internal/lsp/source/completion: use c.matcher for comments This change adds calls to c.matcher.score for commment completion so comments match the prefix (or fuzzy match depending on user settings) and don't end up giving the user too many irrelevant suggestions. Change-Id: Ie660f82c491c17d52e68e781a812bf8053e501f8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/258322 Trust: Danish Dua Run-TryBot: Danish Dua gopls-CI: kokoro TryBot-Result: Go Bot Reviewed-by: Heschi Kreinick --- internal/lsp/source/completion/completion.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/internal/lsp/source/completion/completion.go b/internal/lsp/source/completion/completion.go index bd157e6344..71403748b9 100644 --- a/internal/lsp/source/completion/completion.go +++ b/internal/lsp/source/completion/completion.go @@ -859,6 +859,9 @@ func (c *completer) populateCommentCompletions(ctx context.Context, comment *ast continue } obj := c.pkg.GetTypesInfo().ObjectOf(name) + if matchScore := c.matcher.Score(name.String()); matchScore <= 0 { + continue + } c.deepState.enqueue(candidate{obj: obj, score: stdScore}) } case *ast.TypeSpec: @@ -878,6 +881,9 @@ func (c *completer) populateCommentCompletions(ctx context.Context, comment *ast } obj := c.pkg.GetTypesInfo().ObjectOf(spec.Name) + if matchScore := c.matcher.Score(obj.Name()); matchScore <= 0 { + continue + } // Type name should get a higher score than fields but not highScore by default // since field near a comment cursor gets a highScore score := stdScore * 1.1 @@ -924,6 +930,9 @@ func (c *completer) populateCommentCompletions(ctx context.Context, comment *ast // use c.item here to ensure scoring order is // maintained. deepSearch manipulates the score so // we can't enqueue the items directly. + if matchScore := c.matcher.Score(field.Name()); matchScore <= 0 { + continue + } item, err := c.item(ctx, candidate{obj: field, name: field.Name(), score: lowScore}) if err != nil { continue @@ -943,6 +952,9 @@ func (c *completer) populateCommentCompletions(ctx context.Context, comment *ast continue } + if matchScore := c.matcher.Score(obj.Name()); matchScore <= 0 { + continue + } // We don't want to expandFuncCall inside comments. deepSearch // doesn't respect this setting so we don't enqueue the item here. item, err := c.item(ctx, candidate{ @@ -1016,6 +1028,10 @@ func (c *completer) addFieldItems(ctx context.Context, fields *ast.FieldList) { continue } + if matchScore := c.matcher.Score(obj.Name()); matchScore <= 0 { + continue + } + // if we're in a field comment/doc, score that field as more relevant score := stdScore if field.Comment != nil && field.Comment.Pos() <= cursor && cursor <= field.Comment.End() {