LEFT | RIGHT |
(no file at all) | |
1 // Copyright 2009 The Go Authors. All rights reserved. | 1 // Copyright 2009 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // The doc package extracts source code documentation from a Go AST. | 5 // The doc package extracts source code documentation from a Go AST. |
6 package doc | 6 package doc |
7 | 7 |
8 import ( | 8 import ( |
9 "go/ast" | 9 "go/ast" |
10 "go/token" | 10 "go/token" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 // More than one package comment: Usually there will be only | 60 // More than one package comment: Usually there will be only |
61 // one file with a package comment, but it's better to collect | 61 // one file with a package comment, but it's better to collect |
62 // all comments than drop them on the floor. | 62 // all comments than drop them on the floor. |
63 // (This code isn't particularly clever - no amortized doubling is | 63 // (This code isn't particularly clever - no amortized doubling is |
64 // used - but this situation occurs rarely and is not time-critical.) | 64 // used - but this situation occurs rarely and is not time-critical.) |
65 n1 := len(doc.doc.List) | 65 n1 := len(doc.doc.List) |
66 n2 := len(comments.List) | 66 n2 := len(comments.List) |
67 list := make([]*ast.Comment, n1+1+n2) // + 1 for separator line | 67 list := make([]*ast.Comment, n1+1+n2) // + 1 for separator line |
68 copy(list, doc.doc.List) | 68 copy(list, doc.doc.List) |
69 » list[n1] = &ast.Comment{token.NoPos, []byte("//")} // separator line | 69 » list[n1] = &ast.Comment{token.NoPos, "//"} // separator line |
70 copy(list[n1+1:], comments.List) | 70 copy(list[n1+1:], comments.List) |
71 doc.doc = &ast.CommentGroup{list} | 71 doc.doc = &ast.CommentGroup{list} |
72 } | 72 } |
73 | 73 |
74 | 74 |
75 func (doc *docReader) addType(decl *ast.GenDecl) { | 75 func (doc *docReader) addType(decl *ast.GenDecl) { |
76 spec := decl.Specs[0].(*ast.TypeSpec) | 76 spec := decl.Specs[0].(*ast.TypeSpec) |
77 typ := doc.lookupTypeDoc(spec.Name.Name) | 77 typ := doc.lookupTypeDoc(spec.Name.Name) |
78 // typ should always be != nil since declared types | 78 // typ should always be != nil since declared types |
79 // are always named - be conservative and check | 79 // are always named - be conservative and check |
(...skipping 18 matching lines...) Expand all Loading... |
98 return tdoc | 98 return tdoc |
99 } | 99 } |
100 | 100 |
101 | 101 |
102 func baseTypeName(typ ast.Expr) string { | 102 func baseTypeName(typ ast.Expr) string { |
103 switch t := typ.(type) { | 103 switch t := typ.(type) { |
104 case *ast.Ident: | 104 case *ast.Ident: |
105 // if the type is not exported, the effect to | 105 // if the type is not exported, the effect to |
106 // a client is as if there were no type name | 106 // a client is as if there were no type name |
107 if t.IsExported() { | 107 if t.IsExported() { |
108 » » » return string(t.Name) | 108 » » » return t.Name |
109 } | 109 } |
110 case *ast.StarExpr: | 110 case *ast.StarExpr: |
111 return baseTypeName(t.X) | 111 return baseTypeName(t.X) |
112 } | 112 } |
113 return "" | 113 return "" |
114 } | 114 } |
115 | 115 |
116 | 116 |
117 func (doc *docReader) addValue(decl *ast.GenDecl) { | 117 func (doc *docReader) addValue(decl *ast.GenDecl) { |
118 // determine if decl should be associated with a type | 118 // determine if decl should be associated with a type |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 } | 293 } |
294 | 294 |
295 // add all declarations | 295 // add all declarations |
296 for _, decl := range src.Decls { | 296 for _, decl := range src.Decls { |
297 doc.addDecl(decl) | 297 doc.addDecl(decl) |
298 } | 298 } |
299 | 299 |
300 // collect BUG(...) comments | 300 // collect BUG(...) comments |
301 for _, c := range src.Comments { | 301 for _, c := range src.Comments { |
302 text := c.List[0].Text | 302 text := c.List[0].Text |
303 » » if m := bug_markers.FindIndex(text); m != nil { | 303 » » if m := bug_markers.FindStringIndex(text); m != nil { |
304 // found a BUG comment; maybe empty | 304 // found a BUG comment; maybe empty |
305 » » » if btxt := text[m[1]:]; bug_content.Match(btxt) { | 305 » » » if btxt := text[m[1]:]; bug_content.MatchString(btxt) { |
306 // non-empty BUG comment; collect comment withou
t BUG prefix | 306 // non-empty BUG comment; collect comment withou
t BUG prefix |
307 list := copyCommentList(c.List) | 307 list := copyCommentList(c.List) |
308 list[0].Text = text[m[1]:] | 308 list[0].Text = text[m[1]:] |
309 doc.bugs = append(doc.bugs, &ast.CommentGroup{li
st}) | 309 doc.bugs = append(doc.bugs, &ast.CommentGroup{li
st}) |
310 } | 310 } |
311 } | 311 } |
312 } | 312 } |
313 src.Comments = nil // consumed unassociated comments - remove from ast.F
ile node | 313 src.Comments = nil // consumed unassociated comments - remove from ast.F
ile node |
314 } | 314 } |
315 | 315 |
(...skipping 325 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 // Filter eliminates documentation for names that don't pass through the filter
f. | 641 // Filter eliminates documentation for names that don't pass through the filter
f. |
642 // TODO: Recognize "Type.Method" as a name. | 642 // TODO: Recognize "Type.Method" as a name. |
643 // | 643 // |
644 func (p *PackageDoc) Filter(f Filter) { | 644 func (p *PackageDoc) Filter(f Filter) { |
645 p.Consts = filterValueDocs(p.Consts, f) | 645 p.Consts = filterValueDocs(p.Consts, f) |
646 p.Vars = filterValueDocs(p.Vars, f) | 646 p.Vars = filterValueDocs(p.Vars, f) |
647 p.Types = filterTypeDocs(p.Types, f) | 647 p.Types = filterTypeDocs(p.Types, f) |
648 p.Funcs = filterFuncDocs(p.Funcs, f) | 648 p.Funcs = filterFuncDocs(p.Funcs, f) |
649 p.Doc = "" // don't show top-level package doc | 649 p.Doc = "" // don't show top-level package doc |
650 } | 650 } |
LEFT | RIGHT |