-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
app.ts
386 lines (344 loc) · 10.8 KB
/
app.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
import { Ddc } from "./ddc.ts";
import { ContextBuilder } from "./context.ts";
import {
Context,
DdcEvent,
DdcExtType,
DdcOptions,
DdcUserData,
Item,
} from "./types.ts";
import {
batch,
Denops,
ensureArray,
ensureNumber,
ensureObject,
ensureString,
fn,
Lock,
op,
vars,
} from "./deps.ts";
import { createCallbackContext } from "./callback.ts";
type RegisterArg = {
path: string;
name: string;
type: DdcExtType;
};
export async function main(denops: Denops) {
const ddc: Ddc = new Ddc();
const contextBuilder = new ContextBuilder();
const cbContext = createCallbackContext();
const lock = new Lock();
let queuedEvent: DdcEvent | null = null;
denops.dispatcher = {
async register(arg1: unknown): Promise<void> {
const arg = ensureObject(arg1) as RegisterArg;
await ddc.register(arg.type, arg.path, arg.name);
},
alias(arg1: unknown, arg2: unknown, arg3: unknown): Promise<void> {
ddc.registerAlias(
ensureString(arg1) as DdcExtType,
ensureString(arg2),
ensureString(arg3),
);
return Promise.resolve();
},
setGlobal(arg1: unknown): Promise<void> {
const options = ensureObject(arg1);
contextBuilder.setGlobal(options);
return Promise.resolve();
},
setFiletype(arg1: unknown, arg2: unknown): Promise<void> {
const options = ensureObject(arg1);
const filetype = ensureString(arg2);
contextBuilder.setFiletype(filetype, options);
return Promise.resolve();
},
setContext(arg1: unknown, arg2: unknown): Promise<void> {
const filetype = ensureString(arg1);
const id = ensureString(arg2);
contextBuilder.setContext(filetype, id);
return Promise.resolve();
},
setBuffer(arg1: unknown, arg2: unknown): Promise<void> {
const options = ensureObject(arg1);
const bufnr = ensureNumber(arg2);
contextBuilder.setBuffer(bufnr, options);
return Promise.resolve();
},
patchGlobal(arg1: unknown): Promise<void> {
const options = ensureObject(arg1);
contextBuilder.patchGlobal(options);
return Promise.resolve();
},
patchFiletype(arg1: unknown, arg2: unknown): Promise<void> {
const options = ensureObject(arg1);
const filetype = ensureString(arg2);
contextBuilder.patchFiletype(filetype, options);
return Promise.resolve();
},
patchBuffer(arg1: unknown, arg2: unknown): Promise<void> {
const options = ensureObject(arg1);
const bufnr = ensureNumber(arg2);
contextBuilder.patchBuffer(bufnr, options);
return Promise.resolve();
},
getGlobal(): Promise<Partial<DdcOptions>> {
return Promise.resolve(contextBuilder.getGlobal());
},
getFiletype(): Promise<Record<string, Partial<DdcOptions>>> {
return Promise.resolve(contextBuilder.getFiletype());
},
getContext(): Promise<Record<string, string>> {
return Promise.resolve(contextBuilder.getContext());
},
getBuffer(): Promise<Record<number, Partial<DdcOptions>>> {
return Promise.resolve(contextBuilder.getBuffer());
},
getCurrent(): Promise<DdcOptions> {
return Promise.resolve(contextBuilder.getCurrent(denops));
},
async manualComplete(arg1: unknown, arg2: unknown): Promise<void> {
const sources = arg1 as string[];
const ui = arg2 as string;
const [skip, context, options] = await contextBuilder
.createContext(denops, "Manual");
if (skip) return;
// Hide the current completion
await ddc.hide(denops, context, options);
const mode = await fn.mode(denops);
if (mode == "c") {
// Use cmdlineSources instead
options.sources = options.cmdlineSources;
}
if (sources.length != 0) {
options.sources = sources;
// Load sources
await ddc.autoload(
denops,
"source",
options.sources,
);
}
if (ui.length != 0) {
options.ui = ui;
// Load UI
await ddc.autoload(
denops,
"ui",
[options.ui],
);
}
cbContext.revoke();
await doCompletion(denops, context, options);
},
async updateItems(arg1: unknown, arg2: unknown): Promise<void> {
const name = ensureString(arg1);
const items = ensureArray(arg2) as Item[];
ddc.updateItems(name, items);
const [skip, context, options] = await contextBuilder
.createContext(denops, "Update");
if (skip) return;
cbContext.revoke();
await doCompletion(denops, context, options);
},
async onEvent(arg1: unknown): Promise<void> {
queuedEvent = ensureString(arg1) as DdcEvent;
if (lock.locked()) {
const mode = await fn.mode(denops);
if (mode != "c") {
// Close current popupmenu.
const [_, context, options] = await contextBuilder
.createContext(denops, queuedEvent);
await ddc.hide(denops, context, options);
}
return;
}
// Note: must be locked
await lock.with(async () => {
while (queuedEvent != null) {
const event = queuedEvent;
queuedEvent = null;
await _onEvent(event);
}
});
},
// deno-lint-ignore require-await
async onCallback(id: unknown, payload: unknown): Promise<void> {
cbContext.emit(ensureString(id), payload);
},
async onCompleteDone(arg1: unknown, arg2: unknown): Promise<void> {
const sourceName = ensureString(arg1);
const userData = ensureObject(arg2) as DdcUserData;
const [skip, context, options] = await contextBuilder
.createContext(denops, "CompleteDone");
if (skip) return;
cbContext.revoke();
await ddc.onCompleteDone(
denops,
context,
cbContext.createOnCallback(),
options,
sourceName,
userData,
);
},
async clear(arg1: unknown): Promise<void> {
const event = ensureString(arg1) as DdcEvent;
const [_, context, options] = await contextBuilder.createContext(
denops,
event,
);
await ddc.hide(denops, context, options);
},
};
async function _onEvent(event: DdcEvent): Promise<void> {
const [skip, context, options] = await contextBuilder
.createContext(denops, event);
const mode = await fn.mode(denops);
if (mode == "c") {
// Use cmdlineSources instead
options.sources = options.cmdlineSources;
}
await ddc.onEvent(
denops,
context,
cbContext.createOnCallback(),
options,
);
if (skip) return;
cbContext.revoke();
if (event != "InsertEnter" && await fn.mode(denops) == "n") {
return;
}
const skipCompletion = await ddc.skipCompletion(denops, context, options);
if (skipCompletion) {
return;
}
if (await checkSkipCompletion(event, context, options)) {
await cancelCompletion(denops, context, options);
return;
}
// Check auto complete delay.
if (options.autoCompleteDelay > 0) {
// Cancel previous completion
await cancelCompletion(denops, context, options);
await new Promise((resolve) =>
setTimeout(
resolve,
options.autoCompleteDelay,
)
);
const changedTick = vars.b.get(denops, "changedtick") as Promise<
number
>;
if (context.changedTick != await changedTick) {
// Input is changed. Skip invalid completion.
return;
}
}
await doCompletion(denops, context, options);
}
async function checkSkipCompletion(
event: DdcEvent,
context: Context,
options: DdcOptions,
): Promise<boolean> {
// Note: Don't complete when backspace by default, because of completion
// flicker.
const prevInput = await vars.g.get(denops, "ddc#_prev_input") as string;
const checkBackSpace = (!options.backspaceCompletion &&
context.input != prevInput &&
context.input.length + 1 == prevInput.length &&
prevInput.startsWith(context.input));
if (checkBackSpace && options.completionMode == "popupmenu") {
await vars.g.set(denops, "ddc#_prev_input", context.input);
return true;
}
// Skip special buffers.
const buftype = await op.buftype.getLocal(denops);
const mode = await fn.mode(denops);
if (
buftype != "" && !options.specialBufferCompletion && mode != "c"
) {
return true;
}
if (options.autoCompleteEvents.indexOf(event) < 0) {
return true;
}
return false;
}
async function cancelCompletion(
denops: Denops,
context: Context,
options: DdcOptions,
): Promise<void> {
await batch(denops, async (denops: Denops) => {
await vars.g.set(denops, "ddc#_complete_pos", -1);
await vars.g.set(denops, "ddc#_items", []);
if (options.completionMode != "manual") {
await ddc.hide(denops, context, options);
}
});
}
async function doCompletion(
denops: Denops,
context: Context,
options: DdcOptions,
): Promise<void> {
const [completePos, items] = await ddc.gatherResults(
denops,
context,
cbContext.createOnCallback(),
options,
);
await (async function write() {
await batch(denops, async (denops: Denops) => {
await vars.g.set(denops, "ddc#_prev_input", context.input);
await vars.g.set(denops, "ddc#_complete_pos", completePos);
await vars.g.set(denops, "ddc#_items", items);
await vars.g.set(denops, "ddc#_sources", options.sources);
await vars.g.set(denops, "ddc#_changedtick", context.changedTick);
});
if (items.length == 0) {
await ddc.hide(denops, context, options);
} else if (
options.completionMode == "popupmenu" ||
context.event == "Manual"
) {
await ddc.show(denops, context, options, completePos, items);
} else if (options.completionMode == "manual") {
// through
}
})();
}
const checkTextChangedT = await fn.exists(denops, "##TextChangedT");
await batch(denops, async (denops: Denops) => {
await vars.g.set(denops, "ddc#_items", []);
await vars.g.set(denops, "ddc#_changedtick", 0);
await vars.g.set(denops, "ddc#_complete_pos", -1);
await vars.g.set(denops, "ddc#_prev_input", "");
await vars.g.set(denops, "ddc#_sources", []);
await denops.cmd("doautocmd <nomodeline> User DDCReady");
await denops.cmd("autocmd! User DDCReady");
await denops.call("ddc#_on_event", "Initialize");
ddc.registerAutocmd(denops, [
"InsertEnter",
"InsertLeave",
"TextChangedI",
"TextChangedP",
]);
if (checkTextChangedT) {
ddc.registerAutocmd(denops, [
"TextChangedT",
]);
}
await denops.cmd(
"autocmd ddc CmdlineChanged * " +
"if getcmdtype() ==# '=' || getcmdtype() ==# '@' |" +
" call ddc#_on_event('CmdlineChanged') | endif",
);
});
}