forked from nsf/gocode
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ripper.go
176 lines (148 loc) · 3.45 KB
/
ripper.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
package gocode
import (
"go/scanner"
"go/token"
"sync"
)
// All the code in this file serves single purpose:
// It separates a function with the cursor inside and the rest of the code. I'm
// doing that, because sometimes parser is not able to recover itself from an
// error and the autocompletion results become less complete.
type tok_pos_pair struct {
tok token.Token
pos token.Pos
}
type tok_collection struct {
tokens []tok_pos_pair
fset *token.FileSet
}
func (this *tok_collection) next(s *scanner.Scanner) bool {
pos, tok, _ := s.Scan()
if tok == token.EOF {
return false
}
this.tokens = append(this.tokens, tok_pos_pair{tok, pos})
return true
}
func (this *tok_collection) find_decl_beg(pos int) int {
lowest := 0
lowpos := -1
lowi := -1
cur := 0
for i := pos; i >= 0; i-- {
t := this.tokens[i]
switch t.tok {
case token.RBRACE:
cur++
case token.LBRACE:
cur--
}
if cur < lowest {
lowest = cur
lowpos = this.fset.Position(t.pos).Offset
lowi = i
}
}
cur = lowest
for i := lowi - 1; i >= 0; i-- {
t := this.tokens[i]
switch t.tok {
case token.RBRACE:
cur++
case token.LBRACE:
cur--
}
if t.tok == token.SEMICOLON && cur == lowest {
lowpos = this.fset.Position(t.pos).Offset
break
}
}
return lowpos
}
func (this *tok_collection) find_decl_end(pos int) int {
highest := 0
highpos := -1
cur := 0
if this.tokens[pos].tok == token.LBRACE {
pos++
}
for i := pos; i < len(this.tokens); i++ {
t := this.tokens[i]
switch t.tok {
case token.RBRACE:
cur++
case token.LBRACE:
cur--
}
if cur > highest {
highest = cur
highpos = this.fset.Position(t.pos).Offset
}
}
return highpos
}
func (this *tok_collection) find_outermost_scope(cursor int) (int, int) {
pos := 0
for i, t := range this.tokens {
if cursor <= this.fset.Position(t.pos).Offset {
break
}
pos = i
}
return this.find_decl_beg(pos), this.find_decl_end(pos)
}
// return new cursor position, file without ripped part and the ripped part itself
// variants:
// new-cursor, file-without-ripped-part, ripped-part
// old-cursor, file, nil
func (this *tok_collection) rip_off_decl(file []byte, cursor int) (int, []byte, []byte) {
this.fset = token.NewFileSet()
var s scanner.Scanner
s.Init(this.fset.AddFile("", this.fset.Base(), len(file)), file, nil, scanner.ScanComments)
for this.next(&s) {
}
beg, end := this.find_outermost_scope(cursor)
if beg == -1 || end == -1 {
return cursor, file, nil
}
ripped := make([]byte, end+1-beg)
copy(ripped, file[beg:end+1])
newfile := make([]byte, len(file)-len(ripped))
copy(newfile, file[:beg])
copy(newfile[beg:], file[end+1:])
return cursor - beg, newfile, ripped
}
func rip_off_decl(file []byte, cursor int) (int, []byte, []byte) {
tc := tok_collection{tokens: get_token_slice()}
pos, newfile, ripped := tc.rip_off_decl(file, cursor)
put_token_slice(tc.tokens)
tc.tokens = nil
return pos, newfile, ripped
}
var tokenPool struct {
sync.Mutex
pool [][]tok_pos_pair
}
func init() {
for i := 0; i < 20; i++ {
put_token_slice(make([]tok_pos_pair, 64))
}
}
func get_token_slice() (p []tok_pos_pair) {
tokenPool.Lock()
if n := len(tokenPool.pool); n != 0 {
p = tokenPool.pool[n-1][:0]
tokenPool.pool = tokenPool.pool[:n-1]
} else {
p = make([]tok_pos_pair, 64)
}
tokenPool.Unlock()
return
}
func put_token_slice(p []tok_pos_pair) {
tokenPool.Lock()
if len(tokenPool.pool) <= 20 {
tokenPool.pool = append(tokenPool.pool, p)
}
tokenPool.Unlock()
}