-
Notifications
You must be signed in to change notification settings - Fork 12
/
completion.go
260 lines (210 loc) · 7.56 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
package readline
import (
"fmt"
"github.com/reeflective/readline/internal/color"
"github.com/reeflective/readline/internal/completion"
"github.com/reeflective/readline/internal/history"
"github.com/reeflective/readline/internal/keymap"
)
func (rl *Shell) completionCommands() commands {
return map[string]func(){
"complete": rl.completeWord,
"possible-completions": rl.possibleCompletions,
"insert-completions": rl.insertCompletions,
"menu-complete": rl.menuComplete,
"menu-complete-backward": rl.menuCompleteBackward,
"delete-char-or-list": rl.deleteCharOrList,
"menu-complete-next-tag": rl.menuCompleteNextTag,
"menu-complete-prev-tag": rl.menuCompletePrevTag,
"accept-and-menu-complete": rl.acceptAndMenuComplete,
"vi-registers-complete": rl.viRegistersComplete,
"menu-incremental-search": rl.menuIncrementalSearch,
}
}
//
// Commands ---------------------------------------------------------------------------
//
// Attempt completion on the current word.
// Currently identitical to menu-complete.
func (rl *Shell) completeWord() {
rl.History.SkipSave()
// This completion function should attempt to insert the first
// valid completion found, without printing the actual list.
if !rl.completer.IsActive() {
rl.startMenuComplete(rl.commandCompletion)
if rl.Config.GetBool("menu-complete-display-prefix") {
return
}
}
rl.completer.Select(1, 0)
rl.completer.SkipDisplay()
}
// List possible completions for the current word.
func (rl *Shell) possibleCompletions() {
rl.History.SkipSave()
rl.startMenuComplete(rl.commandCompletion)
}
// Insert all completions for the current word into the line.
func (rl *Shell) insertCompletions() {
rl.History.Save()
// Generate all possible completions
if !rl.completer.IsActive() {
rl.startMenuComplete(rl.commandCompletion)
}
// Insert each match, cancel insertion with preserving
// the candidate just inserted in the line, for each.
for i := 0; i < rl.completer.Matches(); i++ {
rl.completer.Select(1, 0)
rl.completer.Cancel(false, false)
}
// Clear the completion menu.
rl.completer.Cancel(false, false)
rl.completer.ClearMenu(true)
}
// Like complete-word, except that menu completion is used.
func (rl *Shell) menuComplete() {
rl.History.SkipSave()
// No completions are being printed yet, so simply generate the completions
// as if we just request them without immediately selecting a candidate.
if !rl.completer.IsActive() {
rl.startMenuComplete(rl.commandCompletion)
// Immediately select only if not asked to display first.
if rl.Config.GetBool("menu-complete-display-prefix") {
return
}
}
rl.completer.Select(1, 0)
}
// Deletes the character under the cursor if not at the
// beginning or end of the line (like delete-char).
// If at the end of the line, behaves identically to
// possible-completions.
func (rl *Shell) deleteCharOrList() {
switch {
case rl.cursor.Pos() < rl.line.Len():
rl.line.CutRune(rl.cursor.Pos())
default:
rl.possibleCompletions()
}
}
// Identical to menu-complete, but moves backward through the
// list of possible completions, as if menu-complete had been
// given a negative argument.
func (rl *Shell) menuCompleteBackward() {
rl.History.SkipSave()
// We don't do anything when not already completing.
if !rl.completer.IsActive() {
rl.startMenuComplete(rl.commandCompletion)
}
rl.completer.Select(-1, 0)
}
// In a menu completion, if there are several tags
// of completions, go to the first result of the next tag.
func (rl *Shell) menuCompleteNextTag() {
rl.History.SkipSave()
if !rl.completer.IsActive() {
return
}
rl.completer.SelectTag(true)
}
// In a menu completion, if there are several tags of
// completions, go to the first result of the previous tag.
func (rl *Shell) menuCompletePrevTag() {
rl.History.SkipSave()
if !rl.completer.IsActive() {
return
}
rl.completer.SelectTag(false)
}
// In a menu completion, insert the current completion
// into the buffer, and advance to the next possible completion.
func (rl *Shell) acceptAndMenuComplete() {
rl.History.SkipSave()
// We don't do anything when not already completing.
if !rl.completer.IsActive() {
return
}
// Also return if no candidate
if !rl.completer.IsInserting() {
return
}
// First insert the current candidate.
rl.completer.Cancel(false, false)
// And cycle to the next one.
rl.completer.Select(1, 0)
}
// Open a completion menu (similar to menu-complete) with all currently populated Vim registers.
func (rl *Shell) viRegistersComplete() {
rl.History.SkipSave()
rl.startMenuComplete(rl.Buffers.Complete)
}
// In a menu completion (whether a candidate is selected or not), start incremental-search
// (fuzzy search) on the results. Search backward incrementally for a specified string.
// The search is case-insensitive if the search string does not have uppercase letters
// and no numeric argument was given. The string may begin with ‘^’ to anchor the search
// to the beginning of the line. A restricted set of editing functions is available in the
// mini-buffer. Keys are looked up in the special isearch keymap, On each change in the
// mini-buffer, any currently selected candidate is dropped from the line and the menu.
// An interrupt signal, as defined by the stty setting, will stop the search and go back to the original line.
func (rl *Shell) menuIncrementalSearch() {
rl.History.SkipSave()
// Always regenerate the list of completions.
rl.completer.GenerateWith(rl.commandCompletion)
rl.completer.IsearchStart("completions", false, false)
}
//
// Utilities --------------------------------------------------------------------------
//
// startMenuComplete generates a completion menu with completions
// generated from a given completer, without selecting a candidate.
func (rl *Shell) startMenuComplete(completer completion.Completer) {
rl.History.SkipSave()
rl.Keymap.SetLocal(keymap.MenuSelect)
rl.completer.GenerateWith(completer)
}
// commandCompletion generates the completions for commands/args/flags.
func (rl *Shell) commandCompletion() completion.Values {
if rl.Completer == nil {
return completion.Values{}
}
line, cursor := rl.completer.Line()
comps := rl.Completer(*line, cursor.Pos())
return comps.convert()
}
// historyCompletion manages the various completion/isearch modes related
// to history control. It can start the history completions, stop them, cycle
// through sources if more than one, and adjust the completion/isearch behavior.
func (rl *Shell) historyCompletion(forward, filterLine, substring bool) {
switch {
case rl.Keymap.Local() == keymap.MenuSelect || rl.Keymap.Local() == keymap.Isearch || rl.completer.AutoCompleting():
// If we are currently completing the last
// history source, cancel history completion.
if rl.History.OnLastSource() {
rl.History.Cycle(true)
rl.completer.ResetForce()
rl.Hint.Reset()
return
}
// Else complete the next history source.
rl.History.Cycle(true)
fallthrough
default:
// Notify if we don't have history sources at all.
if rl.History.Current() == nil {
rl.Hint.SetTemporary(fmt.Sprintf("%s%s%s %s", color.Dim, color.FgRed, "No command history source", color.Reset))
return
}
// Generate the completions with specified behavior.
completer := func() completion.Values {
maxLines := rl.Display.AvailableHelperLines()
return history.Complete(rl.History, forward, filterLine, maxLines, rl.completer.IsearchRegex)
}
if substring {
rl.completer.GenerateWith(completer)
rl.completer.IsearchStart(rl.History.Name(), true, true)
} else {
rl.startMenuComplete(completer)
rl.completer.AutocompleteForce()
}
}
}