Skip to content

Commit 3da7f1e

Browse files
pjweinbgofindleyr
authored andcommitted
gopls/hover: remove header tags from hover markdown
Lines in go doc that look like '// ## Header' generate, by default, tags in the markdown that look like '{hdr-Header}' which are harmless and annoying. This CL generates the markdown without the tags. Fixes: golang/go#57048 Change-Id: I7109775ea3ec6b51504d62108187e3614b2d1e9d Reviewed-on: https://go-review.googlesource.com/c/tools/+/456535 Run-TryBot: Peter Weinberger <pjw@google.com> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> gopls-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
1 parent a310bcb commit 3da7f1e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

gopls/internal/lsp/source/comment_go118.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ func CommentToMarkdown(text string) string {
3232
var p comment.Parser
3333
doc := p.Parse(text)
3434
var pr comment.Printer
35+
// The default produces {#Hdr-...} tags for headings.
36+
// vscode displays thems, which is undesirable.
37+
// The godoc for comment.Printer says the tags
38+
// avoid a security problem.
39+
pr.HeadingID = func(*comment.Heading) string { return "" }
3540
easy := pr.Markdown(doc)
3641
return string(easy)
3742
}

gopls/internal/regtest/misc/hover_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,42 @@ func main() {
221221
}
222222
})
223223
}
224+
225+
// for x/tools/gopls: unhandled named anchor on the hover #57048
226+
func TestHoverTags(t *testing.T) {
227+
testenv.NeedsGo1Point(t, 14) // until go1.13 is dropped from kokoro
228+
const source = `
229+
-- go.mod --
230+
module mod.com
231+
232+
go 1.19
233+
234+
-- lib/a.go --
235+
236+
// variety of execution modes.
237+
//
238+
// # Test package setup
239+
//
240+
// The regression test package uses a couple of uncommon patterns to reduce
241+
package lib
242+
243+
-- a.go --
244+
package main
245+
import "mod.com/lib"
246+
247+
const A = 1
248+
249+
}
250+
`
251+
Run(t, source, func(t *testing.T, env *Env) {
252+
t.Run("tags", func(t *testing.T) {
253+
env.OpenFile("a.go")
254+
z := env.RegexpSearch("a.go", "lib")
255+
t.Logf("%#v", z)
256+
got, _ := env.Hover("a.go", env.RegexpSearch("a.go", "lib"))
257+
if strings.Contains(got.Value, "{#hdr-") {
258+
t.Errorf("Hover: got {#hdr- tag:\n%q", got)
259+
}
260+
})
261+
})
262+
}

0 commit comments

Comments
 (0)