internal/lsp: use prefix matcher with comment completion

Since we used to manually set surrounding for comments (instead of using
setSurrounding with an ident), the matched was never initialized and
hence we had to manually do prefix matching. We now initialize matcher
from setSurroundingForComment too.

Change-Id: I8aa735933ebba2fe493182e4245de668997ef7af
Reviewed-on: https://go-review.googlesource.com/c/tools/+/249707
Run-TryBot: Danish Dua <danishdua@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go
index 36380db..3c0ad9f 100644
--- a/internal/lsp/source/completion.go
+++ b/internal/lsp/source/completion.go
@@ -264,13 +264,17 @@
 		mappedRange: newMappedRange(c.snapshot.FileSet(), c.mapper, ident.Pos(), ident.End()),
 	}
 
+	c.setMatcherFromPrefix(c.surrounding.Prefix())
+}
+
+func (c *completer) setMatcherFromPrefix(prefix string) {
 	switch c.opts.matcher {
 	case Fuzzy:
-		c.matcher = fuzzy.NewMatcher(c.surrounding.Prefix())
+		c.matcher = fuzzy.NewMatcher(prefix)
 	case CaseSensitive:
-		c.matcher = prefixMatcher(c.surrounding.Prefix())
+		c.matcher = prefixMatcher(prefix)
 	default:
-		c.matcher = insensitivePrefixMatcher(strings.ToLower(c.surrounding.Prefix()))
+		c.matcher = insensitivePrefixMatcher(strings.ToLower(prefix))
 	}
 }
 
@@ -736,7 +740,6 @@
 
 	// comment is valid, set surrounding as word boundaries around cursor
 	c.setSurroundingForComment(comment)
-	cursorText := c.surrounding.content
 
 	// Using the next line pos, grab and parse the exported symbol on that line
 	for _, n := range c.file.Decls {
@@ -753,7 +756,7 @@
 				switch spec := spec.(type) {
 				case *ast.ValueSpec:
 					for _, name := range spec.Names {
-						if name.String() == "_" || !strings.HasPrefix(name.String(), cursorText) {
+						if name.String() == "_" {
 							continue
 						}
 						obj := c.pkg.GetTypesInfo().ObjectOf(name)
@@ -771,7 +774,7 @@
 						c.addFieldItems(ctx, typeNode.Methods)
 					}
 
-					if spec.Name.String() == "_" || !strings.HasPrefix(spec.Name.String(), cursorText) {
+					if spec.Name.String() == "_" {
 						continue
 					}
 
@@ -816,9 +819,6 @@
 						}
 						for i := 0; i < recvStruct.NumFields(); i++ {
 							field := recvStruct.Field(i)
-							if !strings.HasPrefix(field.Name(), cursorText) {
-								continue
-							}
 							// we use c.item in addFieldItems so we have to use c.item here to ensure scoring
 							// order is maintained. c.found maniplulates the score
 							item, err := c.item(ctx, candidate{obj: field, name: field.Name(), score: lowScore})
@@ -831,7 +831,7 @@
 				}
 			}
 
-			if node.Name.String() == "_" || !strings.HasPrefix(node.Name.String(), cursorText) {
+			if node.Name.String() == "_" {
 				continue
 			}
 
@@ -886,6 +886,7 @@
 		mappedRange: newMappedRange(c.snapshot.FileSet(), c.mapper,
 			token.Pos(int(cursorComment.Slash)+start), token.Pos(int(cursorComment.Slash)+end)),
 	}
+	c.setMatcherFromPrefix(c.surrounding.Prefix())
 }
 
 // isValidIdentifierChar returns true if a byte is a valid go identifier character
@@ -902,11 +903,9 @@
 	}
 
 	cursor := c.surrounding.cursor
-	surroundingPrefix := c.surrounding.content
 	for _, field := range fields.List {
 		for _, name := range field.Names {
-			if name.String() == "_" ||
-				!strings.HasPrefix(name.String(), surroundingPrefix) {
+			if name.String() == "_" {
 				continue
 			}
 			obj := c.pkg.GetTypesInfo().ObjectOf(name)