diff --git a/src/cmd/doc/doc_test.go b/src/cmd/doc/doc_test.go index 39530e3c2d..af7793133e 100644 --- a/src/cmd/doc/doc_test.go +++ b/src/cmd/doc/doc_test.go @@ -579,7 +579,7 @@ var tests = []test{ []string{ `Comment about exported interface`, // Include comment. `type ExportedInterface interface`, // Interface definition. - `Comment before exported method.*\n.*ExportedMethod\(\)` + + `Comment before exported method.\n.*//\n.*// // Code block showing how to use ExportedMethod\n.*// func DoSomething\(\) error {\n.*// ExportedMethod\(\)\n.*// return nil\n.*// }\n.*//.*\n.*ExportedMethod\(\)` + `.*Comment on line with exported method`, `io.Reader.*Comment on line with embedded Reader`, `error.*Comment on line with embedded error`, @@ -599,8 +599,7 @@ var tests = []test{ []string{ `Comment about exported interface`, // Include comment. `type ExportedInterface interface`, // Interface definition. - `Comment before exported method.*\n.*ExportedMethod\(\)` + - `.*Comment on line with exported method`, + `Comment before exported method.\n.*//\n.*// // Code block showing how to use ExportedMethod\n.*// func DoSomething\(\) error {\n.*// ExportedMethod\(\)\n.*// return nil\n.*// }\n.*//.*\n.*ExportedMethod\(\)` + `.*Comment on line with exported method`, `unexportedMethod\(\).*Comment on line with unexported method`, `io.Reader.*Comment on line with embedded Reader`, `error.*Comment on line with embedded error`, @@ -615,7 +614,7 @@ var tests = []test{ "interface method", []string{p, `ExportedInterface.ExportedMethod`}, []string{ - `Comment before exported method.*\n.*ExportedMethod\(\)` + + `Comment before exported method.\n.*//\n.*// // Code block showing how to use ExportedMethod\n.*// func DoSomething\(\) error {\n.*// ExportedMethod\(\)\n.*// return nil\n.*// }\n.*//.*\n.*ExportedMethod\(\)` + `.*Comment on line with exported method`, }, []string{ diff --git a/src/cmd/doc/pkg.go b/src/cmd/doc/pkg.go index c2e06ebc8b..587f0bdc14 100644 --- a/src/cmd/doc/pkg.go +++ b/src/cmd/doc/pkg.go @@ -950,6 +950,9 @@ func (pkg *Package) printMethodDoc(symbol, method string) bool { // Not an interface type. continue } + + // Collect and print only the methods that match. + var methods []*ast.Field for _, iMethod := range inter.Methods.List { // This is an interface, so there can be only one name. // TODO: Anonymous methods (embedding) @@ -958,22 +961,21 @@ func (pkg *Package) printMethodDoc(symbol, method string) bool { } name := iMethod.Names[0].Name if match(method, name) { - if iMethod.Doc != nil { - for _, comment := range iMethod.Doc.List { - doc.ToText(&pkg.buf, comment.Text, "", indent, indentedWidth) - } - } - s := pkg.oneLineNode(iMethod.Type) - // Hack: s starts "func" but there is no name present. - // We could instead build a FuncDecl but it's not worthwhile. - lineComment := "" - if iMethod.Comment != nil { - lineComment = fmt.Sprintf(" %s", iMethod.Comment.List[0].Text) - } - pkg.Printf("func %s%s%s\n", name, s[4:], lineComment) + methods = append(methods, iMethod) found = true } } + if found { + pkg.Printf("type %s ", spec.Name) + inter.Methods.List, methods = methods, inter.Methods.List + err := format.Node(&pkg.buf, pkg.fs, inter) + if err != nil { + log.Fatal(err) + } + pkg.newlines(1) + // Restore the original methods. + inter.Methods.List = methods + } } return found } diff --git a/src/cmd/doc/testdata/pkg.go b/src/cmd/doc/testdata/pkg.go index d695bdf1c5..5ece832565 100644 --- a/src/cmd/doc/testdata/pkg.go +++ b/src/cmd/doc/testdata/pkg.go @@ -111,6 +111,13 @@ const unexportedTypedConstant ExportedType = 1 // In a separate section to test // Comment about exported interface. type ExportedInterface interface { // Comment before exported method. + // + // // Code block showing how to use ExportedMethod + // func DoSomething() error { + // ExportedMethod() + // return nil + // } + // ExportedMethod() // Comment on line with exported method. unexportedMethod() // Comment on line with unexported method. io.Reader // Comment on line with embedded Reader.