1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:54:40 -07: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 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" "sync"
"testing" "testing"
"golang.org/x/tools/internal/jsonrpc2"
"golang.org/x/tools/internal/jsonrpc2/servertest" "golang.org/x/tools/internal/jsonrpc2/servertest"
"golang.org/x/tools/internal/lsp/fake" "golang.org/x/tools/internal/lsp/fake"
"golang.org/x/tools/internal/lsp/protocol" "golang.org/x/tools/internal/lsp/protocol"
@ -31,7 +30,6 @@ type Env struct {
Sandbox *fake.Sandbox Sandbox *fake.Sandbox
Editor *fake.Editor Editor *fake.Editor
Server servertest.Connector Server servertest.Connector
Conn jsonrpc2.Conn
// mu guards the fields below, for the purpose of checking conditions on // mu guards the fields below, for the purpose of checking conditions on
// every change to diagnostics. // every change to diagnostics.
@ -114,7 +112,6 @@ func NewEnv(ctx context.Context, t *testing.T, scratch *fake.Sandbox, ts servert
Ctx: ctx, Ctx: ctx,
Sandbox: scratch, Sandbox: scratch,
Server: ts, Server: ts,
Conn: conn,
state: State{ state: State{
diagnostics: make(map[string]*protocol.PublishDiagnosticsParams), diagnostics: make(map[string]*protocol.PublishDiagnosticsParams),
outstandingWork: make(map[string]*workProgress), outstandingWork: make(map[string]*workProgress),

View File

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

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) { func checkIsFatal(t *testing.T, err error) {
t.Helper() t.Helper()
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrClosedPipe) { if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrClosedPipe) {