internal/lsp: do not complete inside comments in functions
The previous change (https://go-review.googlesource.com/c/tools/+/157678) only stopped completion in comments in global scope. This change prevents completions results from being sent for comments inside of functions.
Fixes golang/go#29370
Change-Id: I2b43ae2942c6ce7376d2a5f88c40e6ac45c2b773
GitHub-Last-Rev: bc4aac1370aa5758941cdfae63290f061a55e204
GitHub-Pull-Request: golang/tools#71
Reviewed-on: https://go-review.googlesource.com/c/158538
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go
index 6df0f57..21cb4b5 100644
--- a/internal/lsp/source/completion.go
+++ b/internal/lsp/source/completion.go
@@ -73,8 +73,11 @@
}
// Skip completion inside comment blocks.
- if p, ok := path[0].(*ast.File); ok && isCommentNode(p, pos) {
- return items, prefix, nil
+ switch path[0].(type) {
+ case *ast.File, *ast.BlockStmt:
+ if inComment(pos, file.Comments) {
+ return items, prefix, nil
+ }
}
// Save certain facts about the query position, including the expected type
@@ -252,22 +255,16 @@
return items
}
-// isCommentNode checks if given token position is inside ast.Comment node.
-func isCommentNode(root ast.Node, pos token.Pos) bool {
- var found bool
- ast.Inspect(root, func(n ast.Node) bool {
- if n == nil {
- return false
- }
- if n.Pos() <= pos && pos <= n.End() {
- if _, ok := n.(*ast.Comment); ok {
- found = true
- return false
+// inComment checks if given token position is inside ast.Comment node.
+func inComment(pos token.Pos, commentGroups []*ast.CommentGroup) bool {
+ for _, g := range commentGroups {
+ for _, c := range g.List {
+ if c.Pos() <= pos && pos <= c.End() {
+ return true
}
}
- return true
- })
- return found
+ }
+ return false
}
// complit finds completions for field names inside a composite literal.
diff --git a/internal/lsp/testdata/foo/foo.go b/internal/lsp/testdata/foo/foo.go
index eb06d32..acd6cd1 100644
--- a/internal/lsp/testdata/foo/foo.go
+++ b/internal/lsp/testdata/foo/foo.go
@@ -19,6 +19,4 @@
}
}
-//@complete("", Foo, IntFoo, StructFoo)
-
-type IntFoo int //@item(IntFoo, "IntFoo", "int", "type")
+type IntFoo int //@item(IntFoo, "IntFoo", "int", "type"),complete("", Foo, IntFoo, StructFoo)