go.tools/godoc: restore line anchors to the source view.
These were removed when adding the -analysis flag (CL
60540044), ostensibly for performance, but I can't reproduce
the serious slowdowns I saw then. In any case, they are
needed for certain URLs.
Fixes golang/go#7743
LGTM=bgarcia
R=bgarcia
CC=golang-codereviews
https://golang.org/cl/86150043
diff --git a/godoc/server.go b/godoc/server.go
index a4d613f..8e273c2 100644
--- a/godoc/server.go
+++ b/godoc/server.go
@@ -527,6 +527,9 @@
// decorating it with the specified analysis links.
//
func formatGoSource(buf *bytes.Buffer, text []byte, links []analysis.Link, pattern string, selection Selection) {
+ // Emit to a temp buffer so that we can add line anchors at the end.
+ saved, buf := buf, new(bytes.Buffer)
+
var i int
var link analysis.Link // shared state of the two funcs below
segmentIter := func() (seg Segment) {
@@ -548,6 +551,18 @@
}
FormatSelections(buf, text, linkWriter, segmentIter, selectionTag, comments, highlights, selection)
+
+ // Now copy buf to saved, adding line anchors.
+
+ // The lineSelection mechanism can't be composed with our
+ // linkWriter, so we have to add line spans as another pass.
+ n := 1
+ for _, line := range bytes.Split(buf.Bytes(), []byte("\n")) {
+ fmt.Fprintf(saved, "<span id=\"L%d\" class=\"ln\">%6d</span>\t", n, n)
+ n++
+ saved.Write(line)
+ saved.WriteByte('\n')
+ }
}
func (p *Presentation) serveDirectory(w http.ResponseWriter, r *http.Request, abspath, relpath string) {