forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
completion-insert.go
327 lines (276 loc) · 8.67 KB
/
completion-insert.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
package readline
import (
"strings"
)
// Insert the current completion candidate into the input line.
// This candidate might either be the currently selected one (white frame),
// or the only candidate available, if the total number of candidates is 1.
func (rl *Instance) insertCandidate() {
cur := rl.currentGroup()
if cur == nil {
return
}
completion := cur.selected().Value
prefix := len(rl.tcPrefix)
// Special case for the only special escape, which
// if not handled, will make us insert the first
// character of our actual rl.tcPrefix in the candidate.
if strings.HasPrefix(string(rl.tcPrefix), "%") {
prefix++
}
// Ensure no indexing error happens with prefix
if len(completion) >= prefix {
rl.insert([]rune(completion[prefix:]))
rl.compSuffix = cur.noSpace
rl.compSuffix.pos = rl.pos - 1
}
}
func (rl *Instance) removeSuffixInserted() {
// We need a line, and no virtual candidate.
if len(rl.line) == 0 || rl.pos == 0 || len(rl.comp) != 0 {
return
}
// If our suffix matcher was registered at a different
// place in our line, then it's an orphan.
if rl.compSuffix.pos != rl.pos-1 {
rl.compSuffix = suffixMatcher{}
return
}
suffix := string(rl.line[rl.pos-1])
// Special case when completing paths: if the comp is ended
// by a slash, only remove this slash if the inserted key is
// one of the suffix matchers, otherwise keep it.
if suffix == "/" && rl.keys != " " {
if keyIsNotMatcher(rl.keys, rl.compSuffix.string) {
return
}
}
if rl.compSuffix.Matches(suffix) {
rl.deletex()
// Only remove the matcher if the key we inserted
// is not the same as the one we removed. This is
// so that if we just inserted a slash after an
// inserted directory, the net result is actually
// nil, so that a space then entered should still
// trigger the same behavior.
if rl.keys != suffix {
rl.compSuffix = suffixMatcher{}
}
}
}
// insertCandidateVirtual - When a completion candidate is selected, we insert it virtually in the input line:
// this will not trigger further firltering against the other candidates. Each time this function
// is called, any previous candidate is dropped, after being used for moving the cursor around.
func (rl *Instance) insertCandidateVirtual(candidate []rune) {
for {
// I don't really understand why `0` is creaping in at the end of the
// array but it only happens with unicode characters.
if len(candidate) > 1 && candidate[len(candidate)-1] == 0 {
candidate = candidate[:len(candidate)-1]
continue
}
break
}
// Place the cursor at the beginning of the previous virtual candidate
if rl.pos > 0 {
rl.pos -= len(rl.comp)
}
// We delete the previous virtual completion, just
// like we would delete a word in vim editing mode.
if len(rl.comp) == 1 {
rl.deleteVirtual()
} else if len(rl.comp) > 0 {
rl.viDeleteByAdjustVirtual(len(rl.comp))
}
// We then keep a reference to the new candidate
// We should not have a remaining virtual completion
// line, so it is now identical to the real line.
rl.comp = candidate
rl.compLine = rl.line
// Insert the new candidate in the virtual line.
switch {
case len(rl.compLine) == 0:
rl.compLine = candidate
case rl.pos == 0:
rl.compLine = append(candidate, rl.compLine...)
case rl.pos < len(rl.compLine):
r := append(candidate, rl.compLine[rl.pos:]...)
rl.compLine = append(rl.compLine[:rl.pos], r...)
default:
rl.compLine = append(rl.compLine, candidate...)
}
rl.pos += len(candidate)
}
// updateVirtualComp - Either insert the current completion
// candidate virtually, or on the real line.
func (rl *Instance) updateVirtualComp() {
cur := rl.currentGroup()
if cur == nil {
return
}
completion := cur.selected().Value
prefix := len(rl.tcPrefix)
if rl.hasUniqueCandidate() {
rl.insertCandidate()
rl.undoSkipAppend = true
rl.resetCompletion()
} else {
// Special case for the only special escape, which
// if not handled, will make us insert the first
// character of our actual rl.tcPrefix in the candidate.
// TODO: This should be changed.
if strings.HasPrefix(string(rl.tcPrefix), "%") {
prefix++
}
if len(completion) >= prefix {
rl.insertCandidateVirtual([]rune(completion[prefix:]))
}
}
}
// resetVirtualComp - This function is called before most of our readline key handlers,
// and makes sure that the current completion (virtually inserted) is either inserted
// or dropped, and that all related parameters are reinitialized.
func (rl *Instance) resetVirtualComp(drop bool) {
if len(rl.comp) == 0 {
return
}
// Get the current candidate and its group.
cur := rl.currentGroup()
if cur == nil {
return
}
completion := cur.selected().Value
// Avoid problems with empty completions
if completion == "" {
rl.clearVirtualComp()
return
}
// We only insert the net difference between prefix and completion.
prefix := len(rl.tcPrefix)
// Special case for the only special escape, which
// if not handled, will make us insert the first
// character of our actual rl.tcPrefix in the candidate.
if strings.HasPrefix(string(rl.tcPrefix), "%") {
prefix++
}
if drop {
rl.pos -= len([]rune(completion[prefix:]))
rl.compLine = rl.line
rl.clearVirtualComp()
return
}
// Trim any suffix when found, except for a few cases.
completion = rl.removeSuffixCandidate(cur, prefix)
rl.insertCandidateVirtual([]rune(completion[prefix:]))
rl.clearVirtualComp()
}
func (rl *Instance) removeSuffixCandidate(cur *comps, prefix int) (comp string) {
comp = cur.selected().Value
// When the completion has a size of 1, don't remove anything:
// stacked flags, for example, will never be inserted otherwise.
if len(comp[prefix:]) == 1 {
return
}
// If we are to even consider removing a suffix, we keep the suffix
// matcher for later: whatever the decision we take here will be identical
// to the one we take while removing suffix in "non-virtual comp" mode.
rl.compSuffix = cur.noSpace
rl.compSuffix.pos = rl.pos - 1
// Add a space to suffix matcher when empty.
if cur.noSpace.string == "" && !rl.config.AutoComplete {
cur.noSpace.Add([]rune{' '}...)
}
// When the suffix matcher is a wildcard, that just means
// it's a noSpace directive: if the currently inserted key
// is a space, don't remove anything, but keep it for later.
if cur.noSpace.string == "*" && rl.keys == " " && comp[len(comp)-1] != ' ' {
return
}
// Special case when completing paths: if the comp is ended
// by a slash, only remove this slash if the inserted key is
// one of the suffix matchers and not a space, otherwise keep it.
if strings.HasSuffix(comp, "/") && rl.keys != " " {
if keyIsNotMatcher(rl.keys, cur.noSpace.string) {
return
}
}
// Else if the suffix matches a pattern, remove
if cur.noSpace.Matches(comp) {
comp = comp[:len(comp)-1]
}
return comp
}
// viDeleteByAdjustVirtual - Same as viDeleteByAdjust, but for our virtually completed input line.
func (rl *Instance) viDeleteByAdjustVirtual(adjust int) {
var newLine []rune
// Avoid doing anything if input line is empty.
if len(rl.compLine) == 0 {
return
}
switch {
case adjust == 0:
rl.undoSkipAppend = true
return
case rl.pos+adjust == len(rl.compLine)-1:
newLine = rl.compLine[:rl.pos]
case rl.pos+adjust == 0:
newLine = rl.compLine[rl.pos:]
case adjust < 0:
newLine = append(rl.compLine[:rl.pos+adjust], rl.compLine[rl.pos:]...)
default:
newLine = append(rl.compLine[:rl.pos], rl.compLine[rl.pos+adjust:]...)
}
// We have our new line completed
rl.compLine = newLine
if adjust < 0 {
rl.moveCursorByAdjust(adjust)
}
}
// viJumpEVirtual - Same as viJumpE, but for our virtually completed input line.
func (rl *Instance) viJumpEVirtual(tokeniser func([]rune, int) ([]string, int, int)) (adjust int) {
split, index, pos := tokeniser(rl.compLine, rl.pos)
if len(split) == 0 {
return
}
word := rTrimWhiteSpace(split[index])
switch {
case len(split) == 0:
return
case index == len(split)-1 && pos >= len(word)-1:
return
case pos >= len(word)-1:
word = rTrimWhiteSpace(split[index+1])
adjust = len(split[index]) - pos
adjust += len(word) - 1
default:
adjust = len(word) - pos - 1
}
return
}
func (rl *Instance) deleteVirtual() {
switch {
case len(rl.compLine) == 0:
return
case rl.pos == 0:
rl.compLine = rl.compLine[1:]
case rl.pos > len(rl.compLine):
case rl.pos == len(rl.compLine):
rl.compLine = rl.compLine[:rl.pos]
default:
rl.compLine = append(rl.compLine[:rl.pos], rl.compLine[rl.pos+1:]...)
}
}
// We are done with the current virtual completion candidate.
// Get ready for the next one.
func (rl *Instance) clearVirtualComp() {
rl.line = rl.compLine
rl.comp = []rune{}
}
func keyIsNotMatcher(key, matchers string) bool {
for _, r := range matchers {
if string(r) == key {
return false
}
}
return true
}