forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompletion.go
More file actions
476 lines (379 loc) · 11 KB
/
Copy pathcompletion.go
File metadata and controls
476 lines (379 loc) · 11 KB
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package readline
import (
"bufio"
"fmt"
"strings"
)
// startMenuComplete generates a completion menu with completions
// generated from a given completer, without selecting a candidate.
func (rl *Instance) startMenuComplete(completer func()) {
rl.local = menuselect
rl.skipUndoAppend()
// Call the completer function to produce completions,
// and store it if it's going to be used by autocomplete.
completer()
rl.completer = completer
// Cancel completion mode if we don't have any candidates.
// The hint string will be provided in a separate step.
if rl.noCompletions() {
rl.resetCompletion()
rl.completer = nil
return
}
rl.currentGroup()
// When there is only candidate, automatically insert it
// and exit the completion mode, except in history completion.
if rl.hasUniqueCandidate() && len(rl.histHint) == 0 {
rl.undoSkipAppend = false
rl.insertCandidate()
rl.resetCompletion()
rl.resetHintText()
}
}
func (rl *Instance) normalCompletions() {
if rl.Completer == nil {
return
}
rl.tcGroups = make([]*comps, 0)
// Get the correct line to be completed, and the current cursor position
compLine, compPos := rl.getCompletionLine()
// Generate the completions, setup the prefix and group the results.
comps := rl.Completer(compLine, compPos)
rl.groupCompletions(comps)
rl.setCompletionPrefix(comps)
}
func (rl *Instance) historyCompletion(forward, filterLine bool) {
switch rl.local {
case menuselect, isearch:
// If we are currently completing the last
// history source, cancel history completion.
if rl.historySourcePos == len(rl.histories)-1 {
rl.histHint = []rune{}
rl.nextHistorySource()
rl.resetCompletion()
rl.completer = nil
rl.local = ""
rl.resetHintText()
return
}
// Else complete the next history source.
rl.nextHistorySource()
fallthrough
default:
// Notify if we don't have history sources at all.
if rl.currentHistory() == nil {
noHistory := fmt.Sprintf("%s%s%s %s", seqDim, seqFgRed, "No command history source", seqReset)
rl.histHint = []rune(noHistory)
return
}
// Generate the completions with specified behavior.
historyCompletion := func() {
rl.tcGroups = make([]*comps, 0)
// Either against the current line or not.
if filterLine {
rl.tcPrefix = string(rl.line)
}
comps := rl.completeHistory(forward)
comps = comps.DisplayList()
rl.groupCompletions(comps)
rl.setCompletionPrefix(comps)
}
// Else, generate the completions.
rl.startMenuComplete(historyCompletion)
}
}
func (rl *Instance) registerCompletion() {
rl.registersComplete = true
rl.tcGroups = make([]*comps, 0)
comps := rl.completeRegisters()
comps = comps.DisplayList("*")
rl.groupCompletions(comps)
rl.setCompletionPrefix(comps)
}
func (rl *Instance) groupCompletions(comps Completions) {
rl.hintCompletions(comps)
// Nothing else to do if no completions
if len(comps.values) == 0 {
return
}
comps.values.eachTag(func(tag string, values rawValues) {
// Separate the completions that have a description and
// those which don't, and devise if there are aliases.
vals, noDescVals, aliased := groupValues(values)
// Create a "first" group with the "first" grouped values
rl.newGroup(comps, tag, vals, aliased)
// If we have a remaining group of values without descriptions,
// we will print and use them in a separate, anonymous group.
if len(noDescVals) > 0 {
rl.newGroup(comps, "", noDescVals, false)
}
})
}
// groupValues separates values based on whether they have descriptions, or are aliases of each other.
func groupValues(values rawValues) (vals, noDescVals rawValues, aliased bool) {
var descriptions []string
for _, val := range values {
// Grid completions
if val.Description == "" {
noDescVals = append(noDescVals, val)
continue
}
// List/map completions.
if stringInSlice(val.Description, descriptions) {
aliased = true
}
descriptions = append(descriptions, val.Description)
vals = append(vals, val)
}
// if no candidates have a description, swap
if len(vals) == 0 {
vals = noDescVals
noDescVals = make(rawValues, 0)
}
return
}
func (rl *Instance) setCompletionPrefix(comps Completions) {
switch comps.PREFIX {
case "":
// When no prefix has been specified, use
// the current word up to the cursor position.
lineWords, _, _ := tokeniseSplitSpaces(rl.line, rl.pos)
if len(lineWords) > 0 {
last := lineWords[len(lineWords)-1]
if last[len(last)-1] != ' ' {
rl.tcPrefix = lineWords[len(lineWords)-1]
}
}
default:
// When the prefix has been overridden, add it to all
// completions AND as a line prefix, for correct candidate insertion.
rl.tcPrefix = comps.PREFIX
}
}
func (rl *Instance) updateSelector(tabX, tabY int) {
grp := rl.currentGroup()
// If there is no current group, we
// leave any current completion mode.
if grp == nil || len(grp.values) == 0 {
return
}
done, next := grp.moveSelector(rl, tabX, tabY)
if !done {
return
}
var newGrp *comps
if next {
rl.cycleNextGroup()
newGrp = rl.currentGroup()
newGrp.firstCell()
} else {
rl.cyclePreviousGroup()
newGrp = rl.currentGroup()
newGrp.lastCell()
}
}
// printCompletions - Prints all completion groups and their items.
func (rl *Instance) printCompletions() {
rl.tcUsedY = 0
// The final completions string to print.
var completions string
// Safecheck
if rl.local != menuselect && rl.local != isearch && !rl.needsAutoComplete() {
return
}
// In any case, we write the completions strings, trimmed for redundant
// newline occurrences that have been put at the end of each group.
for _, group := range rl.tcGroups {
completions += group.writeComps(rl)
}
// Crop the completions so that it fits within our MaxTabCompleterRows
completions, rl.tcUsedY = rl.cropCompletions(completions)
if completions != "" {
print("\n")
rl.tcUsedY++
// Because some completion groups might have more suggestions
// than what their MaxLength allows them to, cycling sometimes occur,
// but does not fully clears itself: some descriptions are messed up with.
// We always clear the screen as a result, between writings.
print(seqClearScreenBelow)
}
// Then we print all of them.
print(completions)
}
// cropCompletions - When the user cycles through a completion list longer
// than the console MaxTabCompleterRows value, we crop the completions string
// so that "global" cycling (across all groups) is printed correctly.
func (rl *Instance) cropCompletions(comps string) (cropped string, usedY int) {
maxRows := rl.getCompletionMaxRows()
// Get the current absolute candidate position
absPos := rl.getAbsPos()
// Scan the completions for cutting them at newlines
scanner := bufio.NewScanner(strings.NewReader(comps))
// If absPos < MaxTabCompleterRows, cut below MaxTabCompleterRows and return
if absPos < maxRows {
return rl.cutCompletionsBelow(scanner, maxRows)
}
// If absolute > MaxTabCompleterRows, cut above and below and return
// -> This includes de facto when we tabCompletionReverse
if absPos >= maxRows {
return rl.cutCompletionsAboveBelow(scanner, maxRows, absPos)
}
return
}
// this is called once and only if the local keymap has not
// matched a given input key: that means no completion menu
// helpers were used, so we need to update our completion
// menu before actually editing/moving around the line.
func (rl *Instance) updateCompletion() {
switch rl.local {
case isearch:
rl.resetVirtualComp(true)
default:
rl.resetVirtualComp(false)
}
rl.resetCompletion()
}
func (rl *Instance) cutCompletionsBelow(scanner *bufio.Scanner, maxRows int) (string, int) {
var count int
var cropped string
for scanner.Scan() {
line := scanner.Text()
if count < maxRows {
cropped += line + "\n"
count++
} else {
break
}
}
cropped, _ = rl.excessCompletionsHint(cropped, maxRows, count-1)
return cropped, count
}
func (rl *Instance) cutCompletionsAboveBelow(scanner *bufio.Scanner, maxRows, absPos int) (string, int) {
cutAbove := absPos - maxRows + 1
var cropped string
var count int
var noRemain bool
for scanner.Scan() {
line := scanner.Text()
if count <= cutAbove {
count++
continue
}
if count > cutAbove && count <= absPos {
cropped += line + "\n"
count++
} else {
break
}
}
cropped, noRemain = rl.excessCompletionsHint(cropped, maxRows, maxRows+cutAbove+1)
if noRemain {
count--
}
return cropped, count - cutAbove
}
func (rl *Instance) excessCompletionsHint(cropped string, maxRows, offset int) (string, bool) {
_, _, adjusted := rl.completionCount()
remain := adjusted - offset
if remain <= 0 || offset < maxRows {
return cropped, true
}
hint := fmt.Sprintf(seqDim+seqFgYellow+" %d more completion rows... (scroll down to show)"+seqReset+"\n", remain)
hinted := cropped + hint
return hinted, false
}
func (rl *Instance) resetCompletion() {
// When we have a history hint, that means we are
// currently completing history, potentially in
// autocomplete: don't exit the current menu.
if rl.local == menuselect && len(rl.histHint) == 0 {
rl.local = ""
}
rl.tcPrefix = ""
rl.tcUsedY = 0
// Don't persist registers completion.
if rl.registersComplete {
rl.completer = nil
rl.registersComplete = false
}
// Reset tab highlighting
if len(rl.tcGroups) > 0 {
for _, g := range rl.tcGroups {
g.isCurrent = false
}
rl.tcGroups[0].isCurrent = true
}
}
// Check if we have a single completion candidate.
func (rl *Instance) hasUniqueCandidate() bool {
switch len(rl.tcGroups) {
case 0:
return false
case 1:
cur := rl.currentGroup()
if cur == nil {
return false
}
if len(cur.values) == 1 {
return len(cur.values[0]) == 1
}
return len(cur.values) == 1
default:
var count int
GROUPS:
for _, group := range rl.tcGroups {
for _, row := range group.values {
count++
for range row {
count++
}
if count > 1 {
break GROUPS
}
}
}
return count == 1
}
}
func (rl *Instance) needsAutoComplete() bool {
needsComplete := rl.config.AutoComplete &&
len(rl.line) > 0 &&
rl.local != menuselect &&
rl.local != isearch
isCorrectMenu := rl.main != vicmd && rl.local != isearch
// We might be at the beginning of line,
// but currently proposing history completion.
completingHistory := len(rl.histHint) > 0
// We always refresh history, except when
// currently having a candidate selection.
if completingHistory && isCorrectMenu && len(rl.comp) == 0 {
return true
}
if needsComplete && isCorrectMenu {
return true
}
return false
}
func (rl *Instance) isAutoCompleting() bool {
if rl.config.AutoComplete &&
len(rl.line) > 0 {
return true
}
return false
}
// autoComplete generates the correct completions in autocomplete mode.
// We don't do it when we are currently in the completion keymap,
// since that means completions have already been computed.
func (rl *Instance) autoComplete() {
if !rl.needsAutoComplete() {
return
}
rl.resetCompletion()
// We either have a completer, or we use the normal
// if we are not currently completing the history.
if rl.completer != nil {
rl.completer()
} else if len(rl.histHint) == 0 {
rl.normalCompletions()
}
}