Skip to content

Commit ed3ae9a

Browse files
agnivaderobpike
authored andcommitted
cmd/doc: properly display interface methods
Previously, we used to call doc.ToText to print each comment in a comment group attached to an interface method. This broke any preformatted code block attached to the comment, and displayed everything aligned to a single column. Additionally, the name of the interface also wasn't displayed which didn't show which interface the method belonged to. To fix this, we print the entire interface node using format.Node which takes care of displaying the comments correctly, and we also filter out the methods that don't match, so that the method can be displayed as belonging to an interface. As an example, previously it would show: // Comment before exported method. // // // Code block showing how to use ExportedMethod // func DoSomething() error { // ExportedMethod() // return nil // } func ExportedMethod() // Comment on line with exported method. Now, it shows: 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. } Fixes #43188 Change-Id: I28099fe4aab35e08049b2616a3506240f57133cc Reviewed-on: https://go-review.googlesource.com/c/go/+/279433 Trust: Agniva De Sarker <[email protected]> Trust: Daniel Martí <[email protected]> Reviewed-by: Rob Pike <[email protected]>
1 parent 9136d95 commit ed3ae9a

File tree

3 files changed

+25
-17
lines changed

3 files changed

+25
-17
lines changed

src/cmd/doc/doc_test.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ var tests = []test{
579579
[]string{
580580
`Comment about exported interface`, // Include comment.
581581
`type ExportedInterface interface`, // Interface definition.
582-
`Comment before exported method.*\n.*ExportedMethod\(\)` +
582+
`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\(\)` +
583583
`.*Comment on line with exported method`,
584584
`io.Reader.*Comment on line with embedded Reader`,
585585
`error.*Comment on line with embedded error`,
@@ -599,8 +599,7 @@ var tests = []test{
599599
[]string{
600600
`Comment about exported interface`, // Include comment.
601601
`type ExportedInterface interface`, // Interface definition.
602-
`Comment before exported method.*\n.*ExportedMethod\(\)` +
603-
`.*Comment on line with exported method`,
602+
`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`,
604603
`unexportedMethod\(\).*Comment on line with unexported method`,
605604
`io.Reader.*Comment on line with embedded Reader`,
606605
`error.*Comment on line with embedded error`,
@@ -615,7 +614,7 @@ var tests = []test{
615614
"interface method",
616615
[]string{p, `ExportedInterface.ExportedMethod`},
617616
[]string{
618-
`Comment before exported method.*\n.*ExportedMethod\(\)` +
617+
`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\(\)` +
619618
`.*Comment on line with exported method`,
620619
},
621620
[]string{

src/cmd/doc/pkg.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,9 @@ func (pkg *Package) printMethodDoc(symbol, method string) bool {
950950
// Not an interface type.
951951
continue
952952
}
953+
954+
// Collect and print only the methods that match.
955+
var methods []*ast.Field
953956
for _, iMethod := range inter.Methods.List {
954957
// This is an interface, so there can be only one name.
955958
// TODO: Anonymous methods (embedding)
@@ -958,22 +961,21 @@ func (pkg *Package) printMethodDoc(symbol, method string) bool {
958961
}
959962
name := iMethod.Names[0].Name
960963
if match(method, name) {
961-
if iMethod.Doc != nil {
962-
for _, comment := range iMethod.Doc.List {
963-
doc.ToText(&pkg.buf, comment.Text, "", indent, indentedWidth)
964-
}
965-
}
966-
s := pkg.oneLineNode(iMethod.Type)
967-
// Hack: s starts "func" but there is no name present.
968-
// We could instead build a FuncDecl but it's not worthwhile.
969-
lineComment := ""
970-
if iMethod.Comment != nil {
971-
lineComment = fmt.Sprintf(" %s", iMethod.Comment.List[0].Text)
972-
}
973-
pkg.Printf("func %s%s%s\n", name, s[4:], lineComment)
964+
methods = append(methods, iMethod)
974965
found = true
975966
}
976967
}
968+
if found {
969+
pkg.Printf("type %s ", spec.Name)
970+
inter.Methods.List, methods = methods, inter.Methods.List
971+
err := format.Node(&pkg.buf, pkg.fs, inter)
972+
if err != nil {
973+
log.Fatal(err)
974+
}
975+
pkg.newlines(1)
976+
// Restore the original methods.
977+
inter.Methods.List = methods
978+
}
977979
}
978980
return found
979981
}

src/cmd/doc/testdata/pkg.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ const unexportedTypedConstant ExportedType = 1 // In a separate section to test
111111
// Comment about exported interface.
112112
type ExportedInterface interface {
113113
// Comment before exported method.
114+
//
115+
// // Code block showing how to use ExportedMethod
116+
// func DoSomething() error {
117+
// ExportedMethod()
118+
// return nil
119+
// }
120+
//
114121
ExportedMethod() // Comment on line with exported method.
115122
unexportedMethod() // Comment on line with unexported method.
116123
io.Reader // Comment on line with embedded Reader.

0 commit comments

Comments
 (0)