Commit fa42157d authored by Gert Cuykens's avatar Gert Cuykens Committed by Andrew Bonventre

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 #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: default avatarAndrew Bonventre <andybons@golang.org>
parent b7e9c7a3
...@@ -210,6 +210,18 @@ var tests = []test{ ...@@ -210,6 +210,18 @@ var tests = []test{
`func \(unexportedType\)`, `func \(unexportedType\)`,
}, },
}, },
// Package dump -short
{
"full package with -short",
[]string{`-short`, p},
[]string{
`const ExportedConstant = 1`, // Simple constant.
`func ReturnUnexported\(\) unexportedType`, // Function with unexported return type.
},
[]string{
`MultiLine(String|Method|Field)`, // No data from multi line portions.
},
},
// Package dump -u // Package dump -u
{ {
"full package with u", "full package with u",
......
...@@ -57,6 +57,7 @@ var ( ...@@ -57,6 +57,7 @@ var (
showAll bool // -all flag showAll bool // -all flag
showCmd bool // -cmd flag showCmd bool // -cmd flag
showSrc bool // -src flag showSrc bool // -src flag
short bool // -short flag
) )
// usage is a replacement usage function for the flags package. // 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) { ...@@ -94,6 +95,7 @@ func do(writer io.Writer, flagSet *flag.FlagSet, args []string) (err error) {
flagSet.BoolVar(&showAll, "all", false, "show all documentation for package") flagSet.BoolVar(&showAll, "all", false, "show all documentation for package")
flagSet.BoolVar(&showCmd, "cmd", false, "show symbols with package docs even if package is a command") flagSet.BoolVar(&showCmd, "cmd", false, "show symbols with package docs even if package is a command")
flagSet.BoolVar(&showSrc, "src", false, "show source code for symbol") flagSet.BoolVar(&showSrc, "src", false, "show source code for symbol")
flagSet.BoolVar(&short, "short", false, "one-line representation for each symbol")
flagSet.Parse(args) flagSet.Parse(args)
var paths []string var paths []string
var symbol, method string var symbol, method string
......
...@@ -507,24 +507,34 @@ func (pkg *Package) allDoc() { ...@@ -507,24 +507,34 @@ func (pkg *Package) allDoc() {
func (pkg *Package) packageDoc() { func (pkg *Package) packageDoc() {
defer pkg.flush() defer pkg.flush()
if !short {
doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth) doc.ToText(&pkg.buf, pkg.doc.Doc, "", indent, indentedWidth)
pkg.newlines(1) pkg.newlines(1)
}
if pkg.pkg.Name == "main" && !showCmd { if pkg.pkg.Name == "main" && !showCmd {
// Show only package docs for commands. // Show only package docs for commands.
return return
} }
if !short {
pkg.newlines(2) // Guarantee blank line before the components. pkg.newlines(2) // Guarantee blank line before the components.
}
pkg.valueSummary(pkg.doc.Consts, false) pkg.valueSummary(pkg.doc.Consts, false)
pkg.valueSummary(pkg.doc.Vars, false) pkg.valueSummary(pkg.doc.Vars, false)
pkg.funcSummary(pkg.doc.Funcs, false) pkg.funcSummary(pkg.doc.Funcs, false)
pkg.typeSummary() pkg.typeSummary()
if !short {
pkg.bugs() pkg.bugs()
}
} }
// packageClause prints the package clause. // packageClause prints the package clause.
func (pkg *Package) packageClause() { func (pkg *Package) packageClause() {
if short {
return
}
importPath := pkg.build.ImportComment importPath := pkg.build.ImportComment
if importPath == "" { if importPath == "" {
importPath = pkg.build.ImportPath importPath = pkg.build.ImportPath
......
...@@ -363,6 +363,8 @@ ...@@ -363,6 +363,8 @@
// Treat a command (package main) like a regular package. // Treat a command (package main) like a regular package.
// Otherwise package main's exported symbols are hidden // Otherwise package main's exported symbols are hidden
// when showing the package's top-level documentation. // when showing the package's top-level documentation.
// -short
// One-line representation for each symbol.
// -src // -src
// Show the full source code for the symbol. This will // Show the full source code for the symbol. This will
// display the full Go source of its declaration and // display the full Go source of its declaration and
......
...@@ -114,6 +114,8 @@ Flags: ...@@ -114,6 +114,8 @@ Flags:
Treat a command (package main) like a regular package. Treat a command (package main) like a regular package.
Otherwise package main's exported symbols are hidden Otherwise package main's exported symbols are hidden
when showing the package's top-level documentation. when showing the package's top-level documentation.
-short
One-line representation for each symbol.
-src -src
Show the full source code for the symbol. This will Show the full source code for the symbol. This will
display the full Go source of its declaration and display the full Go source of its declaration and
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment