mirror of
https://github.com/golang/go
synced 2024-11-18 15:04:44 -07:00
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 <muir@mnd.rs> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
f66ef90017
commit
dd0d5d4851
@ -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)
|
||||
|
@ -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)
|
||||
}
|
||||
|
@ -6,6 +6,6 @@ func _() {
|
||||
|
||||
switch interface{}(pear).(type) {
|
||||
case b: //@complete(":", basket, banana)
|
||||
b //@complete(" //", banana, basket, break)
|
||||
b //@complete(" //", banana, basket)
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user