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