// Interface.
{
- "type",
+ "interface type",
[]string{p, `ExportedInterface`},
[]string{
`Comment about exported interface`, // Include comment.
},
// Interface -u with unexported methods.
{
- "type with unexported methods and -u",
+ "interface type with unexported methods and -u",
[]string{"-u", p, `ExportedInterface`},
[]string{
`Comment about exported interface`, // Include comment.
},
},
+ // Interface method.
+ {
+ "interface method",
+ []string{p, `ExportedInterface.ExportedMethod`},
+ []string{
+ `Comment before exported method.*\n.*ExportedMethod\(\)` +
+ `.*Comment on line with exported method`,
+ },
+ []string{
+ `Comment about exported interface.`,
+ },
+ },
+
// Method.
{
"method",
}
found := false
for _, typ := range types {
- for _, meth := range typ.Methods {
- if match(method, meth.Name) {
- decl := meth.Decl
- decl.Body = nil
- pkg.emit(meth.Doc, decl)
+ if len(typ.Methods) > 0 {
+ for _, meth := range typ.Methods {
+ if match(method, meth.Name) {
+ decl := meth.Decl
+ decl.Body = nil
+ pkg.emit(meth.Doc, decl)
+ found = true
+ }
+ }
+ continue
+ }
+ // Type may be an interface. The go/doc package does not attach
+ // an interface's methods to the doc.Type. We need to dig around.
+ spec := pkg.findTypeSpec(typ.Decl, typ.Name)
+ inter, ok := spec.Type.(*ast.InterfaceType)
+ if !ok {
+ // Not an interface type.
+ // TODO? Maybe handle struct fields here.
+ continue
+ }
+ for _, iMethod := range inter.Methods.List {
+ // This is an interface, so there can be only one name.
+ // TODO: Anonymous methods (embedding)
+ if len(iMethod.Names) == 0 {
+ continue
+ }
+ name := iMethod.Names[0].Name
+ if match(method, name) {
+ // pkg.oneLineField(iMethod, 0)
+ 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)
found = true
}
}