Commit 108f5c91 authored by Robert Griesemer's avatar Robert Griesemer

godoc: only show directories containing true package files

(ignore directories containing *.go files that don't
actually start with a package clause)

R=r
CC=golang-dev
https://golang.org/cl/2223041
parent abb73a4a
...@@ -101,35 +101,40 @@ func (b *treeBuilder) newDirTree(path, name string, depth int) *Directory { ...@@ -101,35 +101,40 @@ func (b *treeBuilder) newDirTree(path, name string, depth int) *Directory {
list, _ := ioutil.ReadDir(path) // ignore errors list, _ := ioutil.ReadDir(path) // ignore errors
// determine number of subdirectories and package files // determine number of subdirectories and if there are package files
ndirs := 0 ndirs := 0
nfiles := 0 hasPkgFiles := false
var synopses [4]string // prioritized package documentation (0 == highest priority) var synopses [4]string // prioritized package documentation (0 == highest priority)
for _, d := range list { for _, d := range list {
switch { switch {
case isPkgDir(d): case isPkgDir(d):
ndirs++ ndirs++
case isPkgFile(d): case isPkgFile(d):
nfiles++ // looks like a package file, but may just be a file ending in ".go";
// don't just count it yet (otherwise we may end up with hasPkgFiles even
// though the directory doesn't contain any real package files - was bug)
if synopses[0] == "" { if synopses[0] == "" {
// no "optimal" package synopsis yet; continue to collect synopses // no "optimal" package synopsis yet; continue to collect synopses
file, err := parser.ParseFile(pathutil.Join(path, d.Name), nil, file, err := parser.ParseFile(pathutil.Join(path, d.Name), nil,
parser.ParseComments|parser.PackageClauseOnly) parser.ParseComments|parser.PackageClauseOnly)
if err == nil && file.Doc != nil { if err == nil {
// prioritize documentation hasPkgFiles = true
i := -1 if file.Doc != nil {
switch file.Name.Name { // prioritize documentation
case name: i := -1
i = 0 // normal case: directory name matches package name switch file.Name.Name {
case fakePkgName: case name:
i = 1 // synopses for commands i = 0 // normal case: directory name matches package name
case "main": case fakePkgName:
i = 2 // directory contains a main package i = 1 // synopses for commands
default: case "main":
i = 3 // none of the above i = 2 // directory contains a main package
} default:
if 0 <= i && i < len(synopses) && synopses[i] == "" { i = 3 // none of the above
synopses[i] = firstSentence(doc.CommentText(file.Doc)) }
if 0 <= i && i < len(synopses) && synopses[i] == "" {
synopses[i] = firstSentence(doc.CommentText(file.Doc))
}
} }
} }
} }
...@@ -154,8 +159,8 @@ func (b *treeBuilder) newDirTree(path, name string, depth int) *Directory { ...@@ -154,8 +159,8 @@ func (b *treeBuilder) newDirTree(path, name string, depth int) *Directory {
} }
// if there are no package files and no subdirectories // if there are no package files and no subdirectories
// (with package files), ignore the directory // containing package files, ignore the directory
if nfiles == 0 && len(dirs) == 0 { if !hasPkgFiles && len(dirs) == 0 {
return nil return nil
} }
......
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