From f59e586bb35d2073f940c7d02e0746dcec436810 Mon Sep 17 00:00:00 2001 From: Marwan Sulaiman Date: Fri, 15 Mar 2019 12:10:31 -0400 Subject: [PATCH] cmd/lsp: skip completion in string literals This CL ensures that a "." inside a string literal will return an empty completion list. Fixes golang/go#30477 Change-Id: I1442d0acab4c12a829047805f745c4729d69c208 Reviewed-on: https://go-review.googlesource.com/c/tools/+/167857 Reviewed-by: Rebecca Stambler Run-TryBot: Rebecca Stambler --- internal/lsp/lsp_test.go | 2 +- internal/lsp/source/completion.go | 8 ++++++-- internal/lsp/testdata/stringlit/stringlit.go.in | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 internal/lsp/testdata/stringlit/stringlit.go.in diff --git a/internal/lsp/lsp_test.go b/internal/lsp/lsp_test.go index 00187cb4bc..eec3cb86ff 100644 --- a/internal/lsp/lsp_test.go +++ b/internal/lsp/lsp_test.go @@ -36,7 +36,7 @@ func testLSP(t *testing.T, exporter packagestest.Exporter) { // 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 = 63 + const expectedCompletionsCount = 64 const expectedDiagnosticsCount = 16 const expectedFormatCount = 4 const expectedDefinitionsCount = 16 diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 86c1b6d646..81702ea092 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -69,12 +69,16 @@ func Completion(ctx context.Context, f File, pos token.Pos) (items []CompletionI } } - // Skip completion inside comment blocks. - switch path[0].(type) { + // 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 + } } // Save certain facts about the query position, including the expected type diff --git a/internal/lsp/testdata/stringlit/stringlit.go.in b/internal/lsp/testdata/stringlit/stringlit.go.in new file mode 100644 index 0000000000..2558eb5790 --- /dev/null +++ b/internal/lsp/testdata/stringlit/stringlit.go.in @@ -0,0 +1,5 @@ +package stringlit + +func _() { + _ := "hello." //@complete(".") +}