Commit e0a97a59 authored by Rob Pike's avatar Rob Pike

cmd/doc: make comments inside functions appear with -src

The old godoc didn't do this either, perhaps because it's a little
tricky, but it can be done using a special type from the go/printer
package. (Usually we just use go/format).

Fixes #28195.

Change-Id: Ic6d3df3953ba71128398ceaf9a133c798551b6b8
Reviewed-on: https://go-review.googlesource.com/c/143037Reviewed-by: 's avatarRobert Griesemer <gri@golang.org>
parent 41587340
...@@ -466,8 +466,8 @@ var tests = []test{ ...@@ -466,8 +466,8 @@ var tests = []test{
[]string{ []string{
`Comment about exported type`, // Include comment. `Comment about exported type`, // Include comment.
`type ExportedType struct`, // Type definition. `type ExportedType struct`, // Type definition.
`Comment before exported field.*\n.*ExportedField +int` + `Comment before exported field`,
`.*Comment on line with exported field`, `ExportedField.*Comment on line with exported field`,
`ExportedEmbeddedType.*Comment on line with exported embedded field`, `ExportedEmbeddedType.*Comment on line with exported embedded field`,
`unexportedType.*Comment on line with unexported embedded field`, `unexportedType.*Comment on line with unexported embedded field`,
`func \(ExportedType\) ExportedMethod\(a int\) bool`, `func \(ExportedType\) ExportedMethod\(a int\) bool`,
......
...@@ -12,6 +12,7 @@ import ( ...@@ -12,6 +12,7 @@ import (
"go/doc" "go/doc"
"go/format" "go/format"
"go/parser" "go/parser"
"go/printer"
"go/token" "go/token"
"io" "io"
"log" "log"
...@@ -206,7 +207,17 @@ func (pkg *Package) newlines(n int) { ...@@ -206,7 +207,17 @@ func (pkg *Package) newlines(n int) {
// clears the stuff we don't want to print anyway. It's a bit of a magic trick. // clears the stuff we don't want to print anyway. It's a bit of a magic trick.
func (pkg *Package) emit(comment string, node ast.Node) { func (pkg *Package) emit(comment string, node ast.Node) {
if node != nil { if node != nil {
err := format.Node(&pkg.buf, pkg.fs, node) var err error
if showSrc {
// Need an extra little dance to get internal comments to appear.
commentedNode := &printer.CommentedNode{
Node: node,
Comments: pkg.file.Comments,
}
err = format.Node(&pkg.buf, pkg.fs, commentedNode)
} else {
err = format.Node(&pkg.buf, pkg.fs, node)
}
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment