-
Notifications
You must be signed in to change notification settings - Fork 14
/
m_doc.go
282 lines (258 loc) · 6.64 KB
/
m_doc.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
277
278
279
280
281
282
package main
import (
"go/ast"
"go/parser"
"go/token"
"path"
"path/filepath"
"runtime"
"sort"
"strings"
)
type Doc struct {
Src string `json:"src"`
Pkg string `json:"pkg"`
Name string `json:"name"`
Kind string `json:"kind"`
Fn string `json:"fn"`
Row int `json:"row"`
Col int `json:"col"`
}
type mDoc struct {
Fn string
Src string
Env map[string]string
Offset int
TabIndent bool
TabWidth int
}
func (m *mDoc) Call() (interface{}, string) {
res := []*Doc{}
fset, af, err := parseAstFile(m.Fn, m.Src, parser.ParseComments)
if err != nil {
return res, err.Error()
}
sel, id := identAt(fset, af, m.Offset)
if id == nil {
return res, ""
}
pkgs, _ := parser.ParseDir(fset, filepath.Dir(m.Fn), fiHasGoExt, parser.ParseComments)
if pkgs == nil {
pkgs = map[string]*ast.Package{}
}
pkgName := af.Name.Name
files := map[string]*ast.File{}
pkg, _ := pkgs[pkgName]
if pkg != nil {
files = pkg.Files
}
files[m.Fn] = af
pkg, _ = ast.NewPackage(fset, files, nil, nil)
if pkg == nil {
return res, ""
}
pkgs[pkg.Name] = pkg
obj, pkg, objPkgs := findUnderlyingObj(fset, af, pkg, pkgs, rootDirs(m.Env), sel, id)
if obj != nil {
res = append(res, objDoc(fset, pkg, m.TabIndent, m.TabWidth, obj))
if objPkgs != nil {
xName := "Example" + obj.Name
xPrefix := xName + "_"
for _, objPkg := range objPkgs {
xPkg, _ := ast.NewPackage(fset, objPkg.Files, nil, nil)
if xPkg == nil || xPkg.Scope == nil {
continue
}
for _, xObj := range xPkg.Scope.Objects {
if xObj.Name == xName || strings.HasPrefix(xObj.Name, xPrefix) {
res = append(res, objDoc(fset, xPkg, m.TabIndent, m.TabWidth, xObj))
}
}
}
}
}
return res, ""
}
func init() {
registry.Register("doc2", func(_ *Broker) Caller {
return &mDoc{
Env: map[string]string{},
}
})
}
func objDoc(fset *token.FileSet, pkg *ast.Package, tabIndent bool, tabWidth int, obj *ast.Object) *Doc {
decl := obj.Decl
kind := obj.Kind.String()
tp := fset.Position(obj.Pos())
objSrc := ""
pkgName := ""
if pkg != nil && pkg.Name != "builtin" {
pkgName = pkg.Name
}
if obj.Kind == ast.Pkg {
pkgName = ""
doc := ""
// special-case `package name` is generated as a TypeSpec
if v, ok := obj.Decl.(*ast.TypeSpec); ok && v.Doc != nil {
doc = "/*\n" + v.Doc.Text() + "\n*/\n"
}
objSrc = doc + "package " + obj.Name
} else if af, ok := pkg.Files[tp.Filename]; ok {
switch decl.(type) {
case *ast.TypeSpec, *ast.ValueSpec, *ast.Field:
line := tp.Line - 1
for _, cg := range af.Comments {
cgp := fset.Position(cg.End())
if cgp.Filename == tp.Filename && cgp.Line == line {
switch v := decl.(type) {
case *ast.TypeSpec:
v.Doc = cg
case *ast.ValueSpec:
v.Doc = cg
case *ast.Field:
pkgName = ""
kind = "field"
}
break
}
}
}
}
if objSrc == "" {
objSrc, _ = printSrc(fset, decl, tabIndent, tabWidth)
}
return &Doc{
Src: objSrc,
Pkg: pkgName,
Name: obj.Name,
Kind: kind,
Fn: tp.Filename,
Row: tp.Line - 1,
Col: tp.Column - 1,
}
}
func isBetween(n, start, end int) bool {
return (n >= start && n <= end)
}
func identAt(fset *token.FileSet, af *ast.File, offset int) (sel *ast.SelectorExpr, id *ast.Ident) {
ast.Inspect(af, func(n ast.Node) bool {
if n != nil {
start := fset.Position(n.Pos())
end := fset.Position(n.End())
if isBetween(offset, start.Offset, end.Offset) {
switch v := n.(type) {
case *ast.SelectorExpr:
sel = v
case *ast.Ident:
id = v
}
}
}
return true
})
return
}
func findUnderlyingObj(fset *token.FileSet, af *ast.File, pkg *ast.Package, pkgs map[string]*ast.Package, srcRootDirs []string, sel *ast.SelectorExpr, id *ast.Ident) (*ast.Object, *ast.Package, map[string]*ast.Package) {
if id != nil && id.Obj != nil {
return id.Obj, pkg, pkgs
}
if id == nil {
// can this ever happen?
return nil, pkg, pkgs
}
if sel == nil {
if obj := pkg.Scope.Lookup(id.Name); obj != nil {
return obj, pkg, pkgs
}
fn := filepath.Join(runtime.GOROOT(), "src", "pkg", "builtin")
if pkgBuiltin, _, err := parsePkg(fset, fn, parser.ParseComments); err == nil {
if obj := pkgBuiltin.Scope.Lookup(id.Name); obj != nil {
return obj, pkgBuiltin, pkgs
}
}
}
if sel == nil {
return nil, pkg, pkgs
}
switch x := sel.X.(type) {
case *ast.Ident:
if x.Obj != nil {
// todo: resolve type
} else {
if v := pkg.Scope.Lookup(id.Name); v != nil {
// todo: found a type?
} else {
// it's most likely a package
// todo: handle .dot imports
for _, ispec := range af.Imports {
importPath := unquote(ispec.Path.Value)
pkgAlias := ""
if ispec.Name == nil {
_, pkgAlias = path.Split(importPath)
} else {
pkgAlias = ispec.Name.Name
}
if pkgAlias == x.Name {
if id == x {
// where do we go as the first place of a package?
pkg, pkgs, _ = findPkg(fset, importPath, srcRootDirs, parser.ParseComments|parser.PackageClauseOnly)
if pkg != nil {
// we'll just match the behaviour of package browsing
// we will visit some file within the package
// but which file, or where is undefined
var f *ast.File
ok := false
if len(pkg.Files) > 0 {
basedir := ""
for fn, _ := range pkg.Files {
basedir = filepath.Dir(fn)
break
}
baseFn := func(fn string) string {
return filepath.Join(basedir, fn)
}
if f, ok = pkg.Files[baseFn("doc.go")]; !ok {
if f, ok = pkg.Files[baseFn("main.go")]; !ok {
if f, ok = pkg.Files[baseFn(pkgAlias+".go")]; !ok {
// try to keep things consistent
filenames := sort.StringSlice{}
for filename, _ := range pkg.Files {
filenames = append(filenames, filename)
}
sort.Sort(filenames)
f = pkg.Files[filenames[0]]
}
}
}
}
if f != nil && f.Name != nil {
doc := f.Doc
if len(f.Comments) > 0 && f.Doc == nil {
doc = f.Comments[len(f.Comments)-1]
}
o := &ast.Object{
Kind: ast.Pkg,
Name: f.Name.Name,
Decl: &ast.TypeSpec{
Name: f.Name,
Doc: doc,
Type: f.Name,
},
}
return o, pkg, pkgs
}
}
// in-case we don't find a pkg decl
return nil, pkg, pkgs
}
if pkg, pkgs, _ = findPkg(fset, importPath, srcRootDirs, parser.ParseComments); pkg != nil {
obj := pkg.Scope.Lookup(id.Name)
return obj, pkg, pkgs
}
}
}
}
}
}
return nil, pkg, pkgs
}