[gopls-release-branch.0.11] 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>
Reviewed-on: https://go-review.googlesource.com/c/tools/+/456641
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
diff --git a/gopls/internal/lsp/source/comment_go118.go b/gopls/internal/lsp/source/comment_go118.go
index 0503670..ca4ab9d 100644
--- a/gopls/internal/lsp/source/comment_go118.go
+++ b/gopls/internal/lsp/source/comment_go118.go
@@ -32,6 +32,11 @@
var p comment.Parser
doc := p.Parse(text)
var pr comment.Printer
+ // The default produces {#Hdr-...} tags for headings.
+ // vscode displays thems, which is undesirable.
+ // The godoc for comment.Printer says the tags
+ // avoid a security problem.
+ pr.HeadingID = func(*comment.Heading) string { return "" }
easy := pr.Markdown(doc)
return string(easy)
}
diff --git a/gopls/internal/regtest/misc/hover_test.go b/gopls/internal/regtest/misc/hover_test.go
index bea370a..324f88b 100644
--- a/gopls/internal/regtest/misc/hover_test.go
+++ b/gopls/internal/regtest/misc/hover_test.go
@@ -221,3 +221,42 @@
}
})
}
+
+// for x/tools/gopls: unhandled named anchor on the hover #57048
+func TestHoverTags(t *testing.T) {
+ testenv.NeedsGo1Point(t, 14) // until go1.13 is dropped from kokoro
+ const source = `
+-- go.mod --
+module mod.com
+
+go 1.19
+
+-- lib/a.go --
+
+// variety of execution modes.
+//
+// # Test package setup
+//
+// The regression test package uses a couple of uncommon patterns to reduce
+package lib
+
+-- a.go --
+ package main
+ import "mod.com/lib"
+
+ const A = 1
+
+}
+`
+ Run(t, source, func(t *testing.T, env *Env) {
+ t.Run("tags", func(t *testing.T) {
+ env.OpenFile("a.go")
+ z := env.RegexpSearch("a.go", "lib")
+ t.Logf("%#v", z)
+ got, _ := env.Hover("a.go", env.RegexpSearch("a.go", "lib"))
+ if strings.Contains(got.Value, "{#hdr-") {
+ t.Errorf("Hover: got {#hdr- tag:\n%q", got)
+ }
+ })
+ })
+}