From 1b668f2091859760ba5554bd33f4af390d29ee8b Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Fri, 24 Jan 2020 13:34:08 -0500 Subject: [PATCH] internal/lsp: disable literal completion candidates for some clients If a client doesn't support the snippet format in completion insert text, they can't take full advantage of the literal completion candidates. Disable it in those cases, and remove the setting in internal/lsp/source/options.go. Fixes golang/go#36655. Change-Id: Ibc045a0f2945aab753b0187194a03d0c0398dba5 Reviewed-on: https://go-review.googlesource.com/c/tools/+/216299 Run-TryBot: Rebecca Stambler Reviewed-by: Heschi Kreinick --- internal/lsp/completion_test.go | 10 +++++++--- internal/lsp/source/completion.go | 2 +- internal/lsp/source/options.go | 4 ---- internal/lsp/source/source_test.go | 9 ++++++--- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/internal/lsp/completion_test.go b/internal/lsp/completion_test.go index 6dca51f79a..cde7461946 100644 --- a/internal/lsp/completion_test.go +++ b/internal/lsp/completion_test.go @@ -14,8 +14,14 @@ func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, got := r.callCompletion(t, src, func(opts *source.Options) { opts.DeepCompletion = false opts.Matcher = source.CaseInsensitive - opts.Literal = strings.Contains(string(src.URI()), "literal") opts.UnimportedCompletion = false + opts.InsertTextFormat = protocol.PlainTextTextFormat + // Only enable literal completions if in the completion literals tests. + // TODO(rstambler): Separate out literal completion tests. + if strings.Contains(string(src.URI()), "literal") { + opts.InsertTextFormat = protocol.SnippetTextFormat + } + }) if !strings.Contains(string(src.URI()), "builtins") { got = tests.FilterBuiltins(got) @@ -31,7 +37,6 @@ func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.C opts.Placeholders = placeholders opts.DeepCompletion = true opts.Matcher = source.Fuzzy - opts.Literal = true opts.UnimportedCompletion = false }) got := tests.FindItem(list, *items[expected.CompletionItem]) @@ -103,7 +108,6 @@ func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completi got := r.callCompletion(t, src, func(opts *source.Options) { opts.DeepCompletion = true opts.Matcher = source.Fuzzy - opts.Literal = true opts.UnimportedCompletion = false }) want := expected(t, test, items) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index 9c25712ad7..37ccc01ca9 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -466,7 +466,7 @@ func Completion(ctx context.Context, snapshot Snapshot, fh FileHandle, pos proto documentation: opts.CompletionDocumentation, fullDocumentation: opts.HoverKind == FullDocumentation, placeholders: opts.Placeholders, - literal: opts.Literal, + literal: opts.InsertTextFormat == protocol.SnippetTextFormat, budget: opts.CompletionBudget, }, // default to a matcher that always matches diff --git a/internal/lsp/source/options.go b/internal/lsp/source/options.go index 1dbc75b6e3..9a5109d4db 100644 --- a/internal/lsp/source/options.go +++ b/internal/lsp/source/options.go @@ -81,7 +81,6 @@ var ( DeepCompletion: true, UnimportedCompletion: true, CompletionDocumentation: true, - Literal: true, } DefaultHooks = Hooks{ ComputeEdits: myers.ComputeEdits, @@ -159,9 +158,6 @@ type UserOptions struct { // Placeholders adds placeholders to parameters and structs in completion // results. Placeholders bool - - // Literal enables completion for map, slice, and function literals. - Literal bool } type completionOptions struct { diff --git a/internal/lsp/source/source_test.go b/internal/lsp/source/source_test.go index e561a409f9..ee46f940cd 100644 --- a/internal/lsp/source/source_test.go +++ b/internal/lsp/source/source_test.go @@ -106,9 +106,14 @@ func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, } _, got := r.callCompletion(t, src, func(opts *source.Options) { opts.Matcher = source.CaseInsensitive - opts.Literal = strings.Contains(string(src.URI()), "literal") opts.DeepCompletion = false opts.UnimportedCompletion = false + opts.InsertTextFormat = protocol.PlainTextTextFormat + // Only enable literal completions if in the completion literals tests. + // TODO(rstambler): Separate out literal completion tests. + if strings.Contains(string(src.URI()), "literal") { + opts.InsertTextFormat = protocol.SnippetTextFormat + } }) if !strings.Contains(string(src.URI()), "builtins") { got = tests.FilterBuiltins(got) @@ -122,7 +127,6 @@ func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.C _, list := r.callCompletion(t, src, func(opts *source.Options) { opts.Placeholders = placeholders opts.DeepCompletion = true - opts.Literal = true }) got := tests.FindItem(list, *items[expected.CompletionItem]) want := expected.PlainSnippet @@ -217,7 +221,6 @@ func (r *runner) RankCompletion(t *testing.T, src span.Span, test tests.Completi _, got := r.callCompletion(t, src, func(opts *source.Options) { opts.DeepCompletion = true opts.Matcher = source.Fuzzy - opts.Literal = true }) if msg := tests.CheckCompletionOrder(want, got, true); msg != "" { t.Errorf("%s: %s", src, msg)