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>
This commit is contained in:
Andzej Maciusovic 2019-01-19 13:54:24 +00:00 committed by Rebecca Stambler
parent b2f1847864
commit 2d2e1b1749
2 changed files with 14 additions and 19 deletions

View File

@ -73,8 +73,11 @@ func Completion(ctx context.Context, f File, pos token.Pos) (items []CompletionI
}
// 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 @@ func lexical(path []ast.Node, pos token.Pos, pkg *types.Package, info *types.Inf
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.

View File

@ -19,6 +19,4 @@ func _() {
}
}
//@complete("", Foo, IntFoo, StructFoo)
type IntFoo int //@item(IntFoo, "IntFoo", "int", "type")
type IntFoo int //@item(IntFoo, "IntFoo", "int", "type"),complete("", Foo, IntFoo, StructFoo)