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 <danishdua@google.com>
Run-TryBot: Danish Dua <danishdua@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Danish Dua 2020-09-29 18:30:21 -04:00
parent 66e72d03b2
commit a44386fadb
1 changed files with 16 additions and 0 deletions

View File

@ -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() {