Skip to content

Commit fa42157

Browse files
gertcuykensandybons
authored andcommitted
cmd/doc: add option to output a clean one-line symbol representation
Currently there is no way for go doc to output a clean one-line symbol representation of types, functions, vars and consts without documentation lines or other text lines added. For example `go doc fmt` has a huge introduction so if you pass that to grep or fzf to search a symbol let say scan `go doc fmt | grep scan` you get way to many false positives. Added a `-short` flag to be able to do `go doc -short fmt | grep scan` instead which will result in just the symbols you are looking for. func Fscan(r io.Reader, a ...interface{}) (n int, err error) func Fscanf(r io.Reader, format string, a ...interface{}) (n int, err error) func Fscanln(r io.Reader, a ...interface{}) (n int, err error) func Sscan(str string, a ...interface{}) (n int, err error) func Sscanf(str string, format string, a ...interface{}) (n int, err error) func Sscanln(str string, a ...interface{}) (n int, err error) Fixes golang#32597 Change-Id: I77a73838adc512c8d1490f5a82075de6b0462a31 Reviewed-on: https://go-review.googlesource.com/c/go/+/184017 Run-TryBot: Andrew Bonventre <andybons@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Andrew Bonventre <andybons@golang.org>
1 parent b7e9c7a commit fa42157

File tree

5 files changed

+32
-4
lines changed

5 files changed

+32
-4
lines changed

src/cmd/doc/doc_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,18 @@ var tests = []test{
210210
`func \(unexportedType\)`,
211211
},
212212
},
213+
// Package dump -short
214+
{
215+
"full package with -short",
216+
[]string{`-short`, p},
217+
[]string{
218+
`const ExportedConstant = 1`, // Simple constant.
219+
`func ReturnUnexported\(\) unexportedType`, // Function with unexported return type.
220+
},
221+
[]string{
222+
`MultiLine(String|Method|Field)`, // No data from multi line portions.
223+
},
224+
},
213225
// Package dump -u
214226
{
215227
"full package with u",

src/cmd/doc/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ var (
5757
showAll bool // -all flag
5858
showCmd bool // -cmd flag
5959
showSrc bool // -src flag
60+
short bool // -short flag
6061
)
6162

6263
// usage is a replacement usage function for the flags package.
@@ -94,6 +95,7 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
9495
flagSet.BoolVar(&showAll, "all", false, "show all documentation for package")
9596
flagSet.BoolVar(&showCmd, "cmd", false, "show symbols with package docs even if package is a command")
9697
flagSet.BoolVar(&showSrc, "src", false, "show source code for symbol")
98+
flagSet.BoolVar(&short, "short", false, "one-line representation for each symbol")
9799
flagSet.Parse(args)
98100
var paths []string
99101
var symbol, method string

src/cmd/doc/pkg.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,24 +507,34 @@ func (pkg *Package) allDoc() {
507507
func (pkg *Package) packageDoc() {
508508
defer pkg.flush()
509509

510-
doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
511-
pkg.newlines(1)
510+
if !short {
511+
doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
512+
pkg.newlines(1)
513+
}
512514

513515
if pkg.pkg.Name == "main" && !showCmd {
514516
// Show only package docs for commands.
515517
return
516518
}
517519

518-
pkg.newlines(2) // Guarantee blank line before the components.
520+
if !short {
521+
pkg.newlines(2) // Guarantee blank line before the components.
522+
}
523+
519524
pkg.valueSummary(pkg.doc.Consts, false)
520525
pkg.valueSummary(pkg.doc.Vars, false)
521526
pkg.funcSummary(pkg.doc.Funcs, false)
522527
pkg.typeSummary()
523-
pkg.bugs()
528+
if !short {
529+
pkg.bugs()
530+
}
524531
}
525532

526533
// packageClause prints the package clause.
527534
func (pkg *Package) packageClause() {
535+
if short {
536+
return
537+
}
528538
importPath := pkg.build.ImportComment
529539
if importPath == "" {
530540
importPath = pkg.build.ImportPath

src/cmd/go/alldocs.go

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cmd/go/internal/doc/doc.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Flags:
114114
Treat a command (package main) like a regular package.
115115
Otherwise package main's exported symbols are hidden
116116
when showing the package's top-level documentation.
117+
-short
118+
One-line representation for each symbol.
117119
-src
118120
Show the full source code for the symbol. This will
119121
display the full Go source of its declaration and

0 commit comments

Comments
 (0)