diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 4bb2d3e3..6df0f577 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -59,6 +59,7 @@ func Completion(ctx context.Context, f File, pos token.Pos) (items []CompletionI if path == nil { return nil, "", fmt.Errorf("cannot find node enclosing position") } + // If the position is not an identifier but immediately follows // an identifier or selector period (as is common when // requesting a completion), use the path to the preceding node. @@ -71,6 +72,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 + } + // Save certain facts about the query position, including the expected type // of the completion result, the signature of the function enclosing the // position. @@ -246,6 +252,24 @@ 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 + } + } + return true + }) + return found +} + // complit finds completions for field names inside a composite literal. // It reports whether the node was handled as part of a composite literal. func complit(path []ast.Node, pos token.Pos, pkg *types.Package, info *types.Info, found finder) (items []CompletionItem, prefix string, ok bool) { diff --git a/internal/lsp/testdata/foo/foo.go b/internal/lsp/testdata/foo/foo.go index cc515c2a..eb06d329 100644 --- a/internal/lsp/testdata/foo/foo.go +++ b/internal/lsp/testdata/foo/foo.go @@ -20,4 +20,5 @@ func _() { } //@complete("", Foo, IntFoo, StructFoo) + type IntFoo int //@item(IntFoo, "IntFoo", "int", "type")