@@ -31,18 +31,36 @@ const (
31
31
)
32
32
33
33
type Package struct {
34
- writer io.Writer // Destination for output.
35
- name string // Package name, json for encoding/json.
36
- userPath string // String the user used to find this package.
37
- pkg * ast.Package // Parsed package.
38
- file * ast.File // Merged from all files in the package
39
- doc * doc.Package
40
- build * build.Package
41
- typedValue map [* doc.Value ]bool // Consts and vars related to types.
42
- constructor map [* doc.Func ]bool // Constructors.
43
- packageClausePrinted bool // Prevent repeated package clauses.
44
- fs * token.FileSet // Needed for printing.
45
- buf bytes.Buffer
34
+ writer io.Writer // Destination for output.
35
+ name string // Package name, json for encoding/json.
36
+ userPath string // String the user used to find this package.
37
+ pkg * ast.Package // Parsed package.
38
+ file * ast.File // Merged from all files in the package
39
+ doc * doc.Package
40
+ build * build.Package
41
+ typedValue map [* doc.Value ]bool // Consts and vars related to types.
42
+ constructor map [* doc.Func ]bool // Constructors.
43
+ fs * token.FileSet // Needed for printing.
44
+ buf pkgBuffer
45
+ }
46
+
47
+ // pkgBuffer is a wrapper for bytes.Buffer that prints a package clause the
48
+ // first time Write is called.
49
+ type pkgBuffer struct {
50
+ pkg * Package
51
+ printed bool // Prevent repeated package clauses.
52
+ bytes.Buffer
53
+ }
54
+
55
+ func (pb * pkgBuffer ) Write (p []byte ) (int , error ) {
56
+ if ! pb .printed && len (p ) > 0 {
57
+ pb .printed = true
58
+ // Only show package clause for commands if requested explicitly.
59
+ if pb .pkg .pkg .Name != "main" || showCmd {
60
+ pb .pkg .packageClause ()
61
+ }
62
+ }
63
+ return pb .Buffer .Write (p )
46
64
}
47
65
48
66
type PackageError string // type returned by pkg.Fatalf.
@@ -171,7 +189,7 @@ func parsePackage(writer io.Writer, pkg *build.Package, userPath string) *Packag
171
189
}
172
190
}
173
191
174
- return & Package {
192
+ p := & Package {
175
193
writer : writer ,
176
194
name : pkg .Name ,
177
195
userPath : userPath ,
@@ -183,6 +201,8 @@ func parsePackage(writer io.Writer, pkg *build.Package, userPath string) *Packag
183
201
build : pkg ,
184
202
fs : fs ,
185
203
}
204
+ p .buf .pkg = p
205
+ return p
186
206
}
187
207
188
208
func (pkg * Package ) Printf (format string , args ... interface {}) {
@@ -426,9 +446,6 @@ func joinStrings(ss []string) string {
426
446
// allDoc prints all the docs for the package.
427
447
func (pkg * Package ) allDoc () {
428
448
defer pkg .flush ()
429
- if pkg .showInternals () {
430
- pkg .packageClause (false )
431
- }
432
449
433
450
doc .ToText (& pkg .buf , pkg .doc .Doc , "" , indent , indentedWidth )
434
451
pkg .newlines (1 )
@@ -489,14 +506,11 @@ func (pkg *Package) allDoc() {
489
506
// packageDoc prints the docs for the package (package doc plus one-liners of the rest).
490
507
func (pkg * Package ) packageDoc () {
491
508
defer pkg .flush ()
492
- if pkg .showInternals () {
493
- pkg .packageClause (false )
494
- }
495
509
496
510
doc .ToText (& pkg .buf , pkg .doc .Doc , "" , indent , indentedWidth )
497
511
pkg .newlines (1 )
498
512
499
- if ! pkg .showInternals () {
513
+ if pkg .pkg . Name == "main" && ! showCmd {
500
514
// Show only package docs for commands.
501
515
return
502
516
}
@@ -509,29 +523,8 @@ func (pkg *Package) packageDoc() {
509
523
pkg .bugs ()
510
524
}
511
525
512
- // showInternals reports whether we should show the internals
513
- // of a package as opposed to just the package docs.
514
- // Used to decide whether to suppress internals for commands.
515
- // Called only by Package.packageDoc.
516
- func (pkg * Package ) showInternals () bool {
517
- return pkg .pkg .Name != "main" || showCmd
518
- }
519
-
520
526
// packageClause prints the package clause.
521
- // The argument boolean, if true, suppresses the output if the
522
- // user's argument is identical to the actual package path or
523
- // is empty, meaning it's the current directory.
524
- func (pkg * Package ) packageClause (checkUserPath bool ) {
525
- if pkg .packageClausePrinted {
526
- return
527
- }
528
-
529
- if checkUserPath {
530
- if pkg .userPath == "" || pkg .userPath == pkg .build .ImportPath {
531
- return
532
- }
533
- }
534
-
527
+ func (pkg * Package ) packageClause () {
535
528
importPath := pkg .build .ImportComment
536
529
if importPath == "" {
537
530
importPath = pkg .build .ImportPath
@@ -563,7 +556,6 @@ func (pkg *Package) packageClause(checkUserPath bool) {
563
556
if ! usingModules && importPath != pkg .build .ImportPath {
564
557
pkg .Printf ("WARNING: package source is installed in %q\n " , pkg .build .ImportPath )
565
558
}
566
- pkg .packageClausePrinted = true
567
559
}
568
560
569
561
// valueSummary prints a one-line summary for each set of values and constants.
@@ -701,9 +693,6 @@ func (pkg *Package) symbolDoc(symbol string) bool {
701
693
found := false
702
694
// Functions.
703
695
for _ , fun := range pkg .findFuncs (symbol ) {
704
- if ! found {
705
- pkg .packageClause (true )
706
- }
707
696
// Symbol is a function.
708
697
decl := fun .Decl
709
698
pkg .emit (fun .Doc , decl )
0 commit comments