From 7f24a8300e00136fb178593b1fb842d4f3ef48ba Mon Sep 17 00:00:00 2001 From: Alan Donovan Date: Wed, 9 Apr 2014 15:41:18 -0400 Subject: [PATCH] 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 --- godoc/server.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/godoc/server.go b/godoc/server.go index a4d613f9..8e273c20 100644 --- a/godoc/server.go +++ b/godoc/server.go @@ -527,6 +527,9 @@ func (p *Presentation) serveTextFile(w http.ResponseWriter, r *http.Request, abs // 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 @@ func formatGoSource(buf *bytes.Buffer, text []byte, links []analysis.Link, patte } 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, "%6d\t", n, n) + n++ + saved.Write(line) + saved.WriteByte('\n') + } } func (p *Presentation) serveDirectory(w http.ResponseWriter, r *http.Request, abspath, relpath string) {