forked from zmb3/gogetdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ident.go
276 lines (255 loc) · 6.46 KB
/
ident.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
package main
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"go/types"
"strings"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)
func findTypeSpec(decl *ast.GenDecl, pos token.Pos) *ast.TypeSpec {
for _, spec := range decl.Specs {
typeSpec := spec.(*ast.TypeSpec)
if typeSpec.Pos() == pos {
return typeSpec
}
}
return nil
}
func findVarSpec(decl *ast.GenDecl, pos token.Pos) *ast.ValueSpec {
for _, spec := range decl.Specs {
varSpec := spec.(*ast.ValueSpec)
for _, ident := range varSpec.Names {
if ident.Pos() == pos {
return varSpec
}
}
}
return nil
}
func formatNode(n ast.Node, obj types.Object, prog *packages.Package) string {
// fmt.Printf("formatting %T node\n", n)
qual := func(p *types.Package) string { return "" }
// We'd like to use types.ObjectString(obj, qual) where we can,
// but there are several cases where we must render a copy of the AST
// node with no documentation (we emit that ourselves).
// 1) FuncDecl: ObjectString won't give us the decl for builtins
// 2) TypeSpec: ObjectString does not allow us to trim unexported fields
// 3) GenDecl: we need to find the inner {Type|Var}Spec
var nc ast.Node
switch n := n.(type) {
case *ast.FuncDecl:
cp := *n
cp.Doc = nil
cp.Body = nil // Don't print the whole function body
nc = &cp
case *ast.TypeSpec:
specCp := *n
if !*showUnexportedFields {
trimUnexportedElems(&specCp)
}
specCp.Doc = nil
typeSpec := ast.GenDecl{
Tok: token.TYPE,
Specs: []ast.Spec{&specCp},
}
nc = &typeSpec
case *ast.GenDecl:
cp := *n
cp.Doc = nil
if len(n.Specs) > 0 {
// Only print this one type, not all the types in the gendecl
switch n.Specs[0].(type) {
case *ast.TypeSpec:
spec := findTypeSpec(n, obj.Pos())
if spec != nil {
specCp := *spec
if !*showUnexportedFields {
trimUnexportedElems(&specCp)
}
specCp.Doc = nil
cp.Specs = []ast.Spec{&specCp}
}
cp.Lparen = 0
cp.Rparen = 0
case *ast.ValueSpec:
spec := findVarSpec(n, obj.Pos())
if spec != nil {
specCp := *spec
specCp.Doc = nil
cp.Specs = []ast.Spec{&specCp}
}
cp.Lparen = 0
cp.Rparen = 0
}
}
nc = &cp
case *ast.Field:
return types.ObjectString(obj, qual)
default:
return types.ObjectString(obj, qual)
}
buf := &bytes.Buffer{}
cfg := printer.Config{Mode: printer.UseSpaces | printer.TabIndent, Tabwidth: 8}
err := cfg.Fprint(buf, prog.Fset, nc)
if err != nil {
return obj.String()
}
return stripVendorFromImportPath(buf.String())
}
// IdentDoc attempts to get the documentation for a *ast.Ident.
func IdentDoc(id *ast.Ident, info *types.Info, pkg *packages.Package) (*Doc, error) {
// get definition of identifier
obj := info.ObjectOf(id)
// for anonymous fields, we want the type definition, not the field
if v, ok := obj.(*types.Var); ok && v.Anonymous() {
obj = info.Uses[id]
}
var pos string
if p := obj.Pos(); p.IsValid() {
pos = pkg.Fset.Position(p).String()
}
pkgPath, pkgName := "", ""
if op := obj.Pkg(); op != nil {
pkgPath = op.Path()
pkgName = op.Name()
}
// handle packages imported under a different name
if p, ok := obj.(*types.PkgName); ok {
return PackageDoc(pkg, p.Imported().Path())
}
nodes := pathEnclosingInterval(pkg, obj.Pos(), obj.Pos())
if len(nodes) == 0 {
// special case - builtins
doc, decl := findInBuiltin(obj.Name(), obj, pkg)
if doc != "" {
return &Doc{
Import: "builtin",
Pkg: "builtin",
Name: obj.Name(),
Doc: doc,
Decl: decl,
Pos: pos,
}, nil
}
return nil, fmt.Errorf("no documentation found for %s", obj.Name())
}
var doc *Doc
for _, node := range nodes {
switch node.(type) {
case *ast.Ident:
// continue ascending AST (searching for parent node of the identifier)
continue
case *ast.FuncDecl, *ast.GenDecl, *ast.Field, *ast.TypeSpec, *ast.ValueSpec:
// found the parent node
default:
break
}
doc = &Doc{
Import: stripVendorFromImportPath(pkgPath),
Pkg: pkgName,
Name: obj.Name(),
Decl: formatNode(node, obj, pkg),
Pos: pos,
}
break
}
if doc == nil {
// This shouldn't happen
return nil, fmt.Errorf("no documentation found for %s", obj.Name())
}
for _, node := range nodes {
//fmt.Printf("for %s: found %T\n%#v\n", id.Name, node, node)
switch n := node.(type) {
case *ast.Ident:
continue
case *ast.FuncDecl:
doc.Doc = n.Doc.Text()
return doc, nil
case *ast.Field:
if n.Doc != nil {
doc.Doc = n.Doc.Text()
} else if n.Comment != nil {
doc.Doc = n.Comment.Text()
}
return doc, nil
case *ast.TypeSpec:
if n.Doc != nil {
doc.Doc = n.Doc.Text()
return doc, nil
}
if n.Comment != nil {
doc.Doc = n.Comment.Text()
return doc, nil
}
case *ast.ValueSpec:
if n.Doc != nil {
doc.Doc = n.Doc.Text()
return doc, nil
}
if n.Comment != nil {
doc.Doc = n.Comment.Text()
return doc, nil
}
case *ast.GenDecl:
constValue := ""
if c, ok := obj.(*types.Const); ok {
constValue = c.Val().ExactString()
}
if doc.Doc == "" && n.Doc != nil {
doc.Doc = n.Doc.Text()
}
if constValue != "" {
doc.Doc += fmt.Sprintf("\nConstant Value: %s", constValue)
}
return doc, nil
default:
return doc, nil
}
}
return doc, nil
}
// pathEnclosingInterval returns ast.Node of the package that
// contain source interval [start, end), and all the node's ancestors
// up to the AST root. It searches the ast.Files of initPkg and
// the packages it imports recursively until something is found.
//
// Modified from golang.org/x/tools/go/loader.
func pathEnclosingInterval(initPkg *packages.Package, start, end token.Pos) []ast.Node {
for _, f := range initPkg.Syntax {
if f.Pos() == token.NoPos {
// This can happen if the parser saw
// too many errors and bailed out.
// (Use parser.AllErrors to prevent that.)
continue
}
if !tokenFileContainsPos(initPkg.Fset.File(f.Pos()), start) {
continue
}
if path, _ := astutil.PathEnclosingInterval(f, start, end); path != nil {
return path
}
}
for _, p := range initPkg.Imports {
if path := pathEnclosingInterval(p, start, end); path != nil {
return path
}
}
return nil
}
func tokenFileContainsPos(f *token.File, pos token.Pos) bool {
p := int(pos)
base := f.Base()
return base <= p && p < base+f.Size()
}
func stripVendorFromImportPath(ip string) string {
vendor := "/vendor/"
l := len(vendor)
if i := strings.LastIndex(ip, vendor); i != -1 {
return ip[i+l:]
}
return ip
}