2019-05-08 14:04:29 -06:00
|
|
|
// Copyright 2018 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package source_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2019-08-30 10:02:01 -06:00
|
|
|
"os"
|
2019-05-08 14:04:29 -06:00
|
|
|
"os/exec"
|
2019-07-08 19:53:01 -06:00
|
|
|
"path/filepath"
|
2019-05-08 14:04:29 -06:00
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2019-09-14 10:29:08 -06:00
|
|
|
"time"
|
2019-05-08 14:04:29 -06:00
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages/packagestest"
|
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
internal/lsp: add fuzzy completion matching
Make use of the existing fuzzy matcher to perform server side fuzzy
completion matching. Previously the server did exact prefix matching
for completion candidates and left fancy filtering to the
client. Having the server do fuzzy matching has two main benefits:
- Deep completions now update as you type. The completion candidates
returned to the client are marked "incomplete", causing the client
to refresh the candidates after every keystroke. This lets the
server pick the most relevant set of deep completion candidates.
- All editors get fuzzy matching for free. VSCode has fuzzy matching
out of the box, but some editors either don't provide it, or it can
be difficult to set up.
I modified the fuzzy matcher to allow matches where the input doesn't
match the final segment of the candidate. For example, previously "ab"
would not match "abc.def" because the "b" in "ab" did not match the
final segment "def". I can see how this is useful when the text
matching happens in a vacuum and candidate's final segment is the most
specific part. But, in our case, we have various other methods to
order candidates, so we don't want to exclude them just because the
final segment doesn't match. For example, if we know our candidate
needs to be type "context.Context" and "foo.ctx" is of the right type,
we want to suggest "foo.ctx" as soon as the user starts inputting
"foo", even though "foo" doesn't match "ctx" at all.
Note that fuzzy matching is behind the "useDeepCompletions" config
flag for the time being.
Change-Id: Ic7674f0cf885af770c30daef472f2e3c5ac4db78
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190099
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-08-13 14:45:19 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/fuzzy"
|
2019-08-16 15:05:40 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
2019-05-08 14:04:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
|
|
|
"golang.org/x/tools/internal/span"
|
2019-08-30 10:02:01 -06:00
|
|
|
"golang.org/x/tools/internal/testenv"
|
2019-09-17 16:52:19 -06:00
|
|
|
errors "golang.org/x/xerrors"
|
2019-05-08 14:04:29 -06:00
|
|
|
)
|
|
|
|
|
2019-08-30 10:02:01 -06:00
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
testenv.ExitIfSmallMachine()
|
|
|
|
os.Exit(m.Run())
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:04:29 -06:00
|
|
|
func TestSource(t *testing.T) {
|
|
|
|
packagestest.TestAll(t, testSource)
|
|
|
|
}
|
|
|
|
|
|
|
|
type runner struct {
|
2019-05-14 21:04:23 -06:00
|
|
|
view source.View
|
2019-05-08 14:04:29 -06:00
|
|
|
data *tests.Data
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx context.Context
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func testSource(t *testing.T, exporter packagestest.Exporter) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := tests.Context(t)
|
2019-05-08 14:04:29 -06:00
|
|
|
data := tests.Load(t, exporter, "../testdata")
|
|
|
|
defer data.Exported.Cleanup()
|
|
|
|
|
2019-05-15 10:24:49 -06:00
|
|
|
cache := cache.New()
|
2019-07-10 19:01:12 -06:00
|
|
|
session := cache.NewSession(ctx)
|
2019-09-17 09:10:48 -06:00
|
|
|
options := tests.DefaultOptions()
|
2019-09-11 11:13:44 -06:00
|
|
|
options.Env = data.Config.Env
|
2019-05-08 14:04:29 -06:00
|
|
|
r := &runner{
|
2019-09-11 11:13:44 -06:00
|
|
|
view: session.NewView(ctx, "source_test", span.FileURI(data.Config.Dir), options),
|
2019-05-08 14:04:29 -06:00
|
|
|
data: data,
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx: ctx,
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-05-17 08:51:19 -06:00
|
|
|
for filename, content := range data.Config.Overlay {
|
2019-09-18 23:21:54 -06:00
|
|
|
session.SetOverlay(span.FileURI(filename), source.DetectLanguage("", filename), content)
|
2019-05-17 08:51:19 -06:00
|
|
|
}
|
2019-05-08 14:04:29 -06:00
|
|
|
tests.Run(t, r, data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runner) Diagnostics(t *testing.T, data tests.Diagnostics) {
|
|
|
|
for uri, want := range data {
|
2019-07-10 19:11:23 -06:00
|
|
|
f, err := r.view.GetFile(r.ctx, uri)
|
2019-05-23 13:03:11 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-09-06 16:25:36 -06:00
|
|
|
results, _, err := source.Diagnostics(r.ctx, r.view, f.(source.GoFile), nil)
|
2019-05-08 14:04:29 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
got := results[uri]
|
2019-08-02 17:45:56 -06:00
|
|
|
// A special case to test that there are no diagnostics for a file.
|
|
|
|
if len(want) == 1 && want[0].Source == "no_diagnostics" {
|
|
|
|
if len(got) != 0 {
|
|
|
|
t.Errorf("expected no diagnostics for %s, got %v", uri, got)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-08-14 18:12:18 -06:00
|
|
|
if diff := tests.DiffDiagnostics(uri, want, got); diff != "" {
|
2019-05-08 14:04:29 -06:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 09:10:48 -06:00
|
|
|
func (r *runner) Completion(t *testing.T, data tests.Completions, items tests.CompletionItems) {
|
2019-06-17 21:56:06 -06:00
|
|
|
for src, test := range data {
|
2019-09-17 09:10:48 -06:00
|
|
|
var want []protocol.CompletionItem
|
2019-06-17 21:56:06 -06:00
|
|
|
for _, pos := range test.CompletionItems {
|
2019-09-17 09:10:48 -06:00
|
|
|
want = append(want, tests.ToProtocolCompletionItem(*items[pos]))
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
prefix, list := r.callCompletion(t, src, source.CompletionOptions{
|
2019-09-05 22:17:36 -06:00
|
|
|
Documentation: true,
|
2019-09-17 09:10:48 -06:00
|
|
|
FuzzyMatching: true,
|
2019-06-27 11:50:01 -06:00
|
|
|
})
|
2019-09-17 09:10:48 -06:00
|
|
|
if !strings.Contains(string(src.URI()), "builtins") {
|
|
|
|
list = tests.FilterBuiltins(list)
|
2019-06-27 14:28:22 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
var got []protocol.CompletionItem
|
2019-05-08 14:04:29 -06:00
|
|
|
for _, item := range list {
|
2019-09-17 09:10:48 -06:00
|
|
|
if !strings.HasPrefix(strings.ToLower(item.Label), prefix) {
|
2019-05-08 14:04:29 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
got = append(got, item)
|
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
if diff := tests.DiffCompletionItems(want, got); diff != "" {
|
|
|
|
t.Errorf("%s: %s", src, diff)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
2019-08-27 17:41:48 -06:00
|
|
|
|
2019-09-17 09:10:48 -06:00
|
|
|
func (r *runner) CompletionSnippets(t *testing.T, data tests.CompletionSnippets, items tests.CompletionItems) {
|
|
|
|
for _, placeholders := range []bool{true, false} {
|
|
|
|
for src, expected := range data {
|
|
|
|
_, list := r.callCompletion(t, src, source.CompletionOptions{
|
|
|
|
Placeholders: placeholders,
|
|
|
|
Deep: true,
|
|
|
|
Budget: 5 * time.Second,
|
2019-06-27 11:50:01 -06:00
|
|
|
})
|
2019-09-17 09:10:48 -06:00
|
|
|
got := tests.FindItem(list, *items[expected.CompletionItem])
|
|
|
|
want := expected.PlainSnippet
|
|
|
|
if placeholders {
|
|
|
|
want = expected.PlaceholderSnippet
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
if diff := tests.DiffSnippets(want, got); diff != "" {
|
|
|
|
t.Errorf("%s: %s", src, diff)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 09:10:48 -06:00
|
|
|
func (r *runner) UnimportedCompletions(t *testing.T, data tests.UnimportedCompletions, items tests.CompletionItems) {
|
|
|
|
for src, test := range data {
|
|
|
|
var want []protocol.CompletionItem
|
|
|
|
for _, pos := range test.CompletionItems {
|
|
|
|
want = append(want, tests.ToProtocolCompletionItem(*items[pos]))
|
|
|
|
}
|
|
|
|
_, got := r.callCompletion(t, src, source.CompletionOptions{
|
|
|
|
Unimported: true,
|
|
|
|
})
|
|
|
|
if !strings.Contains(string(src.URI()), "builtins") {
|
|
|
|
got = tests.FilterBuiltins(got)
|
|
|
|
}
|
|
|
|
if diff := tests.DiffCompletionItems(want, got); diff != "" {
|
|
|
|
t.Errorf("%s: %s", src, diff)
|
|
|
|
}
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-17 09:10:48 -06:00
|
|
|
func (r *runner) DeepCompletions(t *testing.T, data tests.DeepCompletions, items tests.CompletionItems) {
|
|
|
|
for src, test := range data {
|
|
|
|
var want []protocol.CompletionItem
|
|
|
|
for _, pos := range test.CompletionItems {
|
|
|
|
want = append(want, tests.ToProtocolCompletionItem(*items[pos]))
|
|
|
|
}
|
|
|
|
prefix, list := r.callCompletion(t, src, source.CompletionOptions{
|
|
|
|
Deep: true,
|
|
|
|
Budget: 5 * time.Second,
|
|
|
|
Documentation: true,
|
|
|
|
})
|
|
|
|
if !strings.Contains(string(src.URI()), "builtins") {
|
|
|
|
list = tests.FilterBuiltins(list)
|
|
|
|
}
|
|
|
|
fuzzyMatcher := fuzzy.NewMatcher(prefix, fuzzy.Symbol)
|
|
|
|
var got []protocol.CompletionItem
|
|
|
|
for _, item := range list {
|
|
|
|
if fuzzyMatcher.Score(item.Label) < 0 {
|
2019-06-27 11:50:01 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
got = append(got, item)
|
|
|
|
}
|
|
|
|
if msg := tests.DiffCompletionItems(want, got); msg != "" {
|
|
|
|
t.Errorf("%s: %s", src, msg)
|
2019-06-27 11:50:01 -06:00
|
|
|
}
|
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
}
|
2019-06-27 11:50:01 -06:00
|
|
|
|
2019-09-17 09:10:48 -06:00
|
|
|
func (r *runner) FuzzyCompletions(t *testing.T, data tests.FuzzyCompletions, items tests.CompletionItems) {
|
|
|
|
for src, test := range data {
|
|
|
|
var want []protocol.CompletionItem
|
|
|
|
for _, pos := range test.CompletionItems {
|
|
|
|
want = append(want, tests.ToProtocolCompletionItem(*items[pos]))
|
|
|
|
}
|
|
|
|
prefix, list := r.callCompletion(t, src, source.CompletionOptions{
|
|
|
|
FuzzyMatching: true,
|
|
|
|
Deep: true,
|
|
|
|
Budget: 5 * time.Second,
|
|
|
|
})
|
|
|
|
if !strings.Contains(string(src.URI()), "builtins") {
|
|
|
|
list = tests.FilterBuiltins(list)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
var fuzzyMatcher *fuzzy.Matcher
|
|
|
|
if prefix != "" {
|
|
|
|
fuzzyMatcher = fuzzy.NewMatcher(prefix, fuzzy.Symbol)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
var got []protocol.CompletionItem
|
|
|
|
for _, item := range list {
|
|
|
|
if fuzzyMatcher != nil && fuzzyMatcher.Score(item.Label) < 0 {
|
|
|
|
continue
|
2019-08-06 16:51:17 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
got = append(got, item)
|
2019-08-06 16:51:17 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
if msg := tests.DiffCompletionItems(want, got); msg != "" {
|
|
|
|
t.Errorf("%s: %s", src, msg)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
}
|
2019-06-17 21:56:06 -06:00
|
|
|
}
|
|
|
|
|
2019-09-17 09:10:48 -06:00
|
|
|
func (r *runner) RankCompletions(t *testing.T, data tests.RankCompletions, items tests.CompletionItems) {
|
|
|
|
for src, test := range data {
|
|
|
|
var want []protocol.CompletionItem
|
|
|
|
for _, pos := range test.CompletionItems {
|
|
|
|
want = append(want, tests.ToProtocolCompletionItem(*items[pos]))
|
|
|
|
}
|
|
|
|
prefix, list := r.callCompletion(t, src, source.CompletionOptions{
|
|
|
|
FuzzyMatching: true,
|
|
|
|
Deep: true,
|
|
|
|
Budget: 5 * time.Second,
|
|
|
|
})
|
|
|
|
if !strings.Contains(string(src.URI()), "builtins") {
|
|
|
|
list = tests.FilterBuiltins(list)
|
|
|
|
}
|
|
|
|
fuzzyMatcher := fuzzy.NewMatcher(prefix, fuzzy.Symbol)
|
|
|
|
var got []protocol.CompletionItem
|
|
|
|
for _, item := range list {
|
|
|
|
if fuzzyMatcher.Score(item.Label) < 0 {
|
|
|
|
continue
|
2019-06-17 21:56:06 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
got = append(got, item)
|
2019-06-17 21:56:06 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
if msg := tests.CheckCompletionOrder(want, got); msg != "" {
|
|
|
|
t.Errorf("%s: %s", src, msg)
|
2019-06-17 21:56:06 -06:00
|
|
|
}
|
|
|
|
}
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
|
2019-09-17 09:10:48 -06:00
|
|
|
func (r *runner) callCompletion(t *testing.T, src span.Span, options source.CompletionOptions) (string, []protocol.CompletionItem) {
|
|
|
|
f, err := r.view.GetFile(r.ctx, src.URI())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
list, surrounding, err := source.Completion(r.ctx, r.view, f.(source.GoFile), protocol.Position{
|
|
|
|
Line: float64(src.Start().Line() - 1),
|
|
|
|
Character: float64(src.Start().Column() - 1),
|
|
|
|
}, options)
|
2019-09-17 16:52:19 -06:00
|
|
|
if err != nil && !errors.As(err, &source.ErrIsDefinition{}) {
|
2019-09-17 09:10:48 -06:00
|
|
|
t.Fatalf("failed for %v: %v", src, err)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
var prefix string
|
|
|
|
if surrounding != nil {
|
|
|
|
prefix = strings.ToLower(surrounding.Prefix())
|
|
|
|
}
|
|
|
|
// TODO(rstambler): In testing this out, I noticed that scores are equal,
|
|
|
|
// even when they shouldn't be. This needs more investigation.
|
|
|
|
sort.SliceStable(list, func(i, j int) bool {
|
|
|
|
return list[i].Score > list[j].Score
|
|
|
|
})
|
|
|
|
var numDeepCompletionsSeen int
|
|
|
|
var items []source.CompletionItem
|
|
|
|
// Apply deep completion filtering.
|
|
|
|
for _, item := range list {
|
|
|
|
if item.Depth > 0 {
|
|
|
|
if !options.Deep {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if numDeepCompletionsSeen >= source.MaxDeepCompletions {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
numDeepCompletionsSeen++
|
|
|
|
}
|
|
|
|
items = append(items, item)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-17 09:10:48 -06:00
|
|
|
return prefix, tests.ToProtocolCompletionItems(items)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
|
2019-08-28 19:48:29 -06:00
|
|
|
func (r *runner) FoldingRange(t *testing.T, data tests.FoldingRanges) {
|
|
|
|
for _, spn := range data {
|
|
|
|
uri := spn.URI()
|
|
|
|
|
|
|
|
f, err := r.view.GetFile(r.ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
2019-08-29 17:03:23 -06:00
|
|
|
data, _, err := f.Handle(r.ctx).Read(r.ctx)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-28 19:48:29 -06:00
|
|
|
|
2019-08-29 17:03:23 -06:00
|
|
|
// Test all folding ranges.
|
|
|
|
ranges, err := source.FoldingRange(r.ctx, r.view, f.(source.GoFile), false)
|
2019-08-28 19:48:29 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-29 17:03:23 -06:00
|
|
|
r.foldingRanges(t, "foldingRange", uri, string(data), ranges)
|
|
|
|
|
|
|
|
// Test folding ranges with lineFoldingOnly
|
|
|
|
ranges, err = source.FoldingRange(r.ctx, r.view, f.(source.GoFile), true)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
r.foldingRanges(t, "foldingRange-lineFolding", uri, string(data), ranges)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 12:11:42 -06:00
|
|
|
func (r *runner) foldingRanges(t *testing.T, prefix string, uri span.URI, data string, ranges []*source.FoldingRangeInfo) {
|
2019-08-29 17:03:23 -06:00
|
|
|
t.Helper()
|
|
|
|
// Fold all ranges.
|
2019-09-05 18:14:09 -06:00
|
|
|
nonOverlapping := nonOverlappingRanges(t, ranges)
|
2019-08-29 17:03:23 -06:00
|
|
|
for i, rngs := range nonOverlapping {
|
|
|
|
got, err := foldRanges(string(data), rngs)
|
2019-08-28 19:48:29 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-29 17:03:23 -06:00
|
|
|
tag := fmt.Sprintf("%s-%d", prefix, i)
|
|
|
|
want := string(r.data.Golden(tag, uri.Filename(), func() ([]byte, error) {
|
|
|
|
return []byte(got), nil
|
|
|
|
}))
|
|
|
|
|
|
|
|
if want != got {
|
|
|
|
t.Errorf("%s: foldingRanges failed for %s, expected:\n%v\ngot:\n%v", tag, uri.Filename(), want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by kind.
|
|
|
|
kinds := []protocol.FoldingRangeKind{protocol.Imports, protocol.Comment}
|
|
|
|
for _, kind := range kinds {
|
2019-08-30 12:11:42 -06:00
|
|
|
var kindOnly []*source.FoldingRangeInfo
|
2019-08-29 17:03:23 -06:00
|
|
|
for _, fRng := range ranges {
|
|
|
|
if fRng.Kind == kind {
|
|
|
|
kindOnly = append(kindOnly, fRng)
|
|
|
|
}
|
|
|
|
}
|
2019-08-29 13:28:51 -06:00
|
|
|
|
2019-09-05 18:14:09 -06:00
|
|
|
nonOverlapping := nonOverlappingRanges(t, kindOnly)
|
2019-08-29 13:28:51 -06:00
|
|
|
for i, rngs := range nonOverlapping {
|
|
|
|
got, err := foldRanges(string(data), rngs)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
2019-08-29 17:03:23 -06:00
|
|
|
tag := fmt.Sprintf("%s-%s-%d", prefix, kind, i)
|
|
|
|
want := string(r.data.Golden(tag, uri.Filename(), func() ([]byte, error) {
|
2019-08-29 13:28:51 -06:00
|
|
|
return []byte(got), nil
|
|
|
|
}))
|
2019-08-28 19:48:29 -06:00
|
|
|
|
2019-08-29 13:28:51 -06:00
|
|
|
if want != got {
|
2019-08-29 17:03:23 -06:00
|
|
|
t.Errorf("%s: failed for %s, expected:\n%v\ngot:\n%v", tag, uri.Filename(), want, got)
|
2019-08-28 19:48:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:14:09 -06:00
|
|
|
func nonOverlappingRanges(t *testing.T, ranges []*source.FoldingRangeInfo) (res [][]*source.FoldingRangeInfo) {
|
2019-08-29 13:28:51 -06:00
|
|
|
for _, fRng := range ranges {
|
|
|
|
setNum := len(res)
|
|
|
|
for i := 0; i < len(res); i++ {
|
|
|
|
canInsert := true
|
|
|
|
for _, rng := range res[i] {
|
2019-09-05 18:14:09 -06:00
|
|
|
if conflict(t, rng, fRng) {
|
2019-08-29 13:28:51 -06:00
|
|
|
canInsert = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if canInsert {
|
|
|
|
setNum = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if setNum == len(res) {
|
2019-08-30 12:11:42 -06:00
|
|
|
res = append(res, []*source.FoldingRangeInfo{})
|
2019-08-29 13:28:51 -06:00
|
|
|
}
|
|
|
|
res[setNum] = append(res[setNum], fRng)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-09-05 18:14:09 -06:00
|
|
|
func conflict(t *testing.T, a, b *source.FoldingRangeInfo) bool {
|
|
|
|
arng, err := a.Range()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
brng, err := b.Range()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-08-29 13:28:51 -06:00
|
|
|
// a start position is <= b start positions
|
2019-09-05 18:14:09 -06:00
|
|
|
return protocol.ComparePosition(arng.Start, brng.Start) <= 0 && protocol.ComparePosition(arng.End, brng.Start) > 0
|
2019-08-29 13:28:51 -06:00
|
|
|
}
|
|
|
|
|
2019-08-30 12:11:42 -06:00
|
|
|
func foldRanges(contents string, ranges []*source.FoldingRangeInfo) (string, error) {
|
2019-08-28 19:48:29 -06:00
|
|
|
foldedText := "<>"
|
|
|
|
res := contents
|
|
|
|
// Apply the folds from the end of the file forward
|
|
|
|
// to preserve the offsets.
|
|
|
|
for i := len(ranges) - 1; i >= 0; i-- {
|
|
|
|
fRange := ranges[i]
|
2019-09-05 18:14:09 -06:00
|
|
|
spn, err := fRange.Span()
|
2019-08-28 19:48:29 -06:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
start := spn.Start().Offset()
|
|
|
|
end := spn.End().Offset()
|
|
|
|
|
|
|
|
tmp := res[0:start] + foldedText
|
|
|
|
res = tmp + res[end:]
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:04:29 -06:00
|
|
|
func (r *runner) Format(t *testing.T, data tests.Formats) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-05-08 14:04:29 -06:00
|
|
|
for _, spn := range data {
|
|
|
|
uri := spn.URI()
|
2019-06-06 11:51:00 -06:00
|
|
|
filename := uri.Filename()
|
2019-05-08 14:04:29 -06:00
|
|
|
gofmted := string(r.data.Golden("gofmt", filename, func() ([]byte, error) {
|
|
|
|
cmd := exec.Command("gofmt", filename)
|
|
|
|
out, _ := cmd.Output() // ignore error, sometimes we have intentionally ungofmt-able files
|
|
|
|
return out, nil
|
|
|
|
}))
|
|
|
|
f, err := r.view.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
2019-09-05 14:58:50 -06:00
|
|
|
edits, err := source.Format(ctx, r.view, f)
|
2019-05-08 14:04:29 -06:00
|
|
|
if err != nil {
|
|
|
|
if gofmted != "" {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-06-03 23:04:18 -06:00
|
|
|
data, _, err := f.Handle(ctx).Read(ctx)
|
|
|
|
if err != nil {
|
2019-09-09 22:36:39 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
m, err := r.data.Mapper(f.URI())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-05-17 10:15:22 -06:00
|
|
|
}
|
2019-09-05 14:58:50 -06:00
|
|
|
diffEdits, err := source.FromProtocolEdits(m, edits)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
got := diff.ApplyEdits(string(data), diffEdits)
|
2019-05-08 14:04:29 -06:00
|
|
|
if gofmted != got {
|
|
|
|
t.Errorf("format failed for %s, expected:\n%v\ngot:\n%v", filename, gofmted, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 20:42:59 -06:00
|
|
|
func (r *runner) Import(t *testing.T, data tests.Imports) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-05-31 20:42:59 -06:00
|
|
|
for _, spn := range data {
|
|
|
|
uri := spn.URI()
|
2019-06-06 11:51:00 -06:00
|
|
|
filename := uri.Filename()
|
2019-05-31 20:42:59 -06:00
|
|
|
goimported := string(r.data.Golden("goimports", filename, func() ([]byte, error) {
|
|
|
|
cmd := exec.Command("goimports", filename)
|
|
|
|
out, _ := cmd.Output() // ignore error, sometimes we have intentionally ungofmt-able files
|
|
|
|
return out, nil
|
|
|
|
}))
|
|
|
|
f, err := r.view.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
2019-09-06 21:58:07 -06:00
|
|
|
fh := f.Handle(ctx)
|
|
|
|
tok, err := r.view.Session().Cache().TokenHandle(fh).Token(ctx)
|
2019-07-11 19:05:55 -06:00
|
|
|
if err != nil {
|
2019-09-06 21:58:07 -06:00
|
|
|
t.Fatal(err)
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
2019-09-06 21:58:07 -06:00
|
|
|
rng, err := spn.Range(span.NewTokenConverter(r.data.Exported.ExpectFileSet, tok))
|
2019-05-31 20:42:59 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
2019-06-28 14:21:07 -06:00
|
|
|
edits, err := source.Imports(ctx, r.view, f.(source.GoFile), rng)
|
2019-05-31 20:42:59 -06:00
|
|
|
if err != nil {
|
|
|
|
if goimported != "" {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2019-09-06 21:58:07 -06:00
|
|
|
data, _, err := fh.Read(ctx)
|
2019-06-03 23:04:18 -06:00
|
|
|
if err != nil {
|
2019-09-09 22:36:39 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
m, err := r.data.Mapper(fh.Identity().URI)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-05-31 20:42:59 -06:00
|
|
|
}
|
2019-09-05 14:58:50 -06:00
|
|
|
diffEdits, err := source.FromProtocolEdits(m, edits)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
got := diff.ApplyEdits(string(data), diffEdits)
|
2019-05-31 20:42:59 -06:00
|
|
|
if goimported != got {
|
|
|
|
t.Errorf("import failed for %s, expected:\n%v\ngot:\n%v", filename, goimported, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-04 11:16:09 -06:00
|
|
|
func (r *runner) SuggestedFix(t *testing.T, data tests.SuggestedFixes) {
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:04:29 -06:00
|
|
|
func (r *runner) Definition(t *testing.T, data tests.Definitions) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-05-08 14:04:29 -06:00
|
|
|
for _, d := range data {
|
|
|
|
f, err := r.view.GetFile(ctx, d.Src.URI())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", d.Src, err)
|
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
_, srcRng, err := spanToRange(r.data, d.Src)
|
2019-07-11 19:05:55 -06:00
|
|
|
if err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
t.Fatal(err)
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
ident, err := source.Identifier(ctx, r.view, f.(source.GoFile), srcRng.Start)
|
2019-05-08 14:04:29 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", d.Src, err)
|
|
|
|
}
|
2019-08-12 14:59:23 -06:00
|
|
|
h, err := ident.Hover(ctx)
|
2019-05-08 14:04:29 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", d.Src, err)
|
|
|
|
}
|
2019-08-12 14:59:23 -06:00
|
|
|
var hover string
|
|
|
|
if h.Synopsis != "" {
|
|
|
|
hover += h.Synopsis + "\n"
|
|
|
|
}
|
|
|
|
hover += h.Signature
|
2019-08-26 22:26:45 -06:00
|
|
|
rng, err := ident.Range()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-08 14:04:29 -06:00
|
|
|
if d.IsType {
|
2019-08-26 22:26:45 -06:00
|
|
|
rng, err = ident.Type.Range()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-08 14:04:29 -06:00
|
|
|
hover = ""
|
|
|
|
}
|
|
|
|
if hover != "" {
|
|
|
|
tag := fmt.Sprintf("%s-hover", d.Name)
|
2019-06-06 11:51:00 -06:00
|
|
|
expectHover := string(r.data.Golden(tag, d.Src.URI().Filename(), func() ([]byte, error) {
|
2019-05-08 14:04:29 -06:00
|
|
|
return []byte(hover), nil
|
|
|
|
}))
|
|
|
|
if hover != expectHover {
|
|
|
|
t.Errorf("for %v got %q want %q", d.Src, hover, expectHover)
|
|
|
|
}
|
2019-05-15 15:58:16 -06:00
|
|
|
} else if !d.OnlyHover {
|
2019-09-05 16:54:05 -06:00
|
|
|
if _, defRng, err := spanToRange(r.data, d.Def); err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
} else if rng != defRng {
|
|
|
|
t.Errorf("for %v got %v want %v", d.Src, rng, d.Def)
|
2019-05-15 15:58:16 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("no tests ran for %s", d.Src.URI())
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runner) Highlight(t *testing.T, data tests.Highlights) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-05-08 14:04:29 -06:00
|
|
|
for name, locations := range data {
|
|
|
|
src := locations[0]
|
2019-09-05 16:54:05 -06:00
|
|
|
m, srcRng, err := spanToRange(r.data, src)
|
2019-07-11 19:05:55 -06:00
|
|
|
if err != nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
t.Fatal(err)
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
highlights, err := source.Highlight(ctx, r.view, src.URI(), srcRng.Start)
|
2019-06-21 15:00:02 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("highlight failed for %s: %v", src.URI(), err)
|
|
|
|
}
|
2019-05-08 14:04:29 -06:00
|
|
|
if len(highlights) != len(locations) {
|
2019-06-21 15:00:02 -06:00
|
|
|
t.Errorf("got %d highlights for %s, expected %d", len(highlights), name, len(locations))
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
for i, got := range highlights {
|
|
|
|
want, err := m.Range(locations[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("want %v, got %v\n", want, got)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 08:04:22 -06:00
|
|
|
func (r *runner) Reference(t *testing.T, data tests.References) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-06-07 08:04:22 -06:00
|
|
|
for src, itemList := range data {
|
|
|
|
f, err := r.view.GetFile(ctx, src.URI())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", src, err)
|
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
_, srcRng, err := spanToRange(r.data, src)
|
2019-07-11 19:05:55 -06:00
|
|
|
if err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
t.Fatal(err)
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
ident, err := source.Identifier(ctx, r.view, f.(source.GoFile), srcRng.Start)
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", src, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
want := make(map[span.Span]bool)
|
|
|
|
for _, pos := range itemList {
|
|
|
|
want[pos] = true
|
|
|
|
}
|
|
|
|
|
2019-09-06 21:58:07 -06:00
|
|
|
refs, err := ident.References(ctx)
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", src, err)
|
|
|
|
}
|
|
|
|
|
2019-06-26 16:05:29 -06:00
|
|
|
got := make(map[span.Span]bool)
|
|
|
|
for _, refInfo := range refs {
|
2019-08-26 22:26:45 -06:00
|
|
|
refSpan, err := refInfo.Span()
|
2019-06-07 08:04:22 -06:00
|
|
|
if err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
t.Fatal(err)
|
2019-06-07 08:04:22 -06:00
|
|
|
}
|
2019-06-26 16:05:29 -06:00
|
|
|
got[refSpan] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got) != len(want) {
|
|
|
|
t.Errorf("references failed: different lengths got %v want %v", len(got), len(want))
|
|
|
|
}
|
|
|
|
|
|
|
|
for spn, _ := range got {
|
|
|
|
if !want[spn] {
|
2019-06-07 08:04:22 -06:00
|
|
|
t.Errorf("references failed: incorrect references got %v want locations %v", got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 08:23:37 -06:00
|
|
|
func (r *runner) Rename(t *testing.T, data tests.Renames) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-06-18 08:23:37 -06:00
|
|
|
for spn, newText := range data {
|
2019-07-02 16:35:35 -06:00
|
|
|
tag := fmt.Sprintf("%s-rename", newText)
|
|
|
|
|
2019-06-18 08:23:37 -06:00
|
|
|
f, err := r.view.GetFile(ctx, spn.URI())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
_, srcRng, err := spanToRange(r.data, spn)
|
2019-07-11 19:05:55 -06:00
|
|
|
if err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
t.Fatal(err)
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
ident, err := source.Identifier(r.ctx, r.view, f.(source.GoFile), srcRng.Start)
|
2019-06-21 15:00:02 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2019-07-15 23:27:56 -06:00
|
|
|
continue
|
2019-06-21 15:00:02 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
changes, err := ident.Rename(r.ctx, r.view, newText)
|
2019-06-18 08:23:37 -06:00
|
|
|
if err != nil {
|
2019-07-02 16:35:35 -06:00
|
|
|
renamed := string(r.data.Golden(tag, spn.URI().Filename(), func() ([]byte, error) {
|
|
|
|
return []byte(err.Error()), nil
|
|
|
|
}))
|
|
|
|
if err.Error() != renamed {
|
|
|
|
t.Errorf("rename failed for %s, expected:\n%v\ngot:\n%v\n", newText, renamed, err)
|
|
|
|
}
|
2019-06-18 08:23:37 -06:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-07-08 19:53:01 -06:00
|
|
|
var res []string
|
|
|
|
for editSpn, edits := range changes {
|
|
|
|
f, err := r.view.GetFile(ctx, editSpn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
2019-09-09 22:36:39 -06:00
|
|
|
fh := f.Handle(ctx)
|
|
|
|
data, _, err := fh.Read(ctx)
|
2019-07-08 19:53:01 -06:00
|
|
|
if err != nil {
|
2019-09-09 22:36:39 -06:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
m, err := r.data.Mapper(fh.Identity().URI)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2019-07-08 19:53:01 -06:00
|
|
|
}
|
|
|
|
filename := filepath.Base(editSpn.Filename())
|
2019-09-06 12:55:14 -06:00
|
|
|
diffEdits, err := source.FromProtocolEdits(m, edits)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
contents := applyEdits(string(data), diffEdits)
|
2019-09-24 15:04:33 -06:00
|
|
|
if len(changes) > 1 {
|
|
|
|
contents = fmt.Sprintf("%s:\n%s", filename, contents)
|
|
|
|
}
|
|
|
|
res = append(res, contents)
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
2019-07-08 19:53:01 -06:00
|
|
|
|
|
|
|
// Sort on filename
|
|
|
|
sort.Strings(res)
|
|
|
|
|
|
|
|
var got string
|
|
|
|
for i, val := range res {
|
|
|
|
if i != 0 {
|
|
|
|
got += "\n"
|
|
|
|
}
|
|
|
|
got += val
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
|
|
|
|
2019-07-08 19:53:01 -06:00
|
|
|
renamed := string(r.data.Golden(tag, spn.URI().Filename(), func() ([]byte, error) {
|
2019-06-18 08:23:37 -06:00
|
|
|
return []byte(got), nil
|
|
|
|
}))
|
|
|
|
|
2019-07-08 19:53:01 -06:00
|
|
|
if renamed != got {
|
|
|
|
t.Errorf("rename failed for %s, expected:\n%v\ngot:\n%v", newText, renamed, got)
|
2019-06-18 08:23:37 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 17:28:08 -06:00
|
|
|
func applyEdits(contents string, edits []diff.TextEdit) string {
|
2019-06-18 08:23:37 -06:00
|
|
|
res := contents
|
|
|
|
|
|
|
|
// Apply the edits from the end of the file forward
|
|
|
|
// to preserve the offsets
|
|
|
|
for i := len(edits) - 1; i >= 0; i-- {
|
|
|
|
edit := edits[i]
|
|
|
|
start := edit.Span.Start().Offset()
|
|
|
|
end := edit.Span.End().Offset()
|
|
|
|
tmp := res[0:start] + edit.NewText
|
|
|
|
res = tmp + res[end:]
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2019-08-22 11:31:03 -06:00
|
|
|
func (r *runner) PrepareRename(t *testing.T, data tests.PrepareRenames) {
|
|
|
|
ctx := context.Background()
|
|
|
|
for src, want := range data {
|
|
|
|
f, err := r.view.GetFile(ctx, src.URI())
|
|
|
|
if err != nil {
|
2019-09-05 18:04:28 -06:00
|
|
|
t.Fatalf("failed for %v: %v", src, err)
|
2019-08-22 11:31:03 -06:00
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
_, srcRng, err := spanToRange(r.data, src)
|
2019-08-22 11:31:03 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// Find the identifier at the position.
|
|
|
|
item, err := source.PrepareRename(ctx, r.view, f.(source.GoFile), srcRng.Start)
|
|
|
|
if err != nil {
|
|
|
|
if want.Text != "" { // expected an ident.
|
|
|
|
t.Errorf("prepare rename failed for %v: got error: %v", src, err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if item == nil {
|
|
|
|
if want.Text != "" {
|
|
|
|
t.Errorf("prepare rename failed for %v: got nil", src)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if want.Text == "" && item != nil {
|
|
|
|
t.Errorf("prepare rename failed for %v: expected nil, got %v", src, item)
|
|
|
|
continue
|
|
|
|
}
|
2019-09-05 18:04:28 -06:00
|
|
|
if protocol.CompareRange(want.Range, item.Range) != 0 {
|
2019-08-22 11:31:03 -06:00
|
|
|
t.Errorf("prepare rename failed: incorrect range got %v want %v", item.Range, want.Range)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 14:04:29 -06:00
|
|
|
func (r *runner) Symbol(t *testing.T, data tests.Symbols) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-05-08 14:04:29 -06:00
|
|
|
for uri, expectedSymbols := range data {
|
|
|
|
f, err := r.view.GetFile(ctx, uri)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", uri, err)
|
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
symbols, err := source.DocumentSymbols(ctx, r.view, f.(source.GoFile))
|
2019-06-21 15:00:02 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("symbols failed for %s: %v", uri, err)
|
|
|
|
}
|
2019-05-08 14:04:29 -06:00
|
|
|
if len(symbols) != len(expectedSymbols) {
|
|
|
|
t.Errorf("want %d top-level symbols in %v, got %d", len(expectedSymbols), uri, len(symbols))
|
|
|
|
continue
|
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
if diff := r.diffSymbols(t, uri, expectedSymbols, symbols); diff != "" {
|
2019-05-08 14:04:29 -06:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:54:05 -06:00
|
|
|
func (r *runner) diffSymbols(t *testing.T, uri span.URI, want, got []protocol.DocumentSymbol) string {
|
2019-05-08 14:04:29 -06:00
|
|
|
sort.Slice(want, func(i, j int) bool { return want[i].Name < want[j].Name })
|
|
|
|
sort.Slice(got, func(i, j int) bool { return got[i].Name < got[j].Name })
|
|
|
|
if len(got) != len(want) {
|
2019-09-05 16:54:05 -06:00
|
|
|
return summarizeSymbols(t, -1, want, got, "different lengths got %v want %v", len(got), len(want))
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
for i, w := range want {
|
|
|
|
g := got[i]
|
|
|
|
if w.Name != g.Name {
|
2019-09-05 16:54:05 -06:00
|
|
|
return summarizeSymbols(t, i, want, got, "incorrect name got %v want %v", g.Name, w.Name)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
if w.Kind != g.Kind {
|
2019-09-05 16:54:05 -06:00
|
|
|
return summarizeSymbols(t, i, want, got, "incorrect kind got %v want %v", g.Kind, w.Kind)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
if protocol.CompareRange(w.SelectionRange, g.SelectionRange) != 0 {
|
|
|
|
return summarizeSymbols(t, i, want, got, "incorrect span got %v want %v", g.SelectionRange, w.SelectionRange)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
if msg := r.diffSymbols(t, uri, w.Children, g.Children); msg != "" {
|
2019-05-08 14:04:29 -06:00
|
|
|
return fmt.Sprintf("children of %s: %s", w.Name, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-09-05 16:54:05 -06:00
|
|
|
func summarizeSymbols(t *testing.T, i int, want, got []protocol.DocumentSymbol, reason string, args ...interface{}) string {
|
2019-05-08 14:04:29 -06:00
|
|
|
msg := &bytes.Buffer{}
|
|
|
|
fmt.Fprint(msg, "document symbols failed")
|
|
|
|
if i >= 0 {
|
|
|
|
fmt.Fprintf(msg, " at %d", i)
|
|
|
|
}
|
|
|
|
fmt.Fprint(msg, " because of ")
|
|
|
|
fmt.Fprintf(msg, reason, args...)
|
|
|
|
fmt.Fprint(msg, ":\nexpected:\n")
|
|
|
|
for _, s := range want {
|
2019-09-05 16:54:05 -06:00
|
|
|
fmt.Fprintf(msg, " %v %v %v\n", s.Name, s.Kind, s.SelectionRange)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
fmt.Fprintf(msg, "got:\n")
|
|
|
|
for _, s := range got {
|
2019-09-05 16:54:05 -06:00
|
|
|
fmt.Fprintf(msg, " %v %v %v\n", s.Name, s.Kind, s.SelectionRange)
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
|
|
|
return msg.String()
|
|
|
|
}
|
|
|
|
|
2019-05-14 19:20:41 -06:00
|
|
|
func (r *runner) SignatureHelp(t *testing.T, data tests.Signatures) {
|
2019-07-10 19:11:23 -06:00
|
|
|
ctx := r.ctx
|
2019-06-28 19:27:41 -06:00
|
|
|
for spn, expectedSignature := range data {
|
2019-05-08 14:04:29 -06:00
|
|
|
f, err := r.view.GetFile(ctx, spn.URI())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
_, rng, err := spanToRange(r.data, spn)
|
2019-07-11 19:05:55 -06:00
|
|
|
if err != nil {
|
2019-08-26 22:26:45 -06:00
|
|
|
t.Fatal(err)
|
2019-07-11 19:05:55 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
gotSignature, err := source.SignatureHelp(ctx, r.view, f.(source.GoFile), rng.Start)
|
2019-05-08 14:04:29 -06:00
|
|
|
if err != nil {
|
2019-06-28 19:27:41 -06:00
|
|
|
// Only fail if we got an error we did not expect.
|
|
|
|
if expectedSignature != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", spn, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if expectedSignature == nil {
|
|
|
|
if gotSignature != nil {
|
|
|
|
t.Errorf("expected no signature, got %v", gotSignature)
|
|
|
|
}
|
|
|
|
continue
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-06-28 19:27:41 -06:00
|
|
|
if diff := diffSignatures(spn, expectedSignature, gotSignature); diff != "" {
|
2019-05-08 14:04:29 -06:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-28 19:27:41 -06:00
|
|
|
func diffSignatures(spn span.Span, want *source.SignatureInformation, got *source.SignatureInformation) string {
|
2019-05-08 14:04:29 -06:00
|
|
|
decorate := func(f string, args ...interface{}) string {
|
|
|
|
return fmt.Sprintf("Invalid signature at %s: %s", spn, fmt.Sprintf(f, args...))
|
|
|
|
}
|
|
|
|
if want.ActiveParameter != got.ActiveParameter {
|
|
|
|
return decorate("wanted active parameter of %d, got %f", want.ActiveParameter, got.ActiveParameter)
|
|
|
|
}
|
|
|
|
if want.Label != got.Label {
|
|
|
|
return decorate("wanted label %q, got %q", want.Label, got.Label)
|
|
|
|
}
|
|
|
|
var paramParts []string
|
|
|
|
for _, p := range got.Parameters {
|
|
|
|
paramParts = append(paramParts, p.Label)
|
|
|
|
}
|
|
|
|
paramsStr := strings.Join(paramParts, ", ")
|
|
|
|
if !strings.Contains(got.Label, paramsStr) {
|
|
|
|
return decorate("expected signature %q to contain params %q", got.Label, paramsStr)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runner) Link(t *testing.T, data tests.Links) {
|
2019-06-28 19:27:41 -06:00
|
|
|
// This is a pure LSP feature, no source level functionality to be tested.
|
2019-05-08 14:04:29 -06:00
|
|
|
}
|
2019-08-26 22:26:45 -06:00
|
|
|
|
2019-09-09 22:36:39 -06:00
|
|
|
func spanToRange(data *tests.Data, spn span.Span) (*protocol.ColumnMapper, protocol.Range, error) {
|
|
|
|
m, err := data.Mapper(spn.URI())
|
2019-08-26 22:26:45 -06:00
|
|
|
if err != nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
return nil, protocol.Range{}, err
|
2019-08-26 22:26:45 -06:00
|
|
|
}
|
2019-09-09 22:36:39 -06:00
|
|
|
srcRng, err := m.Range(spn)
|
2019-08-26 22:26:45 -06:00
|
|
|
if err != nil {
|
2019-09-05 16:54:05 -06:00
|
|
|
return nil, protocol.Range{}, err
|
2019-08-26 22:26:45 -06:00
|
|
|
}
|
2019-09-05 16:54:05 -06:00
|
|
|
return m, srcRng, nil
|
2019-08-26 22:26:45 -06:00
|
|
|
}
|