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) {