-
-
Notifications
You must be signed in to change notification settings - Fork 959
/
Copy pathevents.ts
371 lines (338 loc) · 14.7 KB
/
events.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
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
'use strict'
import type { CompleteDoneItem, CompleteFinishKind } from './completion/types'
import { createLogger } from './logger'
import { disposeAll } from './util'
import { CancellationError } from './util/errors'
import * as Is from './util/is'
import { equals } from './util/object'
import { CancellationToken, Disposable } from './util/protocol'
import { byteLength, byteSlice } from './util/string'
const logger = createLogger('events')
const SYNC_AUTOCMDS = ['BufWritePre']
export type Result = void | Promise<void>
export interface PopupChangeEvent {
readonly startcol: number
readonly index: number
readonly word: string
readonly height: number
readonly width: number
readonly row: number
readonly col: number
readonly size: number
readonly scrollbar: boolean
readonly inserted: boolean
readonly move: boolean
}
export interface InsertChange {
readonly lnum: number
readonly col: number
readonly line: string
readonly changedtick: number
pre: string
/**
* Insert character that cause change of this time.
*/
insertChar?: string
}
export enum EventName {
Ready = 'ready',
PumInsert = 'PumInsert',
InsertEnter = 'InsertEnter',
InsertLeave = 'InsertLeave',
CursorHoldI = 'CursorHoldI',
CursorMovedI = 'CursorMovedI',
CursorHold = 'CursorHold',
CursorMoved = 'CursorMoved',
MenuPopupChanged = 'MenuPopupChanged',
InsertCharPre = 'InsertCharPre',
TextChanged = 'TextChanged',
BufEnter = 'BufEnter',
TextChangedI = 'TextChangedI',
TextChangedP = 'TextChangedP',
TextInsert = 'TextInsert',
}
export type BufEvents = 'BufHidden' | 'BufEnter' | 'BufRename'
| 'InsertLeave' | 'TermOpen' | 'InsertEnter' | 'BufCreate' | 'BufUnload'
| 'BufDetach' | 'Enter' | 'LinesChanged'
export type EmptyEvents = 'FocusGained' | 'ColorScheme' | 'FocusLost' | 'InsertSnippet' | 'VimLeavePre' | 'ready' | 'PumNavigate'
export type InsertChangeEvents = 'TextChangedP' | 'TextChangedI'
export type TaskEvents = 'TaskExit' | 'TaskStderr' | 'TaskStdout'
export type WindowEvents = 'WinLeave' | 'WinEnter' | 'WinClosed' | 'WinScrolled'
export type TabEvents = 'TabNew' | 'TabClosed'
export type AllEvents = BufEvents | EmptyEvents | CursorEvents | TaskEvents | WindowEvents | TabEvents
| InsertChangeEvents | 'CompleteStop' | 'CompleteDone' | 'TextChanged' | 'MenuPopupChanged' | 'BufWritePost' | 'BufWritePre'
| 'InsertCharPre' | 'FileType' | 'BufWinEnter' | 'BufWinLeave' | 'VimResized' | 'TermExit'
| 'DirChanged' | 'OptionSet' | 'Command' | 'BufReadCmd' | 'GlobalChange' | 'InputChar'
| 'WinLeave' | 'MenuInput' | 'PromptInsert' | 'FloatBtnClick' | 'InsertSnippet' | 'TextInsert' | 'PromptKeyPress'
export type CursorEvents = CursorHoldEvents | CursorMoveEvents
export type CursorHoldEvents = 'CursorHold' | 'CursorHoldI'
export type CursorMoveEvents = 'CursorMoved' | 'CursorMovedI'
export type OptionValue = string | number | boolean
export interface CursorPosition {
readonly bufnr: number
readonly lnum: number
readonly col: number
readonly insert: boolean
}
export interface LatestInsert {
readonly bufnr: number
readonly character: string
readonly timestamp: number
}
class Events {
private handlers: Map<string, ((...args: any[]) => Promise<unknown>)[]> = new Map()
private _cursor: CursorPosition
private _bufnr = 1
// bufnr & character
private _recentInserts: [number, string][] = []
private _lastChange = 0
private _insertMode = false
private _pumAlignTop = false
private _pumVisible = false
private _completing = false
private _requesting = false
private _ready = false
private _last_pum_insert: string | undefined
public timeout = 1000
// public completing = false
public set requesting(val: boolean) {
this._requesting = val
}
public get requesting(): boolean {
return this._requesting
}
public get ready(): boolean {
return this._ready
}
public set completing(completing: boolean) {
this._completing = completing
this._pumVisible = completing
}
public get completing(): boolean {
return this._completing
}
public get cursor(): CursorPosition {
return this._cursor ?? { bufnr: this._bufnr, col: 1, lnum: 1, insert: false }
}
public get bufnr(): number {
return this._bufnr
}
public get pumvisible(): boolean {
return this._pumVisible
}
public get pumAlignTop(): boolean {
return this._pumAlignTop
}
public get insertMode(): boolean {
return this._insertMode
}
public get lastChangeTs(): number {
return this._lastChange
}
/**
* Resolved when first event fired or timeout
*/
public race(events: AllEvents[], token?: number | CancellationToken): Promise<{ name: AllEvents, args: unknown[] } | undefined> {
let disposables: Disposable[] = []
return new Promise(resolve => {
if (Is.number(token)) {
let timer = setTimeout(() => {
disposeAll(disposables)
resolve(undefined)
}, token)
disposables.push(Disposable.create(() => {
clearTimeout(timer)
}))
} else if (CancellationToken.is(token)) {
token.onCancellationRequested(() => {
disposeAll(disposables)
resolve(undefined)
}, null, disposables)
}
events.forEach(ev => {
this.on(ev, (...args) => {
disposeAll(disposables)
resolve({ name: ev, args })
}, null, disposables)
})
})
}
public async fire(event: string, args: any[]): Promise<void> {
if (event === EventName.Ready) {
this._ready = true
} else if (event == EventName.InsertEnter) {
this._insertMode = true
} else if (event == EventName.InsertLeave) {
this._last_pum_insert = undefined
this._insertMode = false
this._pumVisible = false
this._recentInserts = []
} else if (event == EventName.CursorHoldI || event == EventName.CursorMovedI) {
this._bufnr = args[0]
if (!this._insertMode) {
this._insertMode = true
void this.fire(EventName.InsertEnter, [args[0]])
}
} else if (event == EventName.CursorHold || event == EventName.CursorMoved) {
this._bufnr = args[0]
if (this._insertMode) {
this._insertMode = false
void this.fire(EventName.InsertLeave, [args[0]])
}
} else if (event == EventName.MenuPopupChanged) {
this._pumVisible = true
this._pumAlignTop = args[1] > args[0].row
} else if (event == EventName.InsertCharPre) {
this._recentInserts.push([args[1], args[0]])
} else if (event == EventName.TextChanged) {
this._lastChange = Date.now()
} else if (event == EventName.BufEnter) {
this._bufnr = args[0]
} else if (event == EventName.TextChangedI || event == EventName.TextChangedP) {
let info: InsertChange = args[1]
let pre = byteSlice(info.line ?? '', 0, info.col - 1)
let arr: [number, string][]
// use TextChangedP and disable insert
if (this._last_pum_insert != null && this._last_pum_insert == pre) {
arr = []
event = EventName.TextChangedP
} else {
arr = this._recentInserts.filter(o => o[0] == args[0])
}
this._last_pum_insert = undefined
this._bufnr = args[0]
this._recentInserts = []
this._lastChange = Date.now()
info.pre = pre
// fix cursor since vim not send CursorMovedI event
this._cursor = Object.freeze({
bufnr: args[0],
lnum: info.lnum,
col: info.col,
insert: true
})
if (arr.length && pre.length) {
let character = pre.slice(-1)
if (arr.findIndex(o => o[1] == character) !== -1) {
info.insertChar = character
// make it fires after TextChangedI & TextChangedP
process.nextTick(() => {
void this.fire(EventName.TextInsert, [...args, character])
})
}
}
} else if (event == EventName.PumInsert) {
this._last_pum_insert = args[0]
return
}
if (event == EventName.CursorMoved || event == EventName.CursorMovedI) {
args.push(this._recentInserts.length > 0)
let cursor = {
bufnr: args[0],
lnum: args[1][0],
col: args[1][1],
insert: event == EventName.CursorMovedI
}
if (this._last_pum_insert && byteLength(this._last_pum_insert) + 1 == cursor.col) return
// Avoid CursorMoved event when it's not moved at all
if ((this._cursor && equals(this._cursor, cursor))) return
this._cursor = cursor
}
let cbs = this.handlers.get(event)
if (cbs?.length) {
let fns = cbs.slice()
let traceSlow = SYNC_AUTOCMDS.includes(event)
await Promise.allSettled(fns.map(fn => {
let promiseFn = async () => {
let timer: NodeJS.Timeout
if (traceSlow) {
timer = setTimeout(() => {
console.error(`Slow "${event}" handler detected`, fn['stack'])
logger.error(`Slow "${event}" handler detected`, fn['stack'])
}, this.timeout)
}
try {
await fn(args)
} catch (e) {
let res = shouldIgnore(e)
if (!res) logger.error(`Error on event: ${event}`, e)
}
clearTimeout(timer)
}
return promiseFn()
}))
}
}
public on(event: BufEvents, handler: (bufnr: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: CursorHoldEvents, handler: (bufnr: number, cursor: [number, number]) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: InsertChangeEvents, handler: (bufnr: number, info: InsertChange) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: WindowEvents, handler: (winid: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: CursorMoveEvents, handler: (bufnr: number, cursor: [number, number], hasInsert: boolean) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'TabClosed', handler: (tabids: number[]) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'TabNew', handler: (tabid: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'TextInsert', handler: (bufnr: number, info: InsertChange, character: string) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'FloatBtnClick', handler: (bufnr: number, index: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'PromptKeyPress', handler: (bufnr: number, key: string) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'BufWritePre', handler: (bufnr: number, bufname: string, changedtick: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'TextChanged' | 'BufWritePost', handler: (bufnr: number, changedtick: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'TaskExit', handler: (id: string, code: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'TaskStderr' | 'TaskStdout', handler: (id: string, lines: string[]) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'BufReadCmd', handler: (scheme: string, fullpath: string) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'VimResized', handler: (columns: number, lines: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'Command', handler: (name: string) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'MenuPopupChanged', handler: (event: PopupChangeEvent, cursorline: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'CompleteDone', handler: (item: CompleteDoneItem | {}) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'CompleteStop', handler: (kind: CompleteFinishKind) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'InsertCharPre', handler: (character: string, bufnr: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'FileType', handler: (filetype: string, bufnr: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'BufWinEnter' | 'BufWinLeave', handler: (bufnr: number, winid: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'TermExit', handler: (bufnr: number, status: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'DirChanged', handler: (cwd: string) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'OptionSet' | 'GlobalChange', handler: (option: string, oldVal: OptionValue, newVal: OptionValue) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'InputChar', handler: (session: string, character: string, mode: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: 'PromptInsert', handler: (value: string, bufnr: number) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: EmptyEvents, handler: () => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: AllEvents | AllEvents[], handler: (...args: unknown[]) => Result, thisArg?: any, disposables?: Disposable[]): Disposable
public on(event: AllEvents[] | AllEvents, handler: (...args: any[]) => Result, thisArg?: any, disposables?: Disposable[]): Disposable {
if (Array.isArray(event)) {
let arr = disposables || []
for (let ev of event) {
this.on(ev as any, handler, thisArg, arr)
}
return Disposable.create(() => {
disposeAll(arr)
})
} else {
let arr = this.handlers.get(event) || []
let wrappedhandler = args => new Promise((resolve, reject) => {
try {
Promise.resolve(handler.apply(thisArg ?? null, args)).then(() => {
resolve(undefined)
}, e => {
reject(e)
})
} catch (e) {
reject(e)
}
})
Error.captureStackTrace(wrappedhandler)
arr.push(wrappedhandler)
this.handlers.set(event, arr)
let disposable = Disposable.create(() => {
let idx = arr.indexOf(wrappedhandler)
if (idx !== -1) {
arr.splice(idx, 1)
}
})
if (Array.isArray(disposables)) {
disposables.push(disposable)
}
return disposable
}
}
}
function shouldIgnore(err: any): boolean {
if (err instanceof CancellationError || (err instanceof Error && err.message.includes('transport disconnected'))) return true
return false
}
export default new Events()