2018-11-07 13:21:31 -07: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 lsp
|
|
|
|
|
|
|
|
import (
|
2018-11-14 18:42:30 -07:00
|
|
|
"bytes"
|
2018-11-07 13:21:31 -07:00
|
|
|
"context"
|
2018-11-16 16:15:25 -07:00
|
|
|
"fmt"
|
2018-11-07 13:21:31 -07:00
|
|
|
"go/token"
|
2019-04-22 16:15:39 -06:00
|
|
|
"os/exec"
|
2019-03-27 18:00:20 -06:00
|
|
|
"sort"
|
2018-11-07 13:21:31 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"golang.org/x/tools/go/packages/packagestest"
|
2018-12-05 15:00:36 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/cache"
|
2019-04-08 07:22:58 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/diff"
|
2018-11-07 13:21:31 -07:00
|
|
|
"golang.org/x/tools/internal/lsp/protocol"
|
|
|
|
"golang.org/x/tools/internal/lsp/source"
|
2019-04-16 13:47:48 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/tests"
|
2019-03-29 17:04:29 -06:00
|
|
|
"golang.org/x/tools/internal/lsp/xlog"
|
2019-02-19 19:11:15 -07:00
|
|
|
"golang.org/x/tools/internal/span"
|
2018-11-07 13:21:31 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLSP(t *testing.T) {
|
|
|
|
packagestest.TestAll(t, testLSP)
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
type runner struct {
|
|
|
|
server *Server
|
|
|
|
data *tests.Data
|
|
|
|
}
|
|
|
|
|
2018-11-07 13:21:31 -07:00
|
|
|
func testLSP(t *testing.T, exporter packagestest.Exporter) {
|
2019-03-29 17:04:29 -06:00
|
|
|
ctx := context.Background()
|
2019-04-16 14:19:03 -06:00
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
data := tests.Load(t, exporter, "testdata")
|
|
|
|
defer data.Exported.Cleanup()
|
2018-11-07 13:21:31 -07:00
|
|
|
|
2019-03-29 17:04:29 -06:00
|
|
|
log := xlog.New(xlog.StdSink{})
|
2019-04-16 13:47:48 -06:00
|
|
|
r := &runner{
|
|
|
|
server: &Server{
|
|
|
|
views: []*cache.View{cache.NewView(ctx, log, "lsp_test", span.FileURI(data.Config.Dir), &data.Config)},
|
|
|
|
undelivered: make(map[span.URI][]source.Diagnostic),
|
|
|
|
log: log,
|
2018-11-14 17:25:49 -07:00
|
|
|
},
|
2019-04-16 13:47:48 -06:00
|
|
|
data: data,
|
|
|
|
}
|
|
|
|
tests.Run(t, r, data)
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
2018-11-14 22:05:30 -07:00
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Diagnostics(t *testing.T, data tests.Diagnostics) {
|
|
|
|
v := r.server.views[0]
|
|
|
|
for uri, want := range data {
|
|
|
|
results, err := source.Diagnostics(context.Background(), v, uri)
|
2019-03-14 14:55:23 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
got := results[uri]
|
2019-02-19 19:11:15 -07:00
|
|
|
if diff := diffDiagnostics(uri, want, got); diff != "" {
|
2018-12-18 13:46:14 -07:00
|
|
|
t.Error(diff)
|
2018-11-20 14:05:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func sortDiagnostics(d []source.Diagnostic) {
|
2019-04-16 16:03:59 -06:00
|
|
|
sort.Slice(d, func(i int, j int) bool {
|
2019-04-16 13:47:48 -06:00
|
|
|
if r := span.Compare(d[i].Span, d[j].Span); r != 0 {
|
|
|
|
return r < 0
|
2019-04-16 16:03:59 -06:00
|
|
|
}
|
|
|
|
return d[i].Message < d[j].Message
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:46:14 -07:00
|
|
|
// diffDiagnostics prints the diff between expected and actual diagnostics test
|
|
|
|
// results.
|
2019-04-16 13:47:48 -06:00
|
|
|
func diffDiagnostics(uri span.URI, want, got []source.Diagnostic) string {
|
2019-04-16 16:03:59 -06:00
|
|
|
sortDiagnostics(want)
|
|
|
|
sortDiagnostics(got)
|
2018-12-18 13:46:14 -07:00
|
|
|
if len(got) != len(want) {
|
2019-04-16 16:03:59 -06:00
|
|
|
return summarizeDiagnostics(-1, want, got, "different lengths got %v want %v", len(got), len(want))
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
for i, w := range want {
|
|
|
|
g := got[i]
|
|
|
|
if w.Message != g.Message {
|
2019-04-16 16:03:59 -06:00
|
|
|
return summarizeDiagnostics(i, want, got, "incorrect Message got %v want %v", g.Message, w.Message)
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
if span.ComparePoint(w.Start(), g.Start()) != 0 {
|
|
|
|
return summarizeDiagnostics(i, want, got, "incorrect Start got %v want %v", g.Start(), w.Start())
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
// Special case for diagnostics on parse errors.
|
2019-02-19 19:11:15 -07:00
|
|
|
if strings.Contains(string(uri), "noparse") {
|
2019-04-16 13:47:48 -06:00
|
|
|
if span.ComparePoint(g.Start(), g.End()) != 0 || span.ComparePoint(w.Start(), g.End()) != 0 {
|
|
|
|
return summarizeDiagnostics(i, want, got, "incorrect End got %v want %v", g.End(), w.Start())
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
} else if !g.IsPoint() { // Accept any 'want' range if the diagnostic returns a zero-length range.
|
|
|
|
if span.ComparePoint(w.End(), g.End()) != 0 {
|
|
|
|
return summarizeDiagnostics(i, want, got, "incorrect End got %v want %v", g.End(), w.End())
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if w.Severity != g.Severity {
|
2019-04-16 16:03:59 -06:00
|
|
|
return summarizeDiagnostics(i, want, got, "incorrect Severity got %v want %v", g.Severity, w.Severity)
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
if w.Source != g.Source {
|
2019-04-16 16:03:59 -06:00
|
|
|
return summarizeDiagnostics(i, want, got, "incorrect Source got %v want %v", g.Source, w.Source)
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
2019-04-16 16:03:59 -06:00
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func summarizeDiagnostics(i int, want []source.Diagnostic, got []source.Diagnostic, reason string, args ...interface{}) string {
|
2018-12-18 13:46:14 -07:00
|
|
|
msg := &bytes.Buffer{}
|
2019-04-16 16:03:59 -06:00
|
|
|
fmt.Fprint(msg, "diagnostics 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")
|
2018-12-18 13:46:14 -07:00
|
|
|
for _, d := range want {
|
|
|
|
fmt.Fprintf(msg, " %v\n", d)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(msg, "got:\n")
|
|
|
|
for _, d := range got {
|
|
|
|
fmt.Fprintf(msg, " %v\n", d)
|
|
|
|
}
|
|
|
|
return msg.String()
|
2018-11-20 14:05:10 -07:00
|
|
|
}
|
|
|
|
|
2019-04-28 21:19:54 -06:00
|
|
|
func (r *runner) Completion(t *testing.T, data tests.Completions, snippets tests.CompletionSnippets, items tests.CompletionItems) {
|
2019-04-16 13:47:48 -06:00
|
|
|
for src, itemList := range data {
|
|
|
|
var want []source.CompletionItem
|
2018-11-14 22:05:30 -07:00
|
|
|
for _, pos := range itemList {
|
|
|
|
want = append(want, *items[pos])
|
|
|
|
}
|
2019-04-28 21:19:54 -06:00
|
|
|
|
|
|
|
list := r.runCompletion(t, src)
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
wantBuiltins := strings.Contains(string(src.URI()), "builtins")
|
2018-11-21 21:51:01 -07:00
|
|
|
var got []protocol.CompletionItem
|
|
|
|
for _, item := range list.Items {
|
2019-01-15 15:04:34 -07:00
|
|
|
if !wantBuiltins && isBuiltin(item) {
|
2018-11-21 21:51:01 -07:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
got = append(got, item)
|
|
|
|
}
|
2019-01-17 09:59:05 -07:00
|
|
|
if diff := diffCompletionItems(t, src, want, got); diff != "" {
|
2019-04-23 16:00:40 -06:00
|
|
|
t.Errorf("%s: %s", src, diff)
|
2018-11-14 22:05:30 -07:00
|
|
|
}
|
2018-11-07 13:21:31 -07:00
|
|
|
}
|
2019-04-23 16:00:40 -06:00
|
|
|
// Make sure we don't crash completing the first position in file set.
|
2019-04-28 21:19:54 -06:00
|
|
|
firstPos, err := span.NewRange(r.data.Exported.ExpectFileSet, 1, 2).Span()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
_ = r.runCompletion(t, firstPos)
|
|
|
|
|
|
|
|
r.checkCompletionSnippets(t, snippets, items)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *runner) checkCompletionSnippets(t *testing.T, data tests.CompletionSnippets, items tests.CompletionItems) {
|
|
|
|
origPlaceHolders := r.server.usePlaceholders
|
|
|
|
origTextFormat := r.server.insertTextFormat
|
|
|
|
defer func() {
|
|
|
|
r.server.usePlaceholders = origPlaceHolders
|
|
|
|
r.server.insertTextFormat = origTextFormat
|
|
|
|
}()
|
|
|
|
|
|
|
|
r.server.insertTextFormat = protocol.SnippetTextFormat
|
|
|
|
for _, usePlaceholders := range []bool{true, false} {
|
|
|
|
r.server.usePlaceholders = usePlaceholders
|
|
|
|
|
|
|
|
for src, want := range data {
|
|
|
|
list := r.runCompletion(t, src)
|
|
|
|
|
|
|
|
wantCompletion := items[want.CompletionItem]
|
|
|
|
var gotItem *protocol.CompletionItem
|
|
|
|
for _, item := range list.Items {
|
|
|
|
if item.Label == wantCompletion.Label {
|
|
|
|
gotItem = &item
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if gotItem == nil {
|
|
|
|
t.Fatalf("%s: couldn't find completion matching %q", src.URI(), wantCompletion.Label)
|
|
|
|
}
|
|
|
|
|
|
|
|
var expected string
|
|
|
|
if usePlaceholders {
|
|
|
|
expected = want.PlaceholderSnippet
|
|
|
|
} else {
|
|
|
|
expected = want.PlainSnippet
|
|
|
|
}
|
|
|
|
|
|
|
|
if expected != gotItem.TextEdit.NewText {
|
|
|
|
t.Errorf("%s: expected snippet %q, got %q", src, expected, gotItem.TextEdit.NewText)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-04-23 16:00:40 -06:00
|
|
|
|
2019-04-28 21:19:54 -06:00
|
|
|
func (r *runner) runCompletion(t *testing.T, src span.Span) *protocol.CompletionList {
|
|
|
|
t.Helper()
|
|
|
|
list, err := r.server.Completion(context.Background(), &protocol.CompletionParams{
|
2019-04-23 16:00:40 -06:00
|
|
|
TextDocumentPositionParams: protocol.TextDocumentPositionParams{
|
|
|
|
TextDocument: protocol.TextDocumentIdentifier{
|
2019-04-28 21:19:54 -06:00
|
|
|
URI: protocol.NewURI(src.URI()),
|
2019-04-23 16:00:40 -06:00
|
|
|
},
|
|
|
|
Position: protocol.Position{
|
2019-04-28 21:19:54 -06:00
|
|
|
Line: float64(src.Start().Line() - 1),
|
|
|
|
Character: float64(src.Start().Column() - 1),
|
2019-04-23 16:00:40 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-04-28 21:19:54 -06:00
|
|
|
return list
|
2018-11-07 13:21:31 -07:00
|
|
|
}
|
2018-11-14 18:42:30 -07:00
|
|
|
|
2019-01-15 15:04:34 -07:00
|
|
|
func isBuiltin(item protocol.CompletionItem) bool {
|
|
|
|
// If a type has no detail, it is a builtin type.
|
2019-03-11 10:17:05 -06:00
|
|
|
if item.Detail == "" && item.Kind == protocol.TypeParameterCompletion {
|
2019-01-15 15:04:34 -07:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
// Remaining builtin constants, variables, interfaces, and functions.
|
|
|
|
trimmed := item.Label
|
|
|
|
if i := strings.Index(trimmed, "("); i >= 0 {
|
|
|
|
trimmed = trimmed[:i]
|
|
|
|
}
|
|
|
|
switch trimmed {
|
|
|
|
case "append", "cap", "close", "complex", "copy", "delete",
|
|
|
|
"error", "false", "imag", "iota", "len", "make", "new",
|
|
|
|
"nil", "panic", "print", "println", "real", "recover", "true":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-12-18 13:46:14 -07:00
|
|
|
// diffCompletionItems prints the diff between expected and actual completion
|
|
|
|
// test results.
|
2019-04-16 13:47:48 -06:00
|
|
|
func diffCompletionItems(t *testing.T, spn span.Span, want []source.CompletionItem, got []protocol.CompletionItem) string {
|
2018-12-18 13:46:14 -07:00
|
|
|
if len(got) != len(want) {
|
2019-04-16 16:03:59 -06:00
|
|
|
return summarizeCompletionItems(-1, want, got, "different lengths got %v want %v", len(got), len(want))
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
for i, w := range want {
|
|
|
|
g := got[i]
|
|
|
|
if w.Label != g.Label {
|
2019-04-16 16:03:59 -06:00
|
|
|
return summarizeCompletionItems(i, want, got, "incorrect Label got %v want %v", g.Label, w.Label)
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
if w.Detail != g.Detail {
|
2019-04-16 16:03:59 -06:00
|
|
|
return summarizeCompletionItems(i, want, got, "incorrect Detail got %v want %v", g.Detail, w.Detail)
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
if wkind := toProtocolCompletionItemKind(w.Kind); wkind != g.Kind {
|
|
|
|
return summarizeCompletionItems(i, want, got, "incorrect Kind got %v want %v", g.Kind, wkind)
|
2018-12-18 13:46:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
2019-04-16 16:03:59 -06:00
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func summarizeCompletionItems(i int, want []source.CompletionItem, got []protocol.CompletionItem, reason string, args ...interface{}) string {
|
2018-12-18 13:46:14 -07:00
|
|
|
msg := &bytes.Buffer{}
|
2019-04-16 16:03:59 -06:00
|
|
|
fmt.Fprint(msg, "completion 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")
|
2018-12-18 13:46:14 -07:00
|
|
|
for _, d := range want {
|
|
|
|
fmt.Fprintf(msg, " %v\n", d)
|
|
|
|
}
|
|
|
|
fmt.Fprintf(msg, "got:\n")
|
|
|
|
for _, d := range got {
|
|
|
|
fmt.Fprintf(msg, " %v\n", d)
|
|
|
|
}
|
|
|
|
return msg.String()
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Format(t *testing.T, data tests.Formats) {
|
2019-02-19 19:11:15 -07:00
|
|
|
ctx := context.Background()
|
2019-04-22 16:15:39 -06:00
|
|
|
for _, spn := range data {
|
|
|
|
uri := spn.URI()
|
|
|
|
filename, err := uri.Filename()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-05-01 17:16:03 -06:00
|
|
|
gofmted := string(r.data.Golden("gofmt", filename, func() ([]byte, error) {
|
2019-04-22 16:15:39 -06:00
|
|
|
cmd := exec.Command("gofmt", filename)
|
2019-05-01 17:16:03 -06:00
|
|
|
out, _ := cmd.Output() // ignore error, sometimes we have intentionally ungofmt-able files
|
|
|
|
return out, nil
|
2019-04-22 16:15:39 -06:00
|
|
|
}))
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
edits, err := r.server.Formatting(context.Background(), &protocol.DocumentFormattingParams{
|
2018-11-14 18:42:30 -07:00
|
|
|
TextDocument: protocol.TextDocumentIdentifier{
|
2019-02-19 19:11:15 -07:00
|
|
|
URI: protocol.NewURI(uri),
|
2018-11-14 18:42:30 -07:00
|
|
|
},
|
|
|
|
})
|
2019-01-17 09:59:05 -07:00
|
|
|
if err != nil {
|
2018-11-14 18:42:30 -07:00
|
|
|
if gofmted != "" {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2018-11-13 09:13:53 -07:00
|
|
|
continue
|
2018-11-14 18:42:30 -07:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
_, m, err := newColumnMap(ctx, r.server.findView(ctx, uri), uri)
|
2019-01-17 09:59:05 -07:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
2019-04-08 07:22:58 -06:00
|
|
|
sedits, err := FromProtocolEdits(m, edits)
|
2019-03-11 14:41:00 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
2019-01-17 09:59:05 -07:00
|
|
|
}
|
2019-04-08 07:22:58 -06:00
|
|
|
ops := source.EditsToDiff(sedits)
|
|
|
|
got := strings.Join(diff.ApplyEdits(diff.SplitLines(string(m.Content)), ops), "")
|
2019-01-17 09:59:05 -07:00
|
|
|
if gofmted != got {
|
2019-04-08 07:22:58 -06:00
|
|
|
t.Errorf("format failed for %s, expected:\n%v\ngot:\n%v", filename, gofmted, got)
|
2018-11-14 18:42:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Definition(t *testing.T, data tests.Definitions) {
|
|
|
|
for _, d := range data {
|
|
|
|
sm := r.mapper(d.Src.URI())
|
|
|
|
loc, err := sm.Location(d.Src)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", d.Src, err)
|
|
|
|
}
|
2018-12-03 15:14:30 -07:00
|
|
|
params := &protocol.TextDocumentPositionParams{
|
2019-04-16 13:47:48 -06:00
|
|
|
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
|
|
|
|
Position: loc.Range.Start,
|
2018-12-03 15:14:30 -07:00
|
|
|
}
|
|
|
|
var locs []protocol.Location
|
2019-04-29 18:58:12 -06:00
|
|
|
var hover *protocol.Hover
|
2019-04-16 13:47:48 -06:00
|
|
|
if d.IsType {
|
|
|
|
locs, err = r.server.TypeDefinition(context.Background(), params)
|
2018-12-03 15:14:30 -07:00
|
|
|
} else {
|
2019-04-16 13:47:48 -06:00
|
|
|
locs, err = r.server.Definition(context.Background(), params)
|
2019-04-29 18:58:12 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", d.Src, err)
|
|
|
|
}
|
|
|
|
hover, err = r.server.Hover(context.Background(), params)
|
2018-12-03 15:14:30 -07:00
|
|
|
}
|
2018-11-14 19:11:16 -07:00
|
|
|
if err != nil {
|
2019-04-16 13:47:48 -06:00
|
|
|
t.Fatalf("failed for %v: %v", d.Src, err)
|
2018-11-14 19:11:16 -07:00
|
|
|
}
|
|
|
|
if len(locs) != 1 {
|
|
|
|
t.Errorf("got %d locations for definition, expected 1", len(locs))
|
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
locURI := span.NewURI(locs[0].URI)
|
|
|
|
lm := r.mapper(locURI)
|
|
|
|
if def, err := lm.Span(locs[0]); err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", locs[0], err)
|
|
|
|
} else if def != d.Def {
|
|
|
|
t.Errorf("for %v got %v want %v", d.Src, def, d.Def)
|
2018-11-14 19:11:16 -07:00
|
|
|
}
|
2019-04-29 18:58:12 -06:00
|
|
|
if hover != nil {
|
2019-05-01 17:16:03 -06:00
|
|
|
tag := fmt.Sprintf("%s-hover", d.Name)
|
|
|
|
filename, err := d.Src.URI().Filename()
|
2019-04-29 18:58:12 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", d.Def, err)
|
|
|
|
}
|
2019-05-01 17:16:03 -06:00
|
|
|
expectHover := string(r.data.Golden(tag, filename, func() ([]byte, error) {
|
|
|
|
return []byte(hover.Contents.Value), nil
|
2019-04-29 18:58:12 -06:00
|
|
|
}))
|
|
|
|
if hover.Contents.Value != expectHover {
|
|
|
|
t.Errorf("for %v got %q want %q", d.Src, hover.Contents.Value, expectHover)
|
|
|
|
}
|
|
|
|
}
|
2018-11-14 19:11:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Highlight(t *testing.T, data tests.Highlights) {
|
|
|
|
for name, locations := range data {
|
|
|
|
m := r.mapper(locations[0].URI())
|
|
|
|
loc, err := m.Location(locations[0])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", locations[0], err)
|
|
|
|
}
|
2019-03-25 18:56:05 -06:00
|
|
|
params := &protocol.TextDocumentPositionParams{
|
2019-04-16 13:47:48 -06:00
|
|
|
TextDocument: protocol.TextDocumentIdentifier{URI: loc.URI},
|
|
|
|
Position: loc.Range.Start,
|
2019-03-25 18:56:05 -06:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
highlights, err := r.server.DocumentHighlight(context.Background(), params)
|
2019-03-25 18:56:05 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(highlights) != len(locations) {
|
|
|
|
t.Fatalf("got %d highlights for %s, expected %d", len(highlights), name, len(locations))
|
|
|
|
}
|
|
|
|
for i := range highlights {
|
2019-04-16 13:47:48 -06:00
|
|
|
if h, err := m.RangeSpan(highlights[i].Range); err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", highlights[i], err)
|
|
|
|
} else if h != locations[i] {
|
|
|
|
t.Errorf("want %v, got %v\n", locations[i], h)
|
2019-03-25 18:56:05 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Symbol(t *testing.T, data tests.Symbols) {
|
|
|
|
for uri, expectedSymbols := range data {
|
2019-03-27 18:00:20 -06:00
|
|
|
params := &protocol.DocumentSymbolParams{
|
|
|
|
TextDocument: protocol.TextDocumentIdentifier{
|
|
|
|
URI: string(uri),
|
|
|
|
},
|
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
symbols, err := r.server.DocumentSymbol(context.Background(), params)
|
2019-03-27 18:00:20 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(symbols) != len(expectedSymbols) {
|
2019-03-30 14:18:22 -06:00
|
|
|
t.Errorf("want %d top-level symbols in %v, got %d", len(expectedSymbols), uri, len(symbols))
|
2019-03-27 18:00:20 -06:00
|
|
|
continue
|
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
if diff := r.diffSymbols(uri, expectedSymbols, symbols); diff != "" {
|
internal/lsp: improve signatureHelp in various cases
- show signature for function calls whose function expression is not
an object (e.g. the second call in foo()()). since the function name
is not available, we use the generic "func"
- only provide signature help when the position is on or within the
call expression parens. this is consistent with the one other lsp
server i tried (java). this improves the gopls experience in emacs
where lsp-mode is constantly calling "hover" and
"signatureHelp" ("hover" should be preferred unless you are inside
the function params list)
- use the entire signature type string as the label since that includes
the return values, which are useful to see
- don't qualify the function name with its package. it looks funny to
see "bytes.Cap()" as the help when you are in a call
to (*bytes.Buffer).Cap(). it could be useful to include invocant
type info, but leave it out for now since signature help is meant to
focus on the function parameters.
- don't turn variadic args "foo ...int" into "foo []int" for the
parameter information (i.e. maintain it as "foo ...int")
- when determining active parameter, count the space before a
parameter name as being part of that parameter (e.g. the space
before "b" in "func(a int, b int)")
- handle variadic params when determining the active param (i.e.
highlight "foo(a int, *b ...string*)" on signature help for final
param in `foo(123, "a", "b", "c")`
- don't generate an extra space in formatParams() for unnamed
arguments
I also tweaked the signatureHelp server log message to include the
error message itself, and populated the server's logger in lsp_test.go
to aid in development.
Fixes golang/go#31448
Change-Id: Iefe0e1e3c531d17197c0fa997b949174475a276c
GitHub-Last-Rev: 5c0b8ebd87a8c05d5d8f519ea096f94e89c77e2c
GitHub-Pull-Request: golang/tools#82
Reviewed-on: https://go-review.googlesource.com/c/tools/+/172439
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-04-16 22:54:13 -06:00
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) diffSymbols(uri span.URI, want []source.Symbol, got []protocol.DocumentSymbol) string {
|
2019-04-16 14:47:01 -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 })
|
2019-04-16 13:47:48 -06:00
|
|
|
m := r.mapper(uri)
|
2019-03-30 14:18:22 -06:00
|
|
|
if len(got) != len(want) {
|
2019-04-16 14:47:01 -06:00
|
|
|
return summarizeSymbols(-1, want, got, "different lengths got %v want %v", len(got), len(want))
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
|
|
|
for i, w := range want {
|
|
|
|
g := got[i]
|
|
|
|
if w.Name != g.Name {
|
2019-04-16 14:47:01 -06:00
|
|
|
return summarizeSymbols(i, want, got, "incorrect name got %v want %v", g.Name, w.Name)
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
if wkind := toProtocolSymbolKind(w.Kind); wkind != g.Kind {
|
|
|
|
return summarizeSymbols(i, want, got, "incorrect kind got %v want %v", g.Kind, wkind)
|
|
|
|
}
|
|
|
|
spn, err := m.RangeSpan(g.SelectionRange)
|
|
|
|
if err != nil {
|
|
|
|
return summarizeSymbols(i, want, got, "%v", err)
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
if w.SelectionSpan != spn {
|
|
|
|
return summarizeSymbols(i, want, got, "incorrect span got %v want %v", spn, w.SelectionSpan)
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
if msg := r.diffSymbols(uri, w.Children, g.Children); msg != "" {
|
2019-03-30 14:18:22 -06:00
|
|
|
return fmt.Sprintf("children of %s: %s", w.Name, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
2019-04-16 14:47:01 -06:00
|
|
|
}
|
2019-03-30 14:18:22 -06:00
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func summarizeSymbols(i int, want []source.Symbol, got []protocol.DocumentSymbol, reason string, args ...interface{}) string {
|
2019-03-30 14:18:22 -06:00
|
|
|
msg := &bytes.Buffer{}
|
2019-04-16 14:47:01 -06:00
|
|
|
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")
|
2019-03-30 14:18:22 -06:00
|
|
|
for _, s := range want {
|
2019-04-16 13:47:48 -06:00
|
|
|
fmt.Fprintf(msg, " %v %v %v\n", s.Name, s.Kind, s.SelectionSpan)
|
2019-03-30 14:18:22 -06:00
|
|
|
}
|
|
|
|
fmt.Fprintf(msg, "got:\n")
|
|
|
|
for _, s := range got {
|
|
|
|
fmt.Fprintf(msg, " %v %v %v\n", s.Name, s.Kind, s.SelectionRange)
|
|
|
|
}
|
|
|
|
return msg.String()
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) Signature(t *testing.T, data tests.Signatures) {
|
|
|
|
for spn, expectedSignatures := range data {
|
|
|
|
m := r.mapper(spn.URI())
|
|
|
|
loc, err := m.Location(spn)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed for %v: %v", loc, err)
|
|
|
|
}
|
|
|
|
gotSignatures, err := r.server.SignatureHelp(context.Background(), &protocol.TextDocumentPositionParams{
|
|
|
|
TextDocument: protocol.TextDocumentIdentifier{
|
|
|
|
URI: protocol.NewURI(spn.URI()),
|
|
|
|
},
|
|
|
|
Position: loc.Range.Start,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff := diffSignatures(spn, expectedSignatures, gotSignatures); diff != "" {
|
|
|
|
t.Error(diff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func diffSignatures(spn span.Span, want source.SignatureInformation, got *protocol.SignatureHelp) string {
|
|
|
|
decorate := func(f string, args ...interface{}) string {
|
|
|
|
return fmt.Sprintf("Invalid signature at %s: %s", spn, fmt.Sprintf(f, args...))
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(got.Signatures) != 1 {
|
|
|
|
return decorate("wanted 1 signature, got %d", len(got.Signatures))
|
|
|
|
}
|
|
|
|
|
|
|
|
if got.ActiveSignature != 0 {
|
|
|
|
return decorate("wanted active signature of 0, got %f", got.ActiveSignature)
|
|
|
|
}
|
|
|
|
|
|
|
|
if want.ActiveParameter != int(got.ActiveParameter) {
|
|
|
|
return decorate("wanted active parameter of %d, got %f", want.ActiveParameter, got.ActiveParameter)
|
|
|
|
}
|
|
|
|
|
|
|
|
gotSig := got.Signatures[int(got.ActiveSignature)]
|
|
|
|
|
|
|
|
if want.Label != gotSig.Label {
|
|
|
|
return decorate("wanted label %q, got %q", want.Label, gotSig.Label)
|
|
|
|
}
|
|
|
|
|
|
|
|
var paramParts []string
|
|
|
|
for _, p := range gotSig.Parameters {
|
|
|
|
paramParts = append(paramParts, p.Label)
|
|
|
|
}
|
|
|
|
paramsStr := strings.Join(paramParts, ", ")
|
|
|
|
if !strings.Contains(gotSig.Label, paramsStr) {
|
|
|
|
return decorate("expected signature %q to contain params %q", gotSig.Label, paramsStr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-04-24 09:33:45 -06:00
|
|
|
func (r *runner) Link(t *testing.T, data tests.Links) {
|
|
|
|
for uri, wantLinks := range data {
|
|
|
|
m := r.mapper(uri)
|
|
|
|
gotLinks, err := r.server.DocumentLink(context.Background(), &protocol.DocumentLinkParams{
|
|
|
|
TextDocument: protocol.TextDocumentIdentifier{
|
|
|
|
URI: protocol.NewURI(uri),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
links := make(map[span.Span]string, len(wantLinks))
|
|
|
|
for _, link := range wantLinks {
|
|
|
|
links[link.Src] = link.Target
|
|
|
|
}
|
|
|
|
for _, link := range gotLinks {
|
|
|
|
spn, err := m.RangeSpan(link.Range)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if target, ok := links[spn]; ok {
|
|
|
|
delete(links, spn)
|
|
|
|
if target != link.Target {
|
|
|
|
t.Errorf("for %v want %v, got %v\n", spn, link.Target, target)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
t.Errorf("unexpected link %v:%v\n", spn, link.Target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for spn, target := range links {
|
|
|
|
t.Errorf("missing link %v:%v\n", spn, target)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-16 13:47:48 -06:00
|
|
|
func (r *runner) mapper(uri span.URI) *protocol.ColumnMapper {
|
|
|
|
fname, err := uri.Filename()
|
2019-03-15 11:19:43 -06:00
|
|
|
if err != nil {
|
2019-04-16 13:47:48 -06:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
fset := r.data.Exported.ExpectFileSet
|
|
|
|
var f *token.File
|
|
|
|
fset.Iterate(func(check *token.File) bool {
|
|
|
|
if check.Name() == fname {
|
|
|
|
f = check
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if f == nil {
|
|
|
|
return nil
|
2019-03-15 11:19:43 -06:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
content, err := r.data.Exported.FileContents(f.Name())
|
2019-02-19 19:11:15 -07:00
|
|
|
if err != nil {
|
2019-04-16 13:47:48 -06:00
|
|
|
return nil
|
2019-02-19 19:11:15 -07:00
|
|
|
}
|
2019-04-16 13:47:48 -06:00
|
|
|
return protocol.NewColumnMapper(uri, fset, f, content)
|
2018-11-14 19:11:16 -07:00
|
|
|
}
|
2019-02-17 23:00:10 -07:00
|
|
|
|
|
|
|
func TestBytesOffset(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
text string
|
|
|
|
pos protocol.Position
|
|
|
|
want int
|
|
|
|
}{
|
|
|
|
{text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 0}, want: 0},
|
|
|
|
{text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 1}, want: 1},
|
|
|
|
{text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 2}, want: 1},
|
|
|
|
{text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 3}, want: 5},
|
2019-03-11 14:42:38 -06:00
|
|
|
{text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 4}, want: 6},
|
|
|
|
{text: `a𐐀b`, pos: protocol.Position{Line: 0, Character: 5}, want: -1},
|
2019-02-17 23:00:10 -07:00
|
|
|
{text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 3}, want: 3},
|
|
|
|
{text: "aaa\nbbb\n", pos: protocol.Position{Line: 0, Character: 4}, want: -1},
|
|
|
|
{text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 0}, want: 4},
|
|
|
|
{text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 3}, want: 7},
|
|
|
|
{text: "aaa\nbbb\n", pos: protocol.Position{Line: 1, Character: 4}, want: -1},
|
2019-03-11 14:42:38 -06:00
|
|
|
{text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8},
|
|
|
|
{text: "aaa\nbbb\n", pos: protocol.Position{Line: 2, Character: 1}, want: -1},
|
2019-02-17 23:00:10 -07:00
|
|
|
{text: "aaa\nbbb\n\n", pos: protocol.Position{Line: 2, Character: 0}, want: 8},
|
|
|
|
}
|
|
|
|
|
2019-02-19 19:11:15 -07:00
|
|
|
for i, test := range tests {
|
|
|
|
fname := fmt.Sprintf("test %d", i)
|
|
|
|
fset := token.NewFileSet()
|
|
|
|
f := fset.AddFile(fname, -1, len(test.text))
|
|
|
|
f.SetLinesForContent([]byte(test.text))
|
|
|
|
mapper := protocol.NewColumnMapper(span.FileURI(fname), fset, f, []byte(test.text))
|
2019-03-15 11:19:43 -06:00
|
|
|
got, err := mapper.Point(test.pos)
|
|
|
|
if err != nil && test.want != -1 {
|
|
|
|
t.Errorf("unexpected error: %v", err)
|
|
|
|
}
|
|
|
|
if err == nil && got.Offset() != test.want {
|
|
|
|
t.Errorf("want %d for %q(Line:%d,Character:%d), but got %d", test.want, test.text, int(test.pos.Line), int(test.pos.Character), got.Offset())
|
2019-02-17 23:00:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|