From: Rob Pike Date: Mon, 4 May 2015 17:11:27 +0000 (-0700) Subject: cmd/doc: if no top-level symbols match, look for methods X-Git-Tag: go1.5beta1~709 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=9de28cf5472724b113ed02f03fa7222be4ca0b46;p=gostls13.git cmd/doc: if no top-level symbols match, look for methods Improving the usability further. Before: $ go doc bytes.Read doc: symbol Read not present in package bytes installed in "bytes" $ After: $ go doc bytes.Read func (b *Buffer) Read(p []byte) (n int, err error) Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil. func (r *Reader) Read(b []byte) (n int, err error) $ Change-Id: I646511fada138bd09e9b39820da01a5ccef4a90f Reviewed-on: https://go-review.googlesource.com/9656 Reviewed-by: Russ Cox --- diff --git a/src/cmd/doc/pkg.go b/src/cmd/doc/pkg.go index 580a91f7e6..d52dd97864 100644 --- a/src/cmd/doc/pkg.go +++ b/src/cmd/doc/pkg.go @@ -275,9 +275,10 @@ func (pkg *Package) findFuncs(symbol string) (funcs []*doc.Func) { } // findTypes finds the doc.Types that describes the symbol. +// If symbol is empty, it finds all exported types. func (pkg *Package) findTypes(symbol string) (types []*doc.Type) { for _, typ := range pkg.doc.Types { - if match(symbol, typ.Name) { + if symbol == "" && isExported(typ.Name) || match(symbol, typ.Name) { types = append(types, typ) } } @@ -298,6 +299,7 @@ func (pkg *Package) findTypeSpec(decl *ast.GenDecl, symbol string) *ast.TypeSpec // symbolDoc prints the docs for symbol. There may be multiple matches. // If symbol matches a type, output includes its methods factories and associated constants. +// If there is no top-level symbol, symbolDoc looks for methods that match. func (pkg *Package) symbolDoc(symbol string) { defer pkg.flush() found := false @@ -343,7 +345,10 @@ func (pkg *Package) symbolDoc(symbol string) { found = true } if !found { - log.Fatalf("symbol %s not present in package %s installed in %q", symbol, pkg.name, pkg.build.ImportPath) + // See if there are methods. + if !pkg.printMethodDoc("", symbol) { + log.Printf("symbol %s not present in package %s installed in %q", symbol, pkg.name, pkg.build.ImportPath) + } } } @@ -391,11 +396,16 @@ func trimUnexportedFields(spec *ast.TypeSpec) { } } -// methodDoc prints the docs for matches of symbol.method. -func (pkg *Package) methodDoc(symbol, method string) { +// printMethodDoc prints the docs for matches of symbol.method. +// If symbol is empty, it prints all methods that match the name. +// It reports whether it found any methods. +func (pkg *Package) printMethodDoc(symbol, method string) bool { defer pkg.flush() types := pkg.findTypes(symbol) if types == nil { + if symbol == "" { + return false + } log.Fatalf("symbol %s is not a type in package %s installed in %q", symbol, pkg.name, pkg.build.ImportPath) } found := false @@ -409,7 +419,13 @@ func (pkg *Package) methodDoc(symbol, method string) { } } } - if !found { + return found +} + +// methodDoc prints the docs for matches of symbol.method. +func (pkg *Package) methodDoc(symbol, method string) { + defer pkg.flush() + if !pkg.printMethodDoc(symbol, method) { log.Fatalf("no method %s.%s in package %s installed in %q", symbol, method, pkg.name, pkg.build.ImportPath) } }