internal/lsp: add InlayHint regtests

Add regtests for inlay hints to verify we can turn on
and off different inlay hints.

Change-Id: Id88450c40c048b6c2544d22a0d3eadb57b70a723
Reviewed-on: https://go-review.googlesource.com/c/tools/+/411911
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Suzy Mueller <suzmue@golang.org>
This commit is contained in:
Suzy Mueller 2022-06-14 15:18:07 -04:00
parent 2a900561e7
commit e5b3324997
3 changed files with 104 additions and 0 deletions

View File

@ -0,0 +1,72 @@
// Copyright 2022 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 inlayHint
import (
"testing"
"golang.org/x/tools/gopls/internal/hooks"
"golang.org/x/tools/internal/lsp/bug"
. "golang.org/x/tools/internal/lsp/regtest"
"golang.org/x/tools/internal/lsp/source"
"golang.org/x/tools/internal/testenv"
)
func TestMain(m *testing.M) {
bug.PanicOnBugs = true
Main(m, hooks.Options)
}
func TestEnablingInlayHints(t *testing.T) {
testenv.NeedsGo1Point(t, 14) // Test fails on 1.13.
const workspace = `
-- go.mod --
module inlayHint.test
go 1.12
-- lib.go --
package lib
type Number int
const (
Zero Number = iota
One
Two
)
`
tests := []struct {
label string
enabled map[string]bool
wantInlayHint bool
}{
{
label: "default",
wantInlayHint: false,
},
{
label: "enable const",
enabled: map[string]bool{source.ConstantValues: true},
wantInlayHint: true,
},
{
label: "enable parameter names",
enabled: map[string]bool{source.ParameterNames: true},
wantInlayHint: false,
},
}
for _, test := range tests {
t.Run(test.label, func(t *testing.T) {
WithOptions(
EditorConfig{
Settings: map[string]interface{}{
"hints": test.enabled,
},
},
).Run(t, workspace, func(t *testing.T, env *Env) {
env.OpenFile("lib.go")
lens := env.InlayHints("lib.go")
if gotInlayHint := len(lens) > 0; gotInlayHint != test.wantInlayHint {
t.Errorf("got inlayHint: %t, want %t", gotInlayHint, test.wantInlayHint)
}
})
})
}
}

View File

@ -1114,6 +1114,27 @@ func (e *Editor) Symbols(ctx context.Context, sym string) ([]protocol.SymbolInfo
return ans, err
}
// CodeLens executes a codelens request on the server.
func (e *Editor) InlayHint(ctx context.Context, path string) ([]protocol.InlayHint, error) {
if e.Server == nil {
return nil, nil
}
e.mu.Lock()
_, ok := e.buffers[path]
e.mu.Unlock()
if !ok {
return nil, fmt.Errorf("buffer %q is not open", path)
}
params := &protocol.InlayHintParams{
TextDocument: e.textDocumentIdentifier(path),
}
hints, err := e.Server.InlayHint(ctx, params)
if err != nil {
return nil, err
}
return hints, nil
}
// References executes a reference request on the server.
func (e *Editor) References(ctx context.Context, path string, pos Pos) ([]protocol.Location, error) {
if e.Server == nil {

View File

@ -358,6 +358,17 @@ func (e *Env) ExecuteCommand(params *protocol.ExecuteCommandParams, result inter
}
}
// InlayHints calls textDocument/inlayHints for the given path, calling t.Fatal on
// any error.
func (e *Env) InlayHints(path string) []protocol.InlayHint {
e.T.Helper()
hints, err := e.Editor.InlayHint(e.Ctx, path)
if err != nil {
e.T.Fatal(err)
}
return hints
}
// WorkspaceSymbol calls workspace/symbol
func (e *Env) WorkspaceSymbol(sym string) []protocol.SymbolInformation {
e.T.Helper()