]> Cypherpunks repositories - gostls13.git/commitdiff
go/doc, godoc: fix range of type declarations
authorRobert Griesemer <gri@golang.org>
Fri, 24 Feb 2012 21:44:22 +0000 (13:44 -0800)
committerRobert Griesemer <gri@golang.org>
Fri, 24 Feb 2012 21:44:22 +0000 (13:44 -0800)
For grouped type declarations, go/doc introduces
fake individual declarations. Don't use the original
location of the "type" keyword because it will lead
to an overly large source code range for that fake
declaration, and thus an overly large selection shown
via godoc (e.g.: click on the AssignStmt link for:
http://golang.org/pkg/go/ast/#AssignStmt ).

Also: Don't create a fake declaration if not needed.

R=golang-dev, rsc
CC=golang-dev
https://golang.org/cl/5694061

src/pkg/go/doc/reader.go

index 3558892ebd0c2a411cf840ce2a290d37fc76ef2d..5eaae37b7decdf0b9c21197561f4a31e538e7e07 100644 (file)
@@ -432,6 +432,17 @@ func (r *reader) readFile(src *ast.File) {
                                r.readValue(d)
                        case token.TYPE:
                                // types are handled individually
+                               if len(d.Specs) == 1 && !d.Lparen.IsValid() {
+                                       // common case: single declaration w/o parentheses
+                                       // (if a single declaration is parenthesized,
+                                       // create a new fake declaration below, so that
+                                       // go/doc type declarations always appear w/o
+                                       // parentheses)
+                                       if s, ok := d.Specs[0].(*ast.TypeSpec); ok {
+                                               r.readType(d, s)
+                                       }
+                                       break
+                               }
                                for _, spec := range d.Specs {
                                        if s, ok := spec.(*ast.TypeSpec); ok {
                                                // use an individual (possibly fake) declaration
@@ -439,8 +450,13 @@ func (r *reader) readFile(src *ast.File) {
                                                // gets to (re-)use the declaration documentation
                                                // if there's none associated with the spec itself
                                                fake := &ast.GenDecl{
-                                                       Doc:    d.Doc,
-                                                       TokPos: d.Pos(),
+                                                       Doc: d.Doc,
+                                                       // don't use the existing TokPos because it
+                                                       // will lead to the wrong selection range for
+                                                       // the fake declaration if there are more
+                                                       // than one type in the group (this affects
+                                                       // src/cmd/godoc/godoc.go's posLink_urlFunc)
+                                                       TokPos: s.Pos(),
                                                        Tok:    token.TYPE,
                                                        Specs:  []ast.Spec{s},
                                                }