diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 3f70f85060..1a8fc68f79 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -61,16 +61,14 @@ func Completion(ctx context.Context, f File, pos token.Pos) (items []CompletionI return nil, "", fmt.Errorf("cannot find node enclosing position") } - // Skip completion inside comment blocks or string literals. - switch lit := path[0].(type) { - case *ast.File, *ast.BlockStmt: - if inComment(pos, file.Comments) { - return items, prefix, nil - } - case *ast.BasicLit: - if lit.Kind == token.STRING { - return items, prefix, nil - } + // Skip completion inside comments. + if inComment(pos, file.Comments) { + return items, prefix, nil + } + + // Skip completion inside any kind of literal. + if _, ok := path[0].(*ast.BasicLit); ok { + return items, prefix, nil } // Save certain facts about the query position, including the expected type @@ -283,10 +281,8 @@ func lexical(path []ast.Node, pos token.Pos, pkg *types.Package, info *types.Inf // 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 - } + if g.Pos() <= pos && pos <= g.End() { + return true } } return false diff --git a/internal/lsp/testdata/bar/bar.go.in b/internal/lsp/testdata/bar/bar.go.in index 1677e7361b..54f5c64a0f 100644 --- a/internal/lsp/testdata/bar/bar.go.in +++ b/internal/lsp/testdata/bar/bar.go.in @@ -38,9 +38,9 @@ func _() { Value: Valen //@complete("le", Valentine) } _ = foo.StructFoo{ - Value: //@complete(re"$", Valentine, foo, Bar, helper) + Value: //@complete(" //", Valentine, foo, Bar, helper) } _ = foo.StructFoo{ Value: //@complete(" ", Valentine, foo, Bar, helper) } -} \ No newline at end of file +} diff --git a/internal/lsp/testdata/basiclit/basiclit.go b/internal/lsp/testdata/basiclit/basiclit.go new file mode 100644 index 0000000000..ab895dc011 --- /dev/null +++ b/internal/lsp/testdata/basiclit/basiclit.go @@ -0,0 +1,13 @@ +package basiclit + +func _() { + var a int // something for lexical completions + + _ = "hello." //@complete(".") + + _ = 1 //@complete(" //") + + _ = 1. //@complete(".") + + _ = 'a' //@complete("' ") +} diff --git a/internal/lsp/testdata/baz/baz.go.in b/internal/lsp/testdata/baz/baz.go.in index 90d952beea..534854c3da 100644 --- a/internal/lsp/testdata/baz/baz.go.in +++ b/internal/lsp/testdata/baz/baz.go.in @@ -18,14 +18,14 @@ func Baz() { func _() { bob := f.StructFoo{Value: 5} - if x := bob. //@complete(re"$", Value) + if x := bob. //@complete(" //", Value) switch true == false { case true: - if x := bob. //@complete(re"$", Value) + if x := bob. //@complete(" //", Value) case false: } if x := bob.Va //@complete("a", Value) switch true == true { default: } -} \ No newline at end of file +} diff --git a/internal/lsp/testdata/comments/comments.go b/internal/lsp/testdata/comments/comments.go new file mode 100644 index 0000000000..e261cfd342 --- /dev/null +++ b/internal/lsp/testdata/comments/comments.go @@ -0,0 +1,27 @@ +package comments + +var p bool + +//@complete(re"$") + +func _() { + var a int + + switch a { + case 1: + //@complete(re"$") + _ = a + } + + var b chan int + select { + case <-b: + //@complete(re"$") + _ = b + } + + var ( + //@complete(re"$") + _ = a + ) +} diff --git a/internal/lsp/testdata/stringlit/stringlit.go.in b/internal/lsp/testdata/stringlit/stringlit.go.in deleted file mode 100644 index 2558eb5790..0000000000 --- a/internal/lsp/testdata/stringlit/stringlit.go.in +++ /dev/null @@ -1,5 +0,0 @@ -package stringlit - -func _() { - _ := "hello." //@complete(".") -} diff --git a/internal/lsp/tests/tests.go b/internal/lsp/tests/tests.go index bd36b82b06..046c9ee5ad 100644 --- a/internal/lsp/tests/tests.go +++ b/internal/lsp/tests/tests.go @@ -27,7 +27,7 @@ import ( // We hardcode the expected number of test cases to ensure that all tests // are being executed. If a test is added, this number must be changed. const ( - ExpectedCompletionsCount = 75 + ExpectedCompletionsCount = 82 ExpectedDiagnosticsCount = 16 ExpectedFormatCount = 4 ExpectedDefinitionsCount = 21