Skip to content

Commit

Permalink
gopls/hover: remove header tags from hover markdown
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pjweinbgo authored and findleyr committed Dec 9, 2022
1 parent a310bcb commit 3da7f1e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
5 changes: 5 additions & 0 deletions gopls/internal/lsp/source/comment_go118.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func CommentToMarkdown(text string) string {
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)
}
39 changes: 39 additions & 0 deletions gopls/internal/regtest/misc/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,42 @@ func main() {
}
})
}

// 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)
}
})
})
}

0 comments on commit 3da7f1e

Please sign in to comment.