forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcursorcontext.go
171 lines (153 loc) · 3.69 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
package main
import (
"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
}