1
0
mirror of https://github.com/golang/go synced 2024-09-30 14:28:33 -06:00

internal/lsp: change the hover test to use normal editor methods

It was directly generating messages and sending them on the conn, now it
just uses an editor method like all the other tests.
It was also broken because it never opened the file it was hovering in, so I am
not sure it was testing anything useful before.

Change-Id: I7a1b444015c95c82a0a137d3bb1da661ed9331af
Reviewed-on: https://go-review.googlesource.com/c/tools/+/232983
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Ian Cottrell 2020-05-08 13:42:14 -04:00
parent ef124de36d
commit 0310561d58
4 changed files with 34 additions and 15 deletions

View File

@ -721,3 +721,22 @@ func (e *Editor) CodeAction(ctx context.Context, path string) ([]protocol.CodeAc
}
return lens, nil
}
// Hover triggers a hover at the given position in an open buffer.
func (e *Editor) Hover(ctx context.Context, path string, pos Pos) (*protocol.MarkupContent, Pos, error) {
if err := e.checkBufferPosition(path, pos); err != nil {
return nil, Pos{}, err
}
params := &protocol.HoverParams{}
params.TextDocument.URI = e.sandbox.Workdir.URI(path)
params.Position = pos.toProtocolPosition()
resp, err := e.Server.Hover(ctx, params)
if err != nil {
return nil, Pos{}, fmt.Errorf("hover: %w", err)
}
if resp == nil {
return nil, Pos{}, nil
}
return &resp.Contents, fromProtocolPosition(resp.Range.Start), nil
}

View File

@ -12,7 +12,6 @@ import (
"sync"
"testing"
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/jsonrpc2/servertest"
"golang.org/x/tools/internal/lsp/fake"
"golang.org/x/tools/internal/lsp/protocol"
@ -31,7 +30,6 @@ type Env struct {
Sandbox *fake.Sandbox
Editor *fake.Editor
Server servertest.Connector
Conn jsonrpc2.Conn
// mu guards the fields below, for the purpose of checking conditions on
// every change to diagnostics.
@ -114,7 +112,6 @@ func NewEnv(ctx context.Context, t *testing.T, scratch *fake.Sandbox, ts servert
Ctx: ctx,
Sandbox: scratch,
Server: ts,
Conn: conn,
state: State{
diagnostics: make(map[string]*protocol.PublishDiagnosticsParams),
outstandingWork: make(map[string]*workProgress),

View File

@ -5,10 +5,9 @@
package regtest
import (
"encoding/json"
"testing"
"golang.org/x/tools/internal/lsp/protocol"
"golang.org/x/tools/internal/lsp/fake"
)
const simpleProgram = `
@ -28,16 +27,10 @@ func main() {
func TestHoverSerialization(t *testing.T) {
runner.Run(t, simpleProgram, func(t *testing.T, env *Env) {
// Hover on an empty line.
params := protocol.HoverParams{}
params.TextDocument.URI = env.Sandbox.Workdir.URI("main.go")
params.Position.Line = 3
params.Position.Character = 0
var resp json.RawMessage
if err := protocol.Call(env.Ctx, env.Conn, "textDocument/hover", &params, &resp); err != nil {
t.Fatal(err)
}
if len(string(resp)) > 0 {
t.Errorf("got non-empty response for empty hover: %v", string(resp))
env.OpenFile("main.go")
content, pos := env.Hover("main.go", fake.Pos{Line: 3, Column: 0})
if content != nil {
t.Errorf("got non-empty response for empty hover: %v: %v", pos, *content)
}
})
}

View File

@ -145,6 +145,16 @@ func (e *Env) ApplyQuickFixes(path string, diagnostics []protocol.Diagnostic) {
}
}
// Hover in the editor, calling t.Fatal on any error.
func (e *Env) Hover(name string, pos fake.Pos) (*protocol.MarkupContent, fake.Pos) {
e.T.Helper()
c, p, err := e.Editor.Hover(e.Ctx, name, pos)
if err != nil {
e.T.Fatal(err)
}
return c, p
}
func checkIsFatal(t *testing.T, err error) {
t.Helper()
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrClosedPipe) {