-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
prompt.ts
252 lines (221 loc) · 7.1 KB
/
prompt.ts
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
'use strict'
import { Neovim } from '@chemzqm/neovim'
import { Emitter, Event } from '../util/protocol'
import { getUnicodeClass } from '../util/string'
import listConfiguration from './configuration'
import { ListMode, ListOptions, Matcher } from './types'
export default class Prompt {
private cusorIndex = 0
private _input = ''
private _matcher: Matcher | ''
private _mode: ListMode = 'insert'
private interactive = false
private requestInput = false
private _onDidChangeInput = new Emitter<string>()
public readonly onDidChangeInput: Event<string> = this._onDidChangeInput.event
constructor(private nvim: Neovim) {
}
public get input(): string {
return this._input
}
public set input(str: string) {
if (this._input == str) return
this.cusorIndex = str.length
this._input = str
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public get mode(): ListMode {
return this._mode
}
public set mode(val: ListMode) {
if (val == this._mode) return
this._mode = val
this.drawPrompt()
}
public set matcher(val: Matcher) {
this._matcher = val
this.drawPrompt()
}
public start(opts?: ListOptions): void {
if (opts) {
this.interactive = opts.interactive
this.cusorIndex = opts.input.length
this._input = opts.input
this._mode = opts.mode
this._matcher = opts.interactive ? '' : opts.matcher
}
this.nvim.call('coc#prompt#start_prompt', ['list'], true)
this.drawPrompt()
}
public cancel(): void {
let { nvim } = this
nvim.call('coc#prompt#stop_prompt', ['list'], true)
}
public reset(): void {
this._input = ''
this.cusorIndex = 0
}
public drawPrompt(): void {
let indicator = listConfiguration.get<string>('indicator', '>')
let { cusorIndex, interactive, input, _matcher } = this
let cmds = ['echo ""']
if (this.mode == 'insert') {
if (interactive) {
cmds.push(`echohl MoreMsg | echon 'INTERACTIVE ' | echohl None`)
} else if (_matcher) {
cmds.push(`echohl MoreMsg | echon '${_matcher.toUpperCase()} ' | echohl None`)
}
cmds.push(`echohl Special | echon '${indicator} ' | echohl None`)
if (cusorIndex == input.length) {
cmds.push(`echon '${input.replace(/'/g, "''")}'`)
cmds.push(`echohl Cursor | echon ' ' | echohl None`)
} else {
let pre = input.slice(0, cusorIndex)
if (pre) cmds.push(`echon '${pre.replace(/'/g, "''")}'`)
cmds.push(`echohl Cursor | echon '${input[cusorIndex].replace(/'/, "''")}' | echohl None`)
let post = input.slice(cusorIndex + 1)
cmds.push(`echon '${post.replace(/'/g, "''")}'`)
}
} else {
cmds.push(`echohl MoreMsg | echo "" | echohl None`)
}
cmds.push('redraw')
let cmd = cmds.join('|')
this.nvim.command(cmd, true)
}
public moveLeft(): void {
if (this.cusorIndex == 0) return
this.cusorIndex = this.cusorIndex - 1
this.drawPrompt()
}
public moveRight(): void {
if (this.cusorIndex == this._input.length) return
this.cusorIndex = this.cusorIndex + 1
this.drawPrompt()
}
public moveLeftWord(): void {
// Reuses logic from removeWord(), except that we only update the
// cursorIndex and don't actually remove the word.
let { cusorIndex, input } = this
if (cusorIndex == 0) return
let pre = input.slice(0, cusorIndex)
let remain = getLastWordRemovedText(pre)
this.cusorIndex = cusorIndex - (pre.length - remain.length)
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public moveRightWord(): void {
let { cusorIndex, input } = this
if (cusorIndex == input.length) return
let post = input.slice(cusorIndex)
let nextWord = post.match(/[\w$]+ */).at(0) ?? post
this.cusorIndex = cusorIndex + nextWord.length
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public moveToEnd(): void {
if (this.cusorIndex == this._input.length) return
this.cusorIndex = this._input.length
this.drawPrompt()
}
public moveToStart(): void {
if (this.cusorIndex == 0) return
this.cusorIndex = 0
this.drawPrompt()
}
public onBackspace(): void {
let { cusorIndex, input } = this
if (cusorIndex == 0) return
let pre = input.slice(0, cusorIndex)
let post = input.slice(cusorIndex)
this.cusorIndex = cusorIndex - 1
this._input = `${pre.slice(0, pre.length - 1)}${post}`
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public removeNext(): void {
let { cusorIndex, input } = this
if (cusorIndex == input.length) return
let pre = input.slice(0, cusorIndex)
let post = input.slice(cusorIndex + 1)
this._input = `${pre}${post}`
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public removeWord(): void {
let { cusorIndex, input } = this
if (cusorIndex == 0) return
let pre = input.slice(0, cusorIndex)
let post = input.slice(cusorIndex)
let remain = getLastWordRemovedText(pre)
this.cusorIndex = cusorIndex - (pre.length - remain.length)
this._input = `${remain}${post}`
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public removeTail(): void {
let { cusorIndex, input } = this
if (cusorIndex == input.length) return
let pre = input.slice(0, cusorIndex)
this._input = pre
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public removeAhead(): void {
let { cusorIndex, input } = this
if (cusorIndex == 0) return
let post = input.slice(cusorIndex)
this.cusorIndex = 0
this._input = post
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
public async acceptCharacter(ch: string): Promise<void> {
if (this.requestInput) {
this.requestInput = false
if (/^[0-9a-z"%#*+/:\-.]$/.test(ch)) {
let text = await this.nvim.call('getreg', ch) as string
text = text.replace(/\n/g, ' ')
this.addText(text)
}
} else {
this.addText(ch)
}
}
public insertRegister(): void {
this.requestInput = true
}
public async paste(): Promise<void> {
let text = await this.nvim.eval('@*') as string
text = text.replace(/\n/g, '')
if (!text) return
this.addText(text)
}
public async eval(expression: string): Promise<void> {
let text = await this.nvim.call('eval', [expression]) as string
text = text.replace(/\n/g, '')
this.addText(text)
}
private addText(text: string): void {
let { cusorIndex, input } = this
this.cusorIndex = cusorIndex + text.length
let pre = input.slice(0, cusorIndex)
let post = input.slice(cusorIndex)
this._input = `${pre}${text}${post}`
this.drawPrompt()
this._onDidChangeInput.fire(this._input)
}
}
function getLastWordRemovedText(text: string): string {
let res = text
// Remove last whitespaces
res = res.trimEnd()
if (res === "") return res
// Remove last contiguous characters of the same unicode class.
const last = getUnicodeClass(res[res.length - 1])
while (res !== "" && getUnicodeClass(res[res.length - 1]) === last) {
res = res.slice(0, res.length - 1)
}
return res
}