Skip to content

Commit 4906a71

Browse files
committed
gopls/internal/lsp/source: add documentation links to hover
Point to the documentation for entries like [fmt.Printf] in the doc comments shown in hover. The links are to https://pkg.go.dev. Fixes golang/go#58352 Change-Id: Ia5c8fb0e7eaa7861eac359fa138aa6d8d8ff1327 Reviewed-on: https://go-review.googlesource.com/c/tools/+/469715 Run-TryBot: Peter Weinberger <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]>
1 parent 3ec30bd commit 4906a71

File tree

7 files changed

+60
-7
lines changed

7 files changed

+60
-7
lines changed

gopls/internal/lsp/completion.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ func toProtocolCompletionItems(candidates []completion.CompletionItem, rng proto
104104
doc := &protocol.Or_CompletionItem_documentation{
105105
Value: protocol.MarkupContent{
106106
Kind: protocol.Markdown,
107-
Value: source.CommentToMarkdown(candidate.Documentation),
107+
Value: source.CommentToMarkdown(candidate.Documentation, options),
108108
},
109109
}
110110
if options.PreferredContentFormat != protocol.Markdown {

gopls/internal/lsp/source/comment.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
// prefix removed unless empty, in which case it will be converted to a newline.
3030
//
3131
// URLs in the comment text are converted into links.
32-
func CommentToMarkdown(text string) string {
32+
func CommentToMarkdown(text string, _ *Options) string {
3333
buf := &bytes.Buffer{}
3434
commentToMarkdown(buf, text)
3535
return buf.String()

gopls/internal/lsp/source/comment_go118_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ F declaration\.
364364
tt.in = strings.TrimPrefix(tt.in, "\n")
365365
tt.out = strings.TrimPrefix(tt.out, "\n")
366366

367-
if out := CommentToMarkdown(tt.in); out != tt.out {
367+
if out := CommentToMarkdown(tt.in, nil); out != tt.out {
368368
t.Errorf("#%d: mismatch\nhave: %q\nwant: %q", i, out, tt.out)
369369
}
370370
}

gopls/internal/lsp/source/comment_go118.go renamed to gopls/internal/lsp/source/comment_go119.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,17 @@ package source
2121
// be replaced by this file, the golden test files should be updated.
2222
// (and checkSameMarkdown() could be replaced by a simple comparison.)
2323

24-
import "go/doc/comment"
24+
import (
25+
"fmt"
26+
"go/doc/comment"
27+
)
2528

2629
// CommentToMarkdown converts comment text to formatted markdown.
2730
// The comment was prepared by DocReader,
2831
// so it is known not to have leading, trailing blank lines
2932
// nor to have trailing spaces at the end of lines.
3033
// The comment markers have already been removed.
31-
func CommentToMarkdown(text string) string {
34+
func CommentToMarkdown(text string, options *Options) string {
3235
var p comment.Parser
3336
doc := p.Parse(text)
3437
var pr comment.Printer
@@ -37,6 +40,17 @@ func CommentToMarkdown(text string) string {
3740
// The godoc for comment.Printer says the tags
3841
// avoid a security problem.
3942
pr.HeadingID = func(*comment.Heading) string { return "" }
43+
pr.DocLinkURL = func(link *comment.DocLink) string {
44+
msg := fmt.Sprintf("https://%s/%s", options.LinkTarget, link.ImportPath)
45+
if link.Name != "" {
46+
msg += "#"
47+
if link.Recv != "" {
48+
msg += link.Recv + "."
49+
}
50+
msg += link.Name
51+
}
52+
return msg
53+
}
4054
easy := pr.Markdown(doc)
4155
return string(easy)
4256
}

gopls/internal/lsp/source/hover.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ func formatDoc(h *HoverJSON, options *Options) string {
808808
doc = h.FullDocumentation
809809
}
810810
if options.PreferredContentFormat == protocol.Markdown {
811-
return CommentToMarkdown(doc)
811+
return CommentToMarkdown(doc, options)
812812
}
813813
return doc
814814
}

gopls/internal/lsp/source/signature_help.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ func stringToSigInfoDocumentation(s string, options *Options) *protocol.Or_Signa
170170
v := s
171171
k := protocol.PlainText
172172
if options.PreferredContentFormat == protocol.Markdown {
173-
v = CommentToMarkdown(s)
173+
v = CommentToMarkdown(s, options)
174174
// whether or not content is newline terminated may not matter for LSP clients,
175175
// but our tests expect trailing newlines to be stripped.
176176
v = strings.TrimSuffix(v, "\n") // TODO(pjw): change the golden files

gopls/internal/regtest/misc/hover_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,3 +343,42 @@ func Hello() string {
343343
}
344344
})
345345
}
346+
347+
// Test that the generated markdown contains links for Go references.
348+
// https://github.com/golang/go/issues/58352
349+
func TestHoverLinks(t *testing.T) {
350+
testenv.NeedsGo1Point(t, 19)
351+
const input = `
352+
-- go.mod --
353+
go 1.19
354+
module mod.com
355+
-- main.go --
356+
package main
357+
// [fmt]
358+
var A int
359+
// [fmt.Println]
360+
var B int
361+
// [golang.org/x/tools/go/packages.Package.String]
362+
var C int
363+
`
364+
var tests = []struct {
365+
pat string
366+
ans string
367+
}{
368+
{"A", "fmt"},
369+
{"B", "fmt#Println"},
370+
{"C", "golang.org/x/tools/go/packages#Package.String"},
371+
}
372+
for _, test := range tests {
373+
Run(t, input, func(t *testing.T, env *Env) {
374+
env.OpenFile("main.go")
375+
loc := env.RegexpSearch("main.go", test.pat)
376+
hover, _ := env.Hover(loc)
377+
hoverContent := hover.Value
378+
want := fmt.Sprintf("%s/%s", "https://pkg.go.dev", test.ans)
379+
if !strings.Contains(hoverContent, want) {
380+
t.Errorf("hover:%q does not contain link %q", hoverContent, want)
381+
}
382+
})
383+
}
384+
}

0 commit comments

Comments
 (0)