]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/doc: use new go/doc APIs
authorRuss Cox <rsc@golang.org>
Tue, 8 Feb 2022 18:43:40 +0000 (13:43 -0500)
committerRuss Cox <rsc@golang.org>
Mon, 11 Apr 2022 16:31:53 +0000 (16:31 +0000)
[This CL is part of a sequence implementing the proposal #51082.
The design doc is at https://go.dev/s/godocfmt-design.]

Use the new per-Package go/doc API instead of the
top-level functions from go/doc. These handle links better.

For #51082.

Change-Id: I169b46d973673abdb6f126352c9f1e30f9fe1122
Reviewed-on: https://go-review.googlesource.com/c/go/+/384266
Run-TryBot: Russ Cox <rsc@golang.org>
Reviewed-by: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>

src/cmd/doc/pkg.go

index 49b68873b6f9ceb26e3206a1ff9620b898c36ba4..35f2eb24bf2fc1967cce90f3ac13418f911ba7c0 100644 (file)
@@ -25,8 +25,7 @@ import (
 )
 
 const (
-       punchedCardWidth = 80 // These things just won't leave us alone.
-       indentedWidth    = punchedCardWidth - len(indent)
+       punchedCardWidth = 80
        indent           = "    "
 )
 
@@ -44,6 +43,14 @@ type Package struct {
        buf         pkgBuffer
 }
 
+func (p *Package) ToText(w io.Writer, text, prefix, codePrefix string) {
+       d := p.doc.Parser().Parse(text)
+       pr := p.doc.Printer()
+       pr.TextPrefix = prefix
+       pr.TextCodePrefix = codePrefix
+       w.Write(pr.Text(d))
+}
+
 // pkgBuffer is a wrapper for bytes.Buffer that prints a package clause the
 // first time Write is called.
 type pkgBuffer struct {
@@ -251,7 +258,7 @@ func (pkg *Package) emit(comment string, node ast.Node) {
                }
                if comment != "" && !showSrc {
                        pkg.newlines(1)
-                       doc.ToText(&pkg.buf, comment, indent, indent+indent, indentedWidth)
+                       pkg.ToText(&pkg.buf, comment, indent, indent+indent)
                        pkg.newlines(2) // Blank line after comment to separate from next item.
                } else {
                        pkg.newlines(1)
@@ -463,7 +470,7 @@ func joinStrings(ss []string) string {
 // allDoc prints all the docs for the package.
 func (pkg *Package) allDoc() {
        pkg.Printf("") // Trigger the package clause; we know the package exists.
-       doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
+       pkg.ToText(&pkg.buf, pkg.doc.Doc, "", indent)
        pkg.newlines(1)
 
        printed := make(map[*ast.GenDecl]bool)
@@ -523,7 +530,7 @@ func (pkg *Package) allDoc() {
 func (pkg *Package) packageDoc() {
        pkg.Printf("") // Trigger the package clause; we know the package exists.
        if !short {
-               doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
+               pkg.ToText(&pkg.buf, pkg.doc.Doc, "", indent)
                pkg.newlines(1)
        }
 
@@ -1033,9 +1040,9 @@ func (pkg *Package) printFieldDoc(symbol, fieldName string) bool {
                                if field.Doc != nil {
                                        // To present indented blocks in comments correctly, process the comment as
                                        // a unit before adding the leading // to each line.
-                                       docBuf := bytes.Buffer{}
-                                       doc.ToText(&docBuf, field.Doc.Text(), "", indent, indentedWidth)
-                                       scanner := bufio.NewScanner(&docBuf)
+                                       docBuf := new(bytes.Buffer)
+                                       pkg.ToText(docBuf, field.Doc.Text(), "", indent)
+                                       scanner := bufio.NewScanner(docBuf)
                                        for scanner.Scan() {
                                                fmt.Fprintf(&pkg.buf, "%s// %s\n", indent, scanner.Bytes())
                                        }