This repository has been archived by the owner on Feb 17, 2021. It is now read-only.
forked from mdempsky/gocode
-
Notifications
You must be signed in to change notification settings - Fork 28
/
autocompletefile.go
401 lines (346 loc) · 9.14 KB
/
autocompletefile.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"bytes"
"go/ast"
"go/build"
"go/parser"
"go/token"
)
func parse_decl_list(fset *token.FileSet, data []byte) ([]ast.Decl, error) {
var buf bytes.Buffer
buf.WriteString("package p;")
buf.Write(data)
file, err := parser.ParseFile(fset, "", buf.Bytes(), 0)
if err != nil {
return file.Decls, err
}
return file.Decls, nil
}
//-------------------------------------------------------------------------
// auto_complete_file
//-------------------------------------------------------------------------
type auto_complete_file struct {
name string
package_name string
decls map[string]*decl
packages []package_import
filescope *scope
scope *scope
cursor int // for current file buffer only
fset *token.FileSet
context build.Context
}
func new_auto_complete_file(name string, context build.Context) *auto_complete_file {
p := new(auto_complete_file)
p.name = name
p.cursor = -1
p.fset = token.NewFileSet()
p.context = context
return p
}
func (f *auto_complete_file) offset(p token.Pos) int {
const fixlen = len("package p;")
return f.fset.Position(p).Offset - fixlen
}
// this one is used for current file buffer exclusively
func (f *auto_complete_file) process_data(data []byte) {
cur, filedata, block := rip_off_decl(data, f.cursor)
file, _ := parser.ParseFile(f.fset, "", filedata, 0)
f.package_name = package_name(file)
f.decls = make(map[string]*decl)
f.packages = collect_package_imports(f.name, file.Decls, f.context)
f.filescope = new_scope(nil)
f.scope = f.filescope
for _, d := range file.Decls {
anonymify_ast(d, 0, f.filescope)
}
// process all top-level declarations
for _, decl := range file.Decls {
append_to_top_decls(f.decls, decl, f.scope)
}
if block != nil {
// process local function as top-level declaration
decls, _ := parse_decl_list(f.fset, block)
for _, d := range decls {
anonymify_ast(d, 0, f.filescope)
}
for _, decl := range decls {
append_to_top_decls(f.decls, decl, f.scope)
}
// process function internals
f.cursor = cur
for _, decl := range decls {
f.process_decl_locals(decl)
}
}
}
func (f *auto_complete_file) process_decl_locals(decl ast.Decl) {
switch t := decl.(type) {
case *ast.FuncDecl:
if f.cursor_in(t.Body) {
s := f.scope
f.scope = new_scope(f.scope)
f.process_field_list(t.Recv, s)
f.process_field_list(t.Type.Params, s)
f.process_field_list(t.Type.Results, s)
f.process_block_stmt(t.Body)
}
default:
v := new(func_lit_visitor)
v.ctx = f
ast.Walk(v, decl)
}
}
func (f *auto_complete_file) process_decl(decl ast.Decl) {
if t, ok := decl.(*ast.GenDecl); ok && f.offset(t.TokPos) > f.cursor {
return
}
prevscope := f.scope
foreach_decl(decl, func(data *foreach_decl_struct) {
class := ast_decl_class(data.decl)
if class != decl_type {
f.scope, prevscope = advance_scope(f.scope)
}
for i, name := range data.names {
typ, v, vi := data.type_value_index(i)
d := new_decl_full(name.Name, class, 0, typ, v, vi, prevscope)
if d == nil {
return
}
f.scope.add_named_decl(d)
}
})
}
func (f *auto_complete_file) process_block_stmt(block *ast.BlockStmt) {
if block != nil && f.cursor_in(block) {
f.scope, _ = advance_scope(f.scope)
for _, stmt := range block.List {
f.process_stmt(stmt)
}
// hack to process all func literals
v := new(func_lit_visitor)
v.ctx = f
ast.Walk(v, block)
}
}
type func_lit_visitor struct {
ctx *auto_complete_file
}
func (v *func_lit_visitor) Visit(node ast.Node) ast.Visitor {
if t, ok := node.(*ast.FuncLit); ok && v.ctx.cursor_in(t.Body) {
s := v.ctx.scope
v.ctx.scope = new_scope(v.ctx.scope)
v.ctx.process_field_list(t.Type.Params, s)
v.ctx.process_field_list(t.Type.Results, s)
v.ctx.process_block_stmt(t.Body)
return nil
}
return v
}
func (f *auto_complete_file) process_stmt(stmt ast.Stmt) {
switch t := stmt.(type) {
case *ast.DeclStmt:
f.process_decl(t.Decl)
case *ast.AssignStmt:
f.process_assign_stmt(t)
case *ast.IfStmt:
if f.cursor_in_if_head(t) {
f.process_stmt(t.Init)
} else if f.cursor_in_if_stmt(t) {
f.scope, _ = advance_scope(f.scope)
f.process_stmt(t.Init)
f.process_block_stmt(t.Body)
f.process_stmt(t.Else)
}
case *ast.BlockStmt:
f.process_block_stmt(t)
case *ast.RangeStmt:
f.process_range_stmt(t)
case *ast.ForStmt:
if f.cursor_in_for_head(t) {
f.process_stmt(t.Init)
} else if f.cursor_in(t.Body) {
f.scope, _ = advance_scope(f.scope)
f.process_stmt(t.Init)
f.process_block_stmt(t.Body)
}
case *ast.SwitchStmt:
f.process_switch_stmt(t)
case *ast.TypeSwitchStmt:
f.process_type_switch_stmt(t)
case *ast.SelectStmt:
f.process_select_stmt(t)
case *ast.LabeledStmt:
f.process_stmt(t.Stmt)
}
}
func (f *auto_complete_file) process_select_stmt(a *ast.SelectStmt) {
if !f.cursor_in(a.Body) {
return
}
var prevscope *scope
f.scope, prevscope = advance_scope(f.scope)
var last_cursor_after *ast.CommClause
for _, s := range a.Body.List {
if cc := s.(*ast.CommClause); f.cursor > f.offset(cc.Colon) {
last_cursor_after = cc
}
}
if last_cursor_after != nil {
if last_cursor_after.Comm != nil {
//if lastCursorAfter.Lhs != nil && lastCursorAfter.Tok == token.DEFINE {
if astmt, ok := last_cursor_after.Comm.(*ast.AssignStmt); ok && astmt.Tok == token.DEFINE {
vname := astmt.Lhs[0].(*ast.Ident).Name
v := new_decl_var(vname, nil, astmt.Rhs[0], -1, prevscope)
f.scope.add_named_decl(v)
}
}
for _, s := range last_cursor_after.Body {
f.process_stmt(s)
}
}
}
func (f *auto_complete_file) process_type_switch_stmt(a *ast.TypeSwitchStmt) {
if !f.cursor_in(a.Body) {
return
}
var prevscope *scope
f.scope, prevscope = advance_scope(f.scope)
f.process_stmt(a.Init)
// type var
var tv *decl
if a, ok := a.Assign.(*ast.AssignStmt); ok {
lhs := a.Lhs
rhs := a.Rhs
if lhs != nil && len(lhs) == 1 {
tvname := lhs[0].(*ast.Ident).Name
tv = new_decl_var(tvname, nil, rhs[0], -1, prevscope)
}
}
var last_cursor_after *ast.CaseClause
for _, s := range a.Body.List {
if cc := s.(*ast.CaseClause); f.cursor > f.offset(cc.Colon) {
last_cursor_after = cc
}
}
if last_cursor_after != nil {
if tv != nil {
if last_cursor_after.List != nil && len(last_cursor_after.List) == 1 {
tv.typ = last_cursor_after.List[0]
tv.value = nil
}
f.scope.add_named_decl(tv)
}
for _, s := range last_cursor_after.Body {
f.process_stmt(s)
}
}
}
func (f *auto_complete_file) process_switch_stmt(a *ast.SwitchStmt) {
if !f.cursor_in(a.Body) {
return
}
f.scope, _ = advance_scope(f.scope)
f.process_stmt(a.Init)
var last_cursor_after *ast.CaseClause
for _, s := range a.Body.List {
if cc := s.(*ast.CaseClause); f.cursor > f.offset(cc.Colon) {
last_cursor_after = cc
}
}
if last_cursor_after != nil {
for _, s := range last_cursor_after.Body {
f.process_stmt(s)
}
}
}
func (f *auto_complete_file) process_range_stmt(a *ast.RangeStmt) {
if !f.cursor_in(a.Body) {
return
}
var prevscope *scope
f.scope, prevscope = advance_scope(f.scope)
if a.Tok == token.DEFINE {
if t, ok := a.Key.(*ast.Ident); ok {
d := new_decl_var(t.Name, nil, a.X, 0, prevscope)
if d != nil {
d.flags |= decl_rangevar
f.scope.add_named_decl(d)
}
}
if a.Value != nil {
if t, ok := a.Value.(*ast.Ident); ok {
d := new_decl_var(t.Name, nil, a.X, 1, prevscope)
if d != nil {
d.flags |= decl_rangevar
f.scope.add_named_decl(d)
}
}
}
}
f.process_block_stmt(a.Body)
}
func (f *auto_complete_file) process_assign_stmt(a *ast.AssignStmt) {
if a.Tok != token.DEFINE || f.offset(a.TokPos) > f.cursor {
return
}
names := make([]*ast.Ident, len(a.Lhs))
for i, name := range a.Lhs {
id, ok := name.(*ast.Ident)
if !ok {
// something is wrong, just ignore the whole stmt
return
}
names[i] = id
}
var prevscope *scope
f.scope, prevscope = advance_scope(f.scope)
pack := decl_pack{names, nil, a.Rhs}
for i, name := range pack.names {
typ, v, vi := pack.type_value_index(i)
d := new_decl_var(name.Name, typ, v, vi, prevscope)
if d == nil {
continue
}
f.scope.add_named_decl(d)
}
}
func (f *auto_complete_file) process_field_list(field_list *ast.FieldList, s *scope) {
if field_list != nil {
decls := ast_field_list_to_decls(field_list, decl_var, 0, s, false)
for _, d := range decls {
f.scope.add_named_decl(d)
}
}
}
func (f *auto_complete_file) cursor_in_if_head(s *ast.IfStmt) bool {
if f.cursor > f.offset(s.If) && f.cursor <= f.offset(s.Body.Lbrace) {
return true
}
return false
}
func (f *auto_complete_file) cursor_in_if_stmt(s *ast.IfStmt) bool {
if f.cursor > f.offset(s.If) {
// magic -10 comes from auto_complete_file.offset method, see
// len() expr in there
if f.offset(s.End()) == -10 || f.cursor < f.offset(s.End()) {
return true
}
}
return false
}
func (f *auto_complete_file) cursor_in_for_head(s *ast.ForStmt) bool {
if f.cursor > f.offset(s.For) && f.cursor <= f.offset(s.Body.Lbrace) {
return true
}
return false
}
func (f *auto_complete_file) cursor_in(block *ast.BlockStmt) bool {
if f.cursor == -1 || block == nil {
return false
}
if f.cursor > f.offset(block.Lbrace) && f.cursor <= f.offset(block.Rbrace) {
return true
}
return false
}