From dd0d5d485177fdcc188f5ec5ef32ee8278ab0a09 Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Sun, 2 Feb 2020 21:17:44 -0800 Subject: [PATCH] internal/lsp: filter keyword completions in tests Filter keywords out of completion results in tests similar to builtins. You don't care about keyword completions unless you are explicitly testing keyword completion. Change-Id: I0caaaef8b0f5b08c4b15ba3ada1a963f35a14028 Reviewed-on: https://go-review.googlesource.com/c/tools/+/217499 Run-TryBot: Muir Manders TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/completion_test.go | 20 +++++-------------- internal/lsp/source/source_test.go | 20 +++++-------------- .../lsp/testdata/rank/type_switch_rank.go.in | 2 +- internal/lsp/tests/completion.go | 17 +++++++++++++--- 4 files changed, 25 insertions(+), 34 deletions(-) diff --git a/internal/lsp/completion_test.go b/internal/lsp/completion_test.go index cde7461946..949ad2f0b2 100644 --- a/internal/lsp/completion_test.go +++ b/internal/lsp/completion_test.go @@ -23,9 +23,7 @@ func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, } }) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) want := expected(t, test, items) if diff := tests.DiffCompletionItems(want, got); diff != "" { t.Errorf("%s: %s", src, diff) @@ -51,9 +49,7 @@ func (r *runner) CompletionSnippet(t *testing.T, src span.Span, expected tests.C func (r *runner) UnimportedCompletion(t *testing.T, src span.Span, test tests.Completion, items tests.CompletionItems) { got := r.callCompletion(t, src, func(opts *source.Options) {}) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) want := expected(t, test, items) if diff := tests.CheckCompletionOrder(want, got, false); diff != "" { t.Errorf("%s: %s", src, diff) @@ -66,9 +62,7 @@ func (r *runner) DeepCompletion(t *testing.T, src span.Span, test tests.Completi opts.Matcher = source.CaseInsensitive opts.UnimportedCompletion = false }) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) want := expected(t, test, items) if msg := tests.DiffCompletionItems(want, got); msg != "" { t.Errorf("%s: %s", src, msg) @@ -81,9 +75,7 @@ func (r *runner) FuzzyCompletion(t *testing.T, src span.Span, test tests.Complet opts.Matcher = source.Fuzzy opts.UnimportedCompletion = false }) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) want := expected(t, test, items) if msg := tests.DiffCompletionItems(want, got); msg != "" { t.Errorf("%s: %s", src, msg) @@ -95,9 +87,7 @@ func (r *runner) CaseSensitiveCompletion(t *testing.T, src span.Span, test tests opts.Matcher = source.CaseSensitive opts.UnimportedCompletion = false }) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) want := expected(t, test, items) if msg := tests.DiffCompletionItems(want, got); msg != "" { t.Errorf("%s: %s", src, msg) diff --git a/internal/lsp/source/source_test.go b/internal/lsp/source/source_test.go index 334e03c622..b97c191cd9 100644 --- a/internal/lsp/source/source_test.go +++ b/internal/lsp/source/source_test.go @@ -115,9 +115,7 @@ func (r *runner) Completion(t *testing.T, src span.Span, test tests.Completion, opts.InsertTextFormat = protocol.SnippetTextFormat } }) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) if diff := tests.DiffCompletionItems(want, got); diff != "" { t.Errorf("%s: %s", src, diff) } @@ -144,9 +142,7 @@ func (r *runner) UnimportedCompletion(t *testing.T, src span.Span, test tests.Co want = append(want, tests.ToProtocolCompletionItem(*items[pos])) } _, got := r.callCompletion(t, src, func(opts *source.Options) {}) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) if diff := tests.CheckCompletionOrder(want, got, false); diff != "" { t.Errorf("%s: %s", src, diff) } @@ -162,9 +158,7 @@ func (r *runner) DeepCompletion(t *testing.T, src span.Span, test tests.Completi opts.Matcher = source.CaseInsensitive opts.UnimportedCompletion = false }) - if !strings.Contains(string(src.URI()), "builtins") { - list = tests.FilterBuiltins(list) - } + list = tests.FilterBuiltins(src, list) fuzzyMatcher := fuzzy.NewMatcher(prefix) var got []protocol.CompletionItem for _, item := range list { @@ -188,9 +182,7 @@ func (r *runner) FuzzyCompletion(t *testing.T, src span.Span, test tests.Complet opts.Matcher = source.Fuzzy opts.UnimportedCompletion = false }) - if !strings.Contains(string(src.URI()), "builtins") { - got = tests.FilterBuiltins(got) - } + got = tests.FilterBuiltins(src, got) if msg := tests.DiffCompletionItems(want, got); msg != "" { t.Errorf("%s: %s", src, msg) } @@ -205,9 +197,7 @@ func (r *runner) CaseSensitiveCompletion(t *testing.T, src span.Span, test tests opts.Matcher = source.CaseSensitive opts.UnimportedCompletion = false }) - if !strings.Contains(string(src.URI()), "builtins") { - list = tests.FilterBuiltins(list) - } + list = tests.FilterBuiltins(src, list) if diff := tests.DiffCompletionItems(want, list); diff != "" { t.Errorf("%s: %s", src, diff) } diff --git a/internal/lsp/testdata/rank/type_switch_rank.go.in b/internal/lsp/testdata/rank/type_switch_rank.go.in index 6cec59742d..457c64b82e 100644 --- a/internal/lsp/testdata/rank/type_switch_rank.go.in +++ b/internal/lsp/testdata/rank/type_switch_rank.go.in @@ -6,6 +6,6 @@ func _() { switch interface{}(pear).(type) { case b: //@complete(":", basket, banana) - b //@complete(" //", banana, basket, break) + b //@complete(" //", banana, basket) } } diff --git a/internal/lsp/tests/completion.go b/internal/lsp/tests/completion.go index ba5ec70f24..8ef13a6088 100644 --- a/internal/lsp/tests/completion.go +++ b/internal/lsp/tests/completion.go @@ -3,12 +3,14 @@ package tests import ( "bytes" "fmt" + "go/token" "sort" "strconv" "strings" "golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/source" + "golang.org/x/tools/internal/span" ) func ToProtocolCompletionItems(items []source.CompletionItem) []protocol.CompletionItem { @@ -38,12 +40,21 @@ func ToProtocolCompletionItem(item source.CompletionItem) protocol.CompletionIte return pItem } -func FilterBuiltins(items []protocol.CompletionItem) []protocol.CompletionItem { - var got []protocol.CompletionItem +func FilterBuiltins(src span.Span, items []protocol.CompletionItem) []protocol.CompletionItem { + var ( + got []protocol.CompletionItem + wantBuiltins = strings.Contains(string(src.URI()), "builtins") + wantKeywords = strings.Contains(string(src.URI()), "keywords") + ) for _, item := range items { - if isBuiltin(item.Label, item.Detail, item.Kind) { + if !wantBuiltins && isBuiltin(item.Label, item.Detail, item.Kind) { continue } + + if !wantKeywords && token.Lookup(item.Label).IsKeyword() { + continue + } + got = append(got, item) } return got