diff options
author | Robert Griesemer <gri@golang.org> | 2010-03-29 18:06:53 -0700 |
---|---|---|
committer | Robert Griesemer <gri@golang.org> | 2010-03-29 18:06:53 -0700 |
commit | 45b2977537e944dc5de474ec33fe32ce6caecf9d (patch) | |
tree | 3583474e6888f38997d416655a2ca889b6b3a467 /src/pkg/go/doc/doc.go | |
parent | 0eff02c3398a847baf2042750790676fe4e52100 (diff) | |
download | golang-45b2977537e944dc5de474ec33fe32ce6caecf9d.tar.gz |
godoc: support for filtering of command-line output in -src mode
+ various minor cleanups
Usage: godoc -src math Sin
R=rsc
CC=golang-dev
http://codereview.appspot.com/791041
Diffstat (limited to 'src/pkg/go/doc/doc.go')
-rw-r--r-- | src/pkg/go/doc/doc.go | 61 |
1 files changed, 20 insertions, 41 deletions
diff --git a/src/pkg/go/doc/doc.go b/src/pkg/go/doc/doc.go index d7e404f14..44947b63a 100644 --- a/src/pkg/go/doc/doc.go +++ b/src/pkg/go/doc/doc.go @@ -10,7 +10,6 @@ import ( "go/ast" "go/token" "regexp" - "strings" "sort" ) @@ -563,38 +562,20 @@ func (doc *docReader) newDoc(importpath string, filenames []string) *PackageDoc // ---------------------------------------------------------------------------- // Filtering by name -// Does s look like a regular expression? -func isRegexp(s string) bool { - return strings.IndexAny(s, ".(|)*+?^$[]") >= 0 -} - - -func match(s string, names []string) bool { - for _, t := range names { - if isRegexp(t) { - if matched, _ := regexp.MatchString(t, s); matched { - return true - } - } - if s == t { - return true - } - } - return false -} +type Filter func(string) bool -func matchDecl(d *ast.GenDecl, names []string) bool { +func matchDecl(d *ast.GenDecl, f Filter) bool { for _, d := range d.Specs { switch v := d.(type) { case *ast.ValueSpec: for _, name := range v.Names { - if match(name.Name(), names) { + if f(name.Name()) { return true } } case *ast.TypeSpec: - if match(v.Name.Name(), names) { + if f(v.Name.Name()) { return true } } @@ -603,10 +584,10 @@ func matchDecl(d *ast.GenDecl, names []string) bool { } -func filterValueDocs(a []*ValueDoc, names []string) []*ValueDoc { +func filterValueDocs(a []*ValueDoc, f Filter) []*ValueDoc { w := 0 for _, vd := range a { - if matchDecl(vd.Decl, names) { + if matchDecl(vd.Decl, f) { a[w] = vd w++ } @@ -615,10 +596,10 @@ func filterValueDocs(a []*ValueDoc, names []string) []*ValueDoc { } -func filterFuncDocs(a []*FuncDoc, names []string) []*FuncDoc { +func filterFuncDocs(a []*FuncDoc, f Filter) []*FuncDoc { w := 0 for _, fd := range a { - if match(fd.Name, names) { + if f(fd.Name) { a[w] = fd w++ } @@ -627,18 +608,18 @@ func filterFuncDocs(a []*FuncDoc, names []string) []*FuncDoc { } -func filterTypeDocs(a []*TypeDoc, names []string) []*TypeDoc { +func filterTypeDocs(a []*TypeDoc, f Filter) []*TypeDoc { w := 0 for _, td := range a { n := 0 // number of matches - if matchDecl(td.Decl, names) { + if matchDecl(td.Decl, f) { n = 1 } else { // type name doesn't match, but we may have matching consts, vars, factories or methods - td.Consts = filterValueDocs(td.Consts, names) - td.Vars = filterValueDocs(td.Vars, names) - td.Factories = filterFuncDocs(td.Factories, names) - td.Methods = filterFuncDocs(td.Methods, names) + td.Consts = filterValueDocs(td.Consts, f) + td.Vars = filterValueDocs(td.Vars, f) + td.Factories = filterFuncDocs(td.Factories, f) + td.Methods = filterFuncDocs(td.Methods, f) n += len(td.Consts) + len(td.Vars) + len(td.Factories) + len(td.Methods) } if n > 0 { @@ -650,15 +631,13 @@ func filterTypeDocs(a []*TypeDoc, names []string) []*TypeDoc { } -// Filter eliminates information from d that is not -// about one of the given names. +// Filter eliminates documentation for names that don't pass through the filter f. // TODO: Recognize "Type.Method" as a name. -// TODO(r): maybe precompile the regexps. // -func (p *PackageDoc) Filter(names []string) { - p.Consts = filterValueDocs(p.Consts, names) - p.Vars = filterValueDocs(p.Vars, names) - p.Types = filterTypeDocs(p.Types, names) - p.Funcs = filterFuncDocs(p.Funcs, names) +func (p *PackageDoc) Filter(f Filter) { + p.Consts = filterValueDocs(p.Consts, f) + p.Vars = filterValueDocs(p.Vars, f) + p.Types = filterTypeDocs(p.Types, f) + p.Funcs = filterFuncDocs(p.Funcs, f) p.Doc = "" // don't show top-level package doc } |