Skip to content

Commit e5b3324

Browse files
committed
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 <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Suzy Mueller <[email protected]>
1 parent 2a90056 commit e5b3324

File tree

3 files changed

+104
-0
lines changed

3 files changed

+104
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2022 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
package inlayHint
5+
6+
import (
7+
"testing"
8+
9+
"golang.org/x/tools/gopls/internal/hooks"
10+
"golang.org/x/tools/internal/lsp/bug"
11+
. "golang.org/x/tools/internal/lsp/regtest"
12+
"golang.org/x/tools/internal/lsp/source"
13+
"golang.org/x/tools/internal/testenv"
14+
)
15+
16+
func TestMain(m *testing.M) {
17+
bug.PanicOnBugs = true
18+
Main(m, hooks.Options)
19+
}
20+
func TestEnablingInlayHints(t *testing.T) {
21+
testenv.NeedsGo1Point(t, 14) // Test fails on 1.13.
22+
const workspace = `
23+
-- go.mod --
24+
module inlayHint.test
25+
go 1.12
26+
-- lib.go --
27+
package lib
28+
type Number int
29+
const (
30+
Zero Number = iota
31+
One
32+
Two
33+
)
34+
`
35+
tests := []struct {
36+
label string
37+
enabled map[string]bool
38+
wantInlayHint bool
39+
}{
40+
{
41+
label: "default",
42+
wantInlayHint: false,
43+
},
44+
{
45+
label: "enable const",
46+
enabled: map[string]bool{source.ConstantValues: true},
47+
wantInlayHint: true,
48+
},
49+
{
50+
label: "enable parameter names",
51+
enabled: map[string]bool{source.ParameterNames: true},
52+
wantInlayHint: false,
53+
},
54+
}
55+
for _, test := range tests {
56+
t.Run(test.label, func(t *testing.T) {
57+
WithOptions(
58+
EditorConfig{
59+
Settings: map[string]interface{}{
60+
"hints": test.enabled,
61+
},
62+
},
63+
).Run(t, workspace, func(t *testing.T, env *Env) {
64+
env.OpenFile("lib.go")
65+
lens := env.InlayHints("lib.go")
66+
if gotInlayHint := len(lens) > 0; gotInlayHint != test.wantInlayHint {
67+
t.Errorf("got inlayHint: %t, want %t", gotInlayHint, test.wantInlayHint)
68+
}
69+
})
70+
})
71+
}
72+
}

internal/lsp/fake/editor.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,6 +1114,27 @@ func (e *Editor) Symbols(ctx context.Context, sym string) ([]protocol.SymbolInfo
11141114
return ans, err
11151115
}
11161116

1117+
// CodeLens executes a codelens request on the server.
1118+
func (e *Editor) InlayHint(ctx context.Context, path string) ([]protocol.InlayHint, error) {
1119+
if e.Server == nil {
1120+
return nil, nil
1121+
}
1122+
e.mu.Lock()
1123+
_, ok := e.buffers[path]
1124+
e.mu.Unlock()
1125+
if !ok {
1126+
return nil, fmt.Errorf("buffer %q is not open", path)
1127+
}
1128+
params := &protocol.InlayHintParams{
1129+
TextDocument: e.textDocumentIdentifier(path),
1130+
}
1131+
hints, err := e.Server.InlayHint(ctx, params)
1132+
if err != nil {
1133+
return nil, err
1134+
}
1135+
return hints, nil
1136+
}
1137+
11171138
// References executes a reference request on the server.
11181139
func (e *Editor) References(ctx context.Context, path string, pos Pos) ([]protocol.Location, error) {
11191140
if e.Server == nil {

internal/lsp/regtest/wrappers.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,17 @@ func (e *Env) ExecuteCommand(params *protocol.ExecuteCommandParams, result inter
358358
}
359359
}
360360

361+
// InlayHints calls textDocument/inlayHints for the given path, calling t.Fatal on
362+
// any error.
363+
func (e *Env) InlayHints(path string) []protocol.InlayHint {
364+
e.T.Helper()
365+
hints, err := e.Editor.InlayHint(e.Ctx, path)
366+
if err != nil {
367+
e.T.Fatal(err)
368+
}
369+
return hints
370+
}
371+
361372
// WorkspaceSymbol calls workspace/symbol
362373
func (e *Env) WorkspaceSymbol(sym string) []protocol.SymbolInformation {
363374
e.T.Helper()

0 commit comments

Comments
 (0)