aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2010-05-26 13:44:27 -0700
committerRobert Griesemer <gri@golang.org>2010-05-26 13:44:27 -0700
commita4129988a4d1dea05b9c604131088ece053e2ccb (patch)
tree6a5690211d37299752706857d4c1838fbce9cfdf
parentf199f292e72b97aa0efffb64e5d058e61c5aa682 (diff)
downloadgo-a4129988a4d1dea05b9c604131088ece053e2ccb.tar.gz
go-a4129988a4d1dea05b9c604131088ece053e2ccb.zip
godoc: collect package comments from all package files, not just the first one found
R=r CC=golang-dev https://golang.org/cl/1331041
-rw-r--r--src/pkg/go/doc/doc.go29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/pkg/go/doc/doc.go b/src/pkg/go/doc/doc.go
index 44947b63ac..b73fd285c1 100644
--- a/src/pkg/go/doc/doc.go
+++ b/src/pkg/go/doc/doc.go
@@ -53,6 +53,28 @@ func (doc *docReader) init(pkgName string) {
}
+func (doc *docReader) addDoc(comments *ast.CommentGroup) {
+ if doc.doc == nil {
+ // common case: just one package comment
+ doc.doc = comments
+ return
+ }
+
+ // More than one package comment: Usually there will be only
+ // one file with a package comment, but it's better to collect
+ // all comments than drop them on the floor.
+ // (This code isn't particularly clever - no amortized doubling is
+ // used - but this situation occurs rarely and is not time-critical.)
+ n1 := len(doc.doc.List)
+ n2 := len(comments.List)
+ list := make([]*ast.Comment, n1+1+n2) // + 1 for separator line
+ copy(list, doc.doc.List)
+ list[n1] = &ast.Comment{token.Position{}, []byte("//")} // separator line
+ copy(list[n1+1:], comments.List)
+ doc.doc = &ast.CommentGroup{list}
+}
+
+
func (doc *docReader) addType(decl *ast.GenDecl) {
spec := decl.Specs[0].(*ast.TypeSpec)
typ := doc.lookupTypeDoc(spec.Name.Name())
@@ -275,12 +297,7 @@ var (
func (doc *docReader) addFile(src *ast.File) {
// add package documentation
if src.Doc != nil {
- // TODO(gri) This won't do the right thing if there is more
- // than one file with package comments. Consider
- // using ast.MergePackageFiles which handles these
- // comments correctly (but currently looses BUG(...)
- // comments).
- doc.doc = src.Doc
+ doc.addDoc(src.Doc)
src.Doc = nil // doc consumed - remove from ast.File node
}