forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cursorcontext.go
216 lines (192 loc) · 4.86 KB
/
cursorcontext.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
package main
import (
"go/ast"
"go/parser"
"unicode"
"unicode/utf8"
)
type cursor_context struct {
decl *decl
partial string
}
type bytes_iterator struct {
data []byte
cursor int
}
// return the character under the cursor
func (this *bytes_iterator) char() byte {
return this.data[this.cursor]
}
// return the rune under the cursor
func (this *bytes_iterator) rune() rune {
r, _ := utf8.DecodeRune(this.data[this.cursor:])
return r
}
// move cursor backwards to the next valid utf8 rune start, or 0
func (this *bytes_iterator) move_backwards() {
for this.cursor != 0 {
this.cursor--
if utf8.RuneStart(this.char()) {
return
}
}
}
var g_unicode_ident_set = []*unicode.RangeTable{
unicode.Letter,
unicode.Digit,
{R16: []unicode.Range16{{'_', '_', 1}}},
}
// move cursor backwards, stop at the first rune that is not from
// 'g_unicode_ident_set', or 0
func (this *bytes_iterator) skip_ident() {
for this.cursor != 0 {
r := this.rune()
// stop if 'r' is not [a-zA-Z0-9_] (unicode correct though)
if !unicode.IsOneOf(g_unicode_ident_set, r) {
return
}
this.move_backwards()
}
}
var g_bracket_pairs = map[byte]byte{
')': '(',
']': '[',
}
// when the cursor is at the ')' or ']', move the cursor to an opposite bracket
// pair, this functions takes inner bracker pairs into account
func (this *bytes_iterator) skip_to_bracket_pair() {
right := this.char()
left := g_bracket_pairs[right]
balance := 1
for balance != 0 {
this.cursor--
if this.cursor == 0 {
return
}
switch this.char() {
case right:
balance++
case left:
balance--
}
}
}
// starting from the end of the 'file', move backwards and return a slice of a
// valid Go expression
func (this *bytes_iterator) extract_go_expr() []byte {
const (
last_none = iota
last_dot
last_paren
last_ident
)
last := last_none
orig := this.cursor
this.move_backwards()
loop:
for {
if this.cursor == 0 {
return this.data[:orig]
}
r := this.rune()
switch r {
case '.':
this.move_backwards()
last = last_dot
case ')', ']':
if last == last_ident {
break loop
}
this.skip_to_bracket_pair()
this.move_backwards()
last = last_paren
default:
if unicode.IsOneOf(g_unicode_ident_set, r) {
this.skip_ident()
last = last_ident
} else {
break loop
}
}
}
return this.data[this.cursor+1 : orig]
}
// this function is called when the cursor is at the '.' and you need to get the
// declaration before that dot
func (c *auto_complete_context) deduce_cursor_decl(iter *bytes_iterator) *decl {
e := string(iter.extract_go_expr())
expr, err := parser.ParseExpr(e)
if err != nil {
return nil
}
return expr_to_decl(expr, c.current.scope)
}
// deduce cursor context, it includes the declaration under the cursor and partial identifier
// (usually a part of the name of the child declaration)
func (c *auto_complete_context) deduce_cursor_context(file []byte, cursor int) (cursor_context, bool) {
if cursor <= 0 {
return cursor_context{nil, ""}, true
}
orig := cursor
iter := bytes_iterator{file, cursor}
// figure out what is just before the cursor
iter.move_backwards()
if iter.char() == '.' {
// we're '<whatever>.'
// figure out decl, Parital is ""
decl := c.deduce_cursor_decl(&iter)
return cursor_context{decl, ""}, decl != nil
}
r := iter.rune()
if unicode.IsOneOf(g_unicode_ident_set, r) {
// we're '<whatever>.<ident>'
// parse <ident> as Partial and figure out decl
iter.skip_ident()
partial := string(iter.data[iter.cursor+1 : orig])
if iter.char() == '.' {
decl := c.deduce_cursor_decl(&iter)
return cursor_context{decl, partial}, decl != nil
} else {
return cursor_context{nil, partial}, true
}
}
return cursor_context{nil, ""}, true
}
// deduce the type of the expression under the cursor, a bit of copy & paste from the method
// above, returns true if deduction was successful (even if the result of it is nil)
func (c *auto_complete_context) deduce_cursor_type_pkg(file []byte, cursor int) (ast.Expr, string, bool) {
deduce := func(iter *bytes_iterator) (ast.Expr, string) {
e := string(iter.extract_go_expr())
expr, err := parser.ParseExpr(e)
if err != nil {
return nil, ""
}
t, scope, _ := infer_type(expr, c.current.scope, -1)
return t, lookup_pkg(get_type_path(t), scope)
}
if cursor <= 0 {
return nil, "", true
}
iter := bytes_iterator{file, cursor}
// figure out what is just before the cursor
iter.move_backwards()
if iter.char() == '.' {
// we're '<whatever>.'
// figure out decl, Parital is ""
decl, pkg := deduce(&iter)
return decl, pkg, decl != nil
}
r := iter.rune()
if unicode.IsOneOf(g_unicode_ident_set, r) {
// we're '<whatever>.<ident>'
// parse <ident> as Partial and figure out decl
iter.skip_ident()
if iter.char() == '.' {
decl, pkg := deduce(&iter)
return decl, pkg, decl != nil
} else {
return nil, "", true
}
}
return nil, "", true
}