-
Notifications
You must be signed in to change notification settings - Fork 0
/
unprintable.ts
379 lines (342 loc) · 11.2 KB
/
unprintable.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
import type {
Context,
Item,
OnCallback,
PumHighlight,
} from "https://deno.land/x/ddc_vim@v5.0.0/types.ts";
import type { Denops } from "https://deno.land/x/denops_std@v6.5.0/mod.ts";
import {
append,
getline,
mode,
type Position,
printf,
setcmdline,
setline,
setpos,
strlen,
} from "https://deno.land/x/denops_std@v6.5.0/function/mod.ts";
import { batch } from "https://deno.land/x/denops_std@v6.5.0/batch/mod.ts";
import { vim } from "https://deno.land/x/denops_std@v6.5.0/variable/mod.ts";
import { defer } from "https://deno.land/x/denops_defer@v1.0.0/batch/defer.ts";
// deno-fmt-ignore
const UNPRINTABLE_CHARS = [
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f,
] as const;
const UNPRINTABLE_CHAR_LENGTH = 2; // "^@".length
const UNPRINTABLE_BYTE_LENGTH = 2; // strlen("^@")
type UnprintableData = {
origWord: string;
origNextInput: string;
};
export type UnprintableUserData = {
unprintable?: never;
};
export type UnprintableOptions = {
/** Highlight name for unprintable chars.
*
* Default is "ddc_unprintable".
*/
highlightName?: string;
/** Highlight group for unprintable chars.
*
* Default is "SpecialKey".
*/
highlightGroup?: string;
/** Placeholder text for unprintable char.
*
* Must be a single character.
* Default is "?".
*/
placeholder?: string;
/** Max width of the abbreviates column.
*
* If 0 is specified, be unlimited.
* Default is 0.
*/
abbrWidth?: number;
/** Callback Id base string.
*
* If empty, use random id.
*/
callbackId?: string;
};
export class Unprintable<
UserData extends UnprintableUserData = UnprintableUserData,
> implements Required<Omit<UnprintableOptions, "callbackId">> {
// deno-lint-ignore no-control-regex
#reUnprintableChar = /[\x00-\x1f]/g;
#highlightName: string;
#highlightGroup: string;
#placeholder = "?";
#abbrWidth = 0;
#callbackId: string;
#completeDoneCount = 0;
constructor(opts: UnprintableOptions = {}) {
this.#highlightName = opts.highlightName ?? "ddc_unprintable";
this.#highlightGroup = opts.highlightGroup ?? "SpecialKey";
if (opts.placeholder !== undefined) this.placeholder = opts.placeholder;
if (opts.abbrWidth !== undefined) this.abbrWidth = opts.abbrWidth;
this.#callbackId = opts.callbackId ?? `unprintable/${makeId()}`;
}
get highlightName(): string {
return this.#highlightName;
}
set highlightName(value: string) {
this.#highlightName = value;
}
get highlightGroup(): string {
return this.#highlightGroup;
}
set highlightGroup(value: string) {
this.#highlightGroup = value;
}
get placeholder(): string {
return this.#placeholder;
}
set placeholder(value: string) {
if (value.length !== 1) {
throw new TypeError("placeholder must be a single character.");
}
this.#placeholder = value;
}
get abbrWidth(): number {
return this.#abbrWidth;
}
set abbrWidth(value: number) {
if (value < 0 || value !== (value | 0)) {
throw new TypeError(
"abbrWidth must be an interger greater than or equal to 0.",
);
}
this.#abbrWidth = value | 0;
}
/** Should convert items by this in `BaseSource.gather()`.
*
* If `Item.word` contains unprintable characters, it will be converted to
* `UnprintableOptions.placeholder`.
* `Item.abbr` and `Item.highlights` are generated and added.
*/
async convertItems(
denops: Denops,
items: Item<UserData>[],
nextInput: string,
): Promise<Item<UserData>[]> {
const abbrWidth = Math.max(0, this.#abbrWidth);
const abbrFormat = `%.${abbrWidth}S`;
const itemSlices = await defer(denops, (helper) =>
items.map((item) => {
const origWord = item.word;
const word = this.#makeWord(origWord);
const longAbbr = this.#makeAbbr(origWord);
const abbr = abbrWidth
? printf(helper, abbrFormat, longAbbr) as Promise<string>
: longAbbr;
const slices = (abbrWidth ? origWord.slice(0, abbrWidth) : origWord)
.split(this.#reUnprintableChar).slice(0, -1)
.map((slice) => ({
chars: slice.length,
bytes: strlen(helper, slice) as Promise<number>,
}));
return { origWord, word, abbr, slices };
}));
return items.map((item, index) => {
const { origWord, word, abbr, slices } = itemSlices[index];
return {
...item,
word,
abbr,
highlights: [
...(item.highlights ?? []),
...this.#generateHighlights(abbr, slices),
],
user_data: {
...item.user_data,
unprintable: {
origWord,
origNextInput: nextInput,
} as UnprintableData,
} as unknown as UserData,
};
});
}
/** Should call this in `BaseSource.onInit()`. */
async onInit(args: { denops: Denops }): Promise<void> {
const chars = [
...UNPRINTABLE_CHARS,
...(await this.#getUnprintableChars(args.denops)),
];
this.#reUnprintableChar = this.#makeUnprintableRegExp(chars);
}
/** Should call this in `BaseSource.onCompleteDone()`. */
async onCompleteDone(
args: {
denops: Denops;
onCallback: OnCallback;
userData: UserData;
context: Context;
},
): Promise<void> {
this.#completeDoneCount++;
const { denops, onCallback, userData } = args;
const { origWord, origNextInput } = userData
.unprintable as unknown as UnprintableData;
let { input, nextInput, lineNr } = args.context;
// If no unprintable contains, do nothing.
if (!this.#reUnprintableChar.test(origWord)) return;
const [vimMode, vchar] = await defer(denops, (helper) => ([
mode(helper),
vim.get(helper, "char") as Promise<string>,
] as const));
const tail = this.#makeWord(origWord) + origNextInput;
const head = nextInput ? tail.slice(0, -nextInput.length) : tail;
let replaceLastLine = false;
if (tail.length > input.length + nextInput.length) {
--lineNr;
input = await getline(denops, lineNr) + input;
replaceLastLine = true;
}
let userInput: string;
let prefix: string;
if (tail.length > nextInput.length) {
userInput = head && input.endsWith(head) ? "" : input.at(-1) ?? "";
prefix = input.slice(0, -head.length - userInput.length);
} else {
userInput = "";
prefix = (input + nextInput).slice(0, -tail.length);
}
userInput += vchar;
const line = prefix + origWord + origNextInput;
const [lineHead, lineTail] = nextInput
? [line.slice(0, -nextInput.length), line.slice(-nextInput.length)]
: [line, ""];
if (vimMode === "c") {
// Replace cmdline
const cmdHead = textToCmdline(lineHead + userInput);
const cmdLine = cmdHead + textToCmdline(lineTail);
const pos = await strlen(denops, cmdHead) as number + 1;
await setcmdline(denops, cmdLine, pos);
} else {
// Replace buffer
const lines = textToRegContents(lineHead + userInput + lineTail);
const linesHead = textToRegContents(lineHead + userInput);
const cursorLN = lineNr + linesHead.length - 1;
const cursorCol = await strlen(denops, linesHead.at(-1)) as number + 1;
const pos: Position = [0, cursorLN, cursorCol, 0];
const [firstLine, middleLines, lastLine] = replaceLastLine
? [lines[0], lines.slice(1, -1), lines.at(-1)]
: [lines[0], lines.slice(1), undefined];
const setBuffer = (): Promise<void> =>
batch(denops, async (helper) => {
await setline(helper, lineNr, firstLine);
if (middleLines.length > 0) {
await append(helper, lineNr, middleLines);
}
if (lastLine !== undefined) {
await setline(helper, lineNr + middleLines.length + 1, lastLine);
}
await setpos(helper, ".", pos);
});
try {
await setBuffer();
} catch (e: unknown) {
if (!/:E565:/.test(`${e}`)) {
throw e;
}
// Fallback: While completion is active buffer text cannot be changed.
const id = `${this.#callbackId}/complete/${this.#completeDoneCount}`;
onCallback(id).then(setBuffer);
await denops.cmd(
`call feedkeys("\\<Cmd>call ddc#callback('${id}')\\<CR>", 'in')`,
);
}
}
}
#getUnprintableChars(denops: Denops): Promise<number[]> {
return denops.eval(
"range(0x100)->filter({ _, n -> nr2char(n) !~# '\\p' })",
) as Promise<number[]>;
}
#makeUnprintableRegExp(unprintableChars: number[]): RegExp {
// generate RegExp e.g.: /[\x00-\x1f\x7f-\x9f]/g
const unprintableSet = new Set(unprintableChars);
const lastGuard = 0x100;
unprintableSet.delete(lastGuard);
const xhh = (n: number) => "\\x" + `0${n.toString(16)}`.slice(-2);
const range: string[] = [];
for (let start = -1, code = 0; code <= lastGuard; ++code) {
if (start < 0 && unprintableSet.has(code)) {
start = code;
} else if (start >= 0 && !unprintableSet.has(code)) {
const end = code - 1;
range.push(start === end ? xhh(start) : xhh(start) + "-" + xhh(end));
start = -1;
}
}
return new RegExp(`[${range.join("")}]`, "g");
}
#makeWord(word: string): string {
return word.replaceAll(this.#reUnprintableChar, this.#placeholder);
}
#makeAbbr(word: string): string {
return word.replaceAll(this.#reUnprintableChar, unpritableCharToDisplay);
}
#generateHighlights(
abbr: string,
abbrSlices: { chars: number; bytes: number }[],
): PumHighlight[] {
const highlights: PumHighlight[] = [];
let lastHighlight: PumHighlight | undefined;
let len = 0; // [chars]
let col = 1; // [bytes]
for (const { chars, bytes } of abbrSlices) {
if (bytes === 0 && lastHighlight) {
// increase width
lastHighlight.width += UNPRINTABLE_BYTE_LENGTH;
} else {
len += chars;
col += bytes;
if (len >= abbr.length) {
break;
}
// add new highlight
lastHighlight = {
name: this.#highlightName,
type: "abbr",
hl_group: this.#highlightGroup,
col,
width: UNPRINTABLE_BYTE_LENGTH,
};
highlights.push(lastHighlight);
}
len += UNPRINTABLE_CHAR_LENGTH;
col += UNPRINTABLE_BYTE_LENGTH;
if (len >= abbr.length) {
lastHighlight.width -= len - abbr.length;
break;
}
}
return highlights;
}
}
function makeId(): string {
return ("0000000" + Math.floor(Math.random() * 0xffffffff).toString(16))
.slice(-8);
}
function textToRegContents(text: string): string[] {
return text.split("\n").map((s) => s.replaceAll("\0", "\n"));
}
function textToCmdline(text: string): string {
return text.replaceAll("\n", "\r").replaceAll("\x00", "\n");
}
function unpritableCharToDisplay(c: string): string {
const code = c.charCodeAt(0);
if (code <= 0x1f) return "^" + String.fromCharCode(code + 0x40);
if (code === 0x7f) return "^?";
if (code <= 0x9f) return "~" + String.fromCharCode(code - 0x40);
if (code <= 0xfe) return "|" + String.fromCharCode(code - 0x80);
return "~?";
}