-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion.go
380 lines (307 loc) · 6.62 KB
/
completion.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
package main
import (
"regexp"
"strings"
)
var trimRight = `)]"':`
type Candidate struct {
*Identifier
Selected bool
Parent string
}
type Identifier struct {
X int
Y int
Value string
}
func (identifier *Identifier) Length() int {
return len([]rune(identifier.Value))
}
func getIdentifierToComplete(
regexpCursor string,
lines []string,
x int,
y int,
) (*Identifier, error) {
textBeforeCursor := string([]rune(lines[y])[:x])
matcher, err := regexp.Compile(
`^.*?(` + regexpCursor + `)$`,
)
if err != nil {
return nil, err
}
matches := matcher.FindStringSubmatch(textBeforeCursor)
if len(matches) < 2 {
return nil, nil
}
return &Identifier{
X: x - len(matches[1]),
Y: y,
Value: matches[1],
}, nil
}
func getCompletionCandidates(
regexpCandidate string,
lines []string,
identifier *Identifier,
) ([]*Candidate, error) {
query := regexpCandidate
if identifier != nil {
query = regexp.QuoteMeta(identifier.Value) + regexpCandidate
}
matcher, err := regexp.Compile(query)
if err != nil {
return nil, err
}
var candidates []*Candidate
type unit struct {
value string
start int
}
for lineNumber, line := range lines {
matches := matcher.FindAllStringSubmatchIndex(line, -1)
for _, match := range matches {
var (
start, end = match[0], match[1]
text = line[start:end]
)
units := []unit{
{text, start},
}
trimmed := strings.TrimRight(text, trimRight)
if len(trimmed) > 0 && trimmed != text {
units = append(
units,
unit{trimmed, start},
)
}
for number, unit := range units {
if identifier != nil && !strings.HasPrefix(unit.value, identifier.Value) {
continue
}
if identifier != nil && unit.value == identifier.Value {
continue
}
var (
x = len([]rune(line[:unit.start]))
y = lineNumber
)
if identifier != nil && x == identifier.X && y == identifier.Y {
continue
}
parent := ""
if number > 0 {
parent = text
}
candidates = append(candidates, &Candidate{
Identifier: &Identifier{
X: x,
Y: y,
Value: unit.value,
},
Parent: parent,
})
}
}
}
return candidates, nil
}
func getSelectedCandidate(candidates []*Candidate) *Candidate {
for _, candidate := range candidates {
if candidate.Selected {
return candidate
}
}
return nil
}
func selectDefaultCandidate(
candidates []*Candidate,
x int,
y int,
) {
if len(candidates) == 0 {
return
}
var closest *Candidate
for _, candidate := range candidates {
// never select nested candidates as default
// there should be a parent
if candidate.Parent != "" {
continue
}
if closest == nil {
closest = candidate
continue
}
if candidate.Y > y {
continue
}
if candidate.Y == y {
if candidate.X > x {
continue
}
}
if y-candidate.Y > y-closest.Y {
continue
}
if candidate.Y == closest.Y {
if candidate.X < closest.X {
continue
}
}
closest = candidate
}
if selected := getSelectedCandidate(candidates); selected != nil {
selected.Selected = false
}
closest.Selected = true
}
func debugCandidate(candidate *Candidate) {
selected := ""
if candidate.Selected {
selected = " (selected)"
}
debug.Printf(
"y: %-3v x: %-3v length: %-3v value: %s%s",
candidate.Y,
candidate.X,
candidate.Length(),
candidate.Value,
selected,
)
}
// the following code has many if-conditions that can be omitted, but please do
// not refactor it, it has been made consciously in order to reduce cognitive
// load
func selectNextCandidate(
candidates []*Candidate,
dirX int,
dirY int,
) {
debug.Printf("selecting next candidate")
selected := getSelectedCandidate(candidates)
if selected == nil {
return
}
next := []*Candidate{}
for _, candidate := range candidates {
if isNextCandidate(selected, dirX, dirY, candidate) {
next = append(next, candidate)
}
}
if len(next) == 0 {
return
}
closest := next[0]
for _, candidate := range next {
distanceX := abs(selected.X-candidate.X) - abs(selected.X-closest.X)
switch {
case dirY != 0:
distanceY := abs(selected.Y-candidate.Y) - abs(selected.Y-closest.Y)
if distanceY < 0 {
closest = candidate
continue
}
if distanceY == 0 && distanceX < 0 {
closest = candidate
continue
}
case dirX != 0:
if distanceX < 0 {
closest = candidate
continue
}
if dirX == 1 {
// looking for size greater than selected but the difference
// should be as small as possible
if candidate.Length() > selected.Length() && candidate.Length() < closest.Length() {
closest = candidate
}
} else {
// looking for size less than selected but the difference
// should be as small as possible (near to selected as possible)
if candidate.Length() < selected.Length() && candidate.Length() > closest.Length() {
closest = candidate
}
}
}
}
closest.Selected = true
selected.Selected = false
}
func isNextCandidate(selected *Candidate, dirX, dirY int, candidate *Candidate) bool {
debugCandidate(candidate)
signX := sign(dirX)
signY := sign(dirY)
offsetX := sign(candidate.X - selected.X)
offsetY := sign(candidate.Y - selected.Y)
if dirX != 0 {
// case: move horizontally
if offsetY != 0 {
// not interested in vertical moves in horizontal mode
return false
}
debug.Printf("signX: %d offsetX: %d", signX, offsetX)
if signX == offsetX {
return true
}
// wrong direction
if offsetX == 0 {
// that case is possible when we have two items on the same
// X coordinate but with different length of identifier
if signX > 0 {
// move right
if candidate.Length() > selected.Length() {
// not interested in less size, candidate length should be
// greater than slected
return true
}
} else {
// move left
if candidate.Length() < selected.Length() {
return true
}
}
}
return false
}
// case: move vertically
if signY == offsetY {
return true
}
return false
}
func abs(x int) int {
if x < 0 {
x = -x
}
return x
}
func sign(value int) int {
switch {
case value > 0:
return 1
case value < 0:
return -1
default:
return 0
}
}
func getUniqueCandidates(candidates []*Candidate) []*Candidate {
uniques := []*Candidate{}
mainLoop:
for i := len(candidates) - 1; i >= 0; i-- {
candidate := candidates[i]
for _, unique := range uniques {
if unique.Value == candidate.Value && unique.Parent == candidate.Parent {
continue mainLoop
}
}
uniques = append(uniques, candidate)
}
reversed := []*Candidate{}
for i := len(uniques) - 1; i >= 0; i-- {
reversed = append(reversed, uniques[i])
}
return reversed
}