internal/lsp: initial test set up for inlay hints

Set up the tests for inlay hints. We test inlay hints by converting them to text edits
and verifying the output is as we expected it.

This change does not yet deal with making sure the server
settings are correct.

Change-Id: I136f971a87bf9936fd44047d45fe0a3f03c9164e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/411095
Run-TryBot: Suzy Mueller <suzmue@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jamal Carvalho <jamal@golang.org>
This commit is contained in:
Suzy Mueller 2022-06-08 10:18:05 -04:00
parent 1d19788894
commit ad756c73a0
6 changed files with 89 additions and 0 deletions

View File

@ -113,6 +113,10 @@ func (r *runner) Hover(t *testing.T, spn span.Span, info string) {
//TODO: hovering not supported on command line
}
func (r *runner) InlayHints(t *testing.T, spn span.Span) {
// TODO: inlayHints not supported on command line
}
func (r *runner) runGoplsCmd(t testing.TB, args ...string) (string, string) {
rStdout, wStdout, err := os.Pipe()
if err != nil {

View File

@ -932,6 +932,48 @@ func (r *runner) References(t *testing.T, src span.Span, itemList []span.Span) {
}
}
func (r *runner) InlayHints(t *testing.T, spn span.Span) {
uri := spn.URI()
filename := uri.Filename()
hints, err := r.server.InlayHint(r.ctx, &protocol.InlayHintParams{
TextDocument: protocol.TextDocumentIdentifier{
URI: protocol.URIFromSpanURI(uri),
},
// TODO: add ViewPort
})
if err != nil {
t.Fatal(err)
}
// Map inlay hints to text edits.
edits := make([]protocol.TextEdit, len(hints))
for i, hint := range hints {
edits[i] = protocol.TextEdit{
Range: protocol.Range{Start: *hint.Position, End: *hint.Position},
NewText: fmt.Sprintf("<%s>", hint.Label[0].Value),
}
}
m, err := r.data.Mapper(uri)
if err != nil {
t.Fatal(err)
}
sedits, err := source.FromProtocolEdits(m, edits)
if err != nil {
t.Error(err)
}
got := diff.ApplyEdits(string(m.Content), sedits)
withinlayHints := string(r.data.Golden("inlayHint", filename, func() ([]byte, error) {
return []byte(got), nil
}))
if withinlayHints != got {
t.Errorf("format failed for %s, expected:\n%v\ngot:\n%v", filename, withinlayHints, got)
}
}
func (r *runner) Rename(t *testing.T, spn span.Span, newText string) {
tag := fmt.Sprintf("%s-rename", newText)

View File

@ -685,6 +685,10 @@ func (r *runner) Highlight(t *testing.T, src span.Span, locations []span.Span) {
}
}
func (r *runner) InlayHints(t *testing.T, src span.Span) {
// TODO(golang/go#53315): add source test
}
func (r *runner) Hover(t *testing.T, src span.Span, text string) {
ctx := r.ctx
_, srcRng, err := spanToRange(r.data, src)

9
internal/lsp/testdata/inlayHint/a.go vendored Normal file
View File

@ -0,0 +1,9 @@
package inlayHint //@inlayHint("package")
func hello(name string) string {
return "Hello " + name
}
func helloWorld() string {
return hello("World")
}

View File

@ -0,0 +1,11 @@
-- inlayHint --
package inlayHint //@inlayHint("package")
func hello(name string) string {
return "Hello " + name
}
func helloWorld() string {
return hello("World")
}

View File

@ -81,6 +81,7 @@ type PrepareRenames map[span.Span]*source.PrepareItem
type Symbols map[span.URI][]protocol.DocumentSymbol
type SymbolsChildren map[string][]protocol.DocumentSymbol
type SymbolInformation map[span.Span]protocol.SymbolInformation
type InlayHints []span.Span
type WorkspaceSymbols map[WorkspaceSymbolsTestType]map[span.URI][]string
type Signatures map[span.Span]*protocol.SignatureHelp
type Links map[span.URI][]Link
@ -113,6 +114,7 @@ type Data struct {
Highlights Highlights
References References
Renames Renames
InlayHints InlayHints
PrepareRenames PrepareRenames
Symbols Symbols
symbolsChildren SymbolsChildren
@ -156,6 +158,7 @@ type Tests interface {
Definition(*testing.T, span.Span, Definition)
Implementation(*testing.T, span.Span, []span.Span)
Highlight(*testing.T, span.Span, []span.Span)
InlayHints(*testing.T, span.Span)
References(*testing.T, span.Span, []span.Span)
Rename(*testing.T, span.Span, string)
PrepareRename(*testing.T, span.Span, *source.PrepareItem)
@ -466,6 +469,7 @@ func load(t testing.TB, mode string, dir string) *Data {
"hoverdef": datum.collectHoverDefinitions,
"hover": datum.collectHovers,
"highlight": datum.collectHighlights,
"inlayHint": datum.collectInlayHints,
"refs": datum.collectReferences,
"rename": datum.collectRenames,
"prepare": datum.collectPrepareRenames,
@ -782,6 +786,17 @@ func Run(t *testing.T, tests Tests, data *Data) {
}
})
t.Run("InlayHints", func(t *testing.T) {
t.Skip("Inlay Hints not yet implemented")
t.Helper()
for _, src := range data.InlayHints {
t.Run(SpanName(src), func(t *testing.T) {
t.Helper()
tests.InlayHints(t, src)
})
}
})
t.Run("References", func(t *testing.T) {
t.Helper()
for src, itemList := range data.References {
@ -1292,6 +1307,10 @@ func (data *Data) collectHighlights(src span.Span, expected []span.Span) {
data.Highlights[src] = append(data.Highlights[src], expected...)
}
func (data *Data) collectInlayHints(src span.Span) {
data.InlayHints = append(data.InlayHints, src)
}
func (data *Data) collectReferences(src span.Span, expected []span.Span) {
data.References[src] = expected
}