Skip to content

Commit a33e9cf

Browse files
committed
cmd/doc: group constructors with type in package presentation
Fixes #14004. $ go doc encoding.gob Before: func Register(value interface{}) func RegisterName(name string, value interface{}) func NewDecoder(r io.Reader) *Decoder func NewEncoder(w io.Writer) *Encoder type CommonType struct { ... } type Decoder struct { ... } type Encoder struct { ... } type GobDecoder interface { ... } type GobEncoder interface { ... } After: func Register(value interface{}) func RegisterName(name string, value interface{}) type CommonType struct { ... } type Decoder struct { ... } func NewDecoder(r io.Reader) *Decoder type Encoder struct { ... } func NewEncoder(w io.Writer) *Encoder type GobDecoder interface { ... } type GobEncoder interface { ... } Change-Id: I021db25bce4a16b3dfa22ab323ca1f4e68d50111 Reviewed-on: https://go-review.googlesource.com/22354 Reviewed-by: Robert Griesemer <[email protected]>
1 parent 8ad8d7d commit a33e9cf

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/cmd/doc/pkg.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ func (pkg *Package) packageDoc() {
268268
pkg.newlines(2) // Guarantee blank line before the components.
269269
pkg.valueSummary(pkg.doc.Consts)
270270
pkg.valueSummary(pkg.doc.Vars)
271-
pkg.funcSummary(pkg.doc.Funcs)
271+
pkg.funcSummary(pkg.doc.Funcs, false)
272272
pkg.typeSummary()
273273
pkg.bugs()
274274
}
@@ -308,24 +308,44 @@ func (pkg *Package) valueSummary(values []*doc.Value) {
308308
}
309309
}
310310

311-
// funcSummary prints a one-line summary for each function.
312-
func (pkg *Package) funcSummary(funcs []*doc.Func) {
311+
// funcSummary prints a one-line summary for each function. Constructors
312+
// are printed by typeSummary, below, and so can be suppressed here.
313+
func (pkg *Package) funcSummary(funcs []*doc.Func, showConstructors bool) {
314+
// First, identify the constructors. Don't bother figuring out if they're exported.
315+
var isConstructor map[*doc.Func]bool
316+
if !showConstructors {
317+
isConstructor = make(map[*doc.Func]bool)
318+
for _, typ := range pkg.doc.Types {
319+
for _, constructor := range typ.Funcs {
320+
isConstructor[constructor] = true
321+
}
322+
}
323+
}
313324
for _, fun := range funcs {
314325
decl := fun.Decl
315326
// Exported functions only. The go/doc package does not include methods here.
316327
if isExported(fun.Name) {
317-
pkg.oneLineFunc(decl)
328+
if !isConstructor[fun] {
329+
pkg.oneLineFunc(decl)
330+
}
318331
}
319332
}
320333
}
321334

322-
// typeSummary prints a one-line summary for each type.
335+
// typeSummary prints a one-line summary for each type, followed by its constructors.
323336
func (pkg *Package) typeSummary() {
324337
for _, typ := range pkg.doc.Types {
325338
for _, spec := range typ.Decl.Specs {
326339
typeSpec := spec.(*ast.TypeSpec) // Must succeed.
327340
if isExported(typeSpec.Name.Name) {
328341
pkg.oneLineTypeDecl(typeSpec)
342+
// Now print the constructors.
343+
for _, constructor := range typ.Funcs {
344+
if isExported(constructor.Name) {
345+
pkg.Printf(indent)
346+
pkg.oneLineFunc(constructor.Decl)
347+
}
348+
}
329349
}
330350
}
331351
}
@@ -453,8 +473,8 @@ func (pkg *Package) symbolDoc(symbol string) bool {
453473
}
454474
pkg.valueSummary(typ.Consts)
455475
pkg.valueSummary(typ.Vars)
456-
pkg.funcSummary(typ.Funcs)
457-
pkg.funcSummary(typ.Methods)
476+
pkg.funcSummary(typ.Funcs, true)
477+
pkg.funcSummary(typ.Methods, true)
458478
found = true
459479
}
460480
if !found {

0 commit comments

Comments
 (0)