-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathdecoration.ts
325 lines (311 loc) Β· 8.15 KB
/
decoration.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
import type { Denops } from "@denops/core";
import * as itertools from "@lambdalisue/itertools";
import { unreachable } from "@lambdalisue/unreachable";
import { batch } from "../batch/batch.ts";
import * as vimFn from "../function/vim/mod.ts";
import * as nvimFn from "../function/nvim/mod.ts";
const cacheKey = "denops_std/buffer/decoration/vimDecorate/rs@1";
const PREFIX = "denops_std:buffer:decoration:decorate";
export type DecorateOptions = {
/**
* Decoration namespace
*/
namespace?: string;
};
export interface Decoration {
/**
* Line number
*/
line: number;
/**
* Column number (bytes)
*/
column: number;
/**
* Length (bytes)
*/
length: number;
/**
* Highlight name
*/
highlight: string;
}
/**
* Decorate the specified buffer with decorations
*
* ```typescript
* import type { Entrypoint } from "jsr:@denops/std";
* import * as fn from "jsr:@denops/std/function";
* import { decorate, open } from "jsr:@denops/std/buffer";
*
* export const main: Entrypoint = async (denops) => {
* await open(denops, "README.md");
* const bufnr = (await fn.bufnr(denops)) as number;
* // ...
* await decorate(denops, bufnr, [
* {
* line: 1,
* column: 1,
* length: 10,
* highlight: "Special",
* },
* {
* line: 2,
* column: 2,
* length: 3,
* highlight: "Comment",
* },
* ]);
* }
* ```
*
* It uses `prop_add_list` in Vim and `nvim_buf_add_highlight` in Neovim to
* decorate the buffer.
*/
export function decorate(
denops: Denops,
bufnr: number,
decorations: readonly Decoration[],
options: Readonly<DecorateOptions> = {},
): Promise<void> {
switch (denops.meta.host) {
case "vim":
return vimDecorate(denops, bufnr, decorations, options);
case "nvim":
return nvimDecorate(denops, bufnr, decorations, options);
default:
unreachable(denops.meta.host);
}
}
/**
* Undecorate the specified buffer
*
* ```typescript
* import type { Entrypoint } from "jsr:@denops/std";
* import * as fn from "jsr:@denops/std/function";
* import { decorate, open, undecorate } from "jsr:@denops/std/buffer";
*
* export const main: Entrypoint = async (denops) => {
* await open(denops, "README.md");
* const bufnr = (await fn.bufnr(denops)) as number;
* // ...
* await decorate(denops, bufnr, [
* {
* line: 1,
* column: 1,
* length: 10,
* highlight: "Special",
* },
* {
* line: 2,
* column: 2,
* length: 3,
* highlight: "Comment",
* },
* ]);
*
* // Do something
*
* // Ranges are 0-based and exclusive.
* // Remove only the first highlight.
* const start = 0;
* const end = 1;
* await undecorate(denops, bufnr, start, end);
* // Start and end are optional. Defaults are 0 and -1 (entire buffer).
* // await undecorate(denops, bufnr);
* }
* ```
*
* It uses `prop_add` in Vim and `nvim_buf_add_highlight` in Neovim to decorate the
* buffer.
*/
export function undecorate(
denops: Denops,
bufnr: number,
start = 0,
end = -1,
options: Readonly<DecorateOptions> = {},
): Promise<void> {
switch (denops.meta.host) {
case "vim":
return vimUndecorate(denops, bufnr, start, end, options);
case "nvim":
return nvimUndecorate(denops, bufnr, start, end, options);
default:
unreachable(denops.meta.host);
}
}
export function listDecorations(
denops: Denops,
bufnr: number,
options: Readonly<DecorateOptions> = {},
): Promise<Decoration[]> {
switch (denops.meta.host) {
case "vim":
return vimListDecorations(denops, bufnr, options);
case "nvim":
return nvimListDecorations(denops, bufnr, options);
default:
unreachable(denops.meta.host);
}
}
function uniq<T>(array: readonly T[]): T[] {
return [...new Set(array)];
}
async function vimDecorate(
denops: Denops,
bufnr: number,
decorations: readonly Decoration[],
options: Readonly<DecorateOptions> = {},
): Promise<void> {
const prefix = options.namespace ?? `${PREFIX}:${denops.name}`;
const toPropType = (n: string) => `${prefix}:${n}`;
const rs = (denops.context[cacheKey] ?? new Set()) as Set<string>;
denops.context[cacheKey] = rs;
const hs = uniq(decorations.map((v) => v.highlight)).filter((v) =>
!rs.has(v)
);
const decoMap = new Map<string, Set<[number, number, number, number]>>();
for (const deco of decorations) {
const propType = toPropType(deco.highlight);
const props = decoMap.get(propType) ?? new Set();
props.add([deco.line, deco.column, deco.line, deco.column + deco.length]);
decoMap.set(propType, props);
}
await batch(denops, async (denops) => {
for (const highlight of hs) {
const propType = toPropType(highlight);
await vimFn.prop_type_add(denops, propType, {
highlight,
combine: false,
});
rs.add(highlight);
}
let id = 1;
for (const [type, props] of decoMap.entries()) {
await vimFn.prop_add_list(denops, { bufnr, type, id: id++ }, [...props]);
}
});
}
async function vimUndecorate(
denops: Denops,
bufnr: number,
start: number,
end: number,
options: Readonly<DecorateOptions>,
): Promise<void> {
const prefix = options.namespace ?? `${PREFIX}:${denops.name}`;
const propList = await vimFn.prop_list(denops, start + 1, {
bufnr,
end_lnum: end,
}) as { id: number; type: string }[];
const propIds = new Set(
propList
.filter((p) => p.type.startsWith(`${prefix}:`))
.map((p) => p.id),
);
await batch(denops, async (denops) => {
for (const propId of propIds) {
await vimFn.prop_remove(denops, { id: propId, bufnr, all: true });
}
});
}
async function vimListDecorations(
denops: Denops,
bufnr: number,
options: Readonly<DecorateOptions>,
): Promise<Decoration[]> {
const prefix = options.namespace ?? `${PREFIX}:${denops.name}`;
const props = await vimFn.prop_list(denops, 1, {
bufnr,
end_lnum: -1,
}) as {
col: number;
end: number;
id: number;
length: number;
lnum: number;
start: number;
type: string;
type_bufnr: number;
}[];
return props
.filter((prop) => prop.type.startsWith(`${prefix}:`))
.map((prop) => ({
line: prop.lnum,
column: prop.col,
length: prop.length,
highlight: prop.type.split(":").pop() as string,
}));
}
async function nvimDecorate(
denops: Denops,
bufnr: number,
decorations: readonly Decoration[],
options: Readonly<DecorateOptions>,
): Promise<void> {
const prefix = options.namespace ?? `${PREFIX}:${denops.name}`;
const ns = await nvimFn.nvim_create_namespace(denops, prefix);
for (const chunk of itertools.chunked(decorations, 1000)) {
await batch(denops, async (denops) => {
for (const deco of chunk) {
await nvimFn.nvim_buf_add_highlight(
denops,
bufnr,
ns,
deco.highlight,
deco.line - 1,
deco.column - 1,
deco.column - 1 + deco.length,
);
}
});
}
}
async function nvimUndecorate(
denops: Denops,
bufnr: number,
start: number,
end: number,
options: Readonly<DecorateOptions>,
): Promise<void> {
const prefix = options.namespace ?? `${PREFIX}:${denops.name}`;
const ns = await nvimFn.nvim_create_namespace(denops, prefix);
await nvimFn.nvim_buf_clear_namespace(denops, bufnr, ns, start, end);
}
async function nvimListDecorations(
denops: Denops,
bufnr: number,
options: Readonly<DecorateOptions>,
): Promise<Decoration[]> {
const prefix = options.namespace ?? `${PREFIX}:${denops.name}`;
const ns = await nvimFn.nvim_create_namespace(denops, prefix);
const extmarks = await nvimFn.nvim_buf_get_extmarks(
denops,
bufnr,
ns,
0,
-1,
{ details: true },
) as [
extmark_id: number,
row: number,
col: number,
{
hl_group: string;
hl_eol: boolean;
end_right_gravity: boolean;
priority: number;
right_gravity: boolean;
end_col: number;
ns_id: number;
end_row: number;
},
][];
return extmarks.map((extmark) => ({
line: extmark[1] + 1,
column: extmark[2] + 1,
length: extmark[3].end_col - extmark[2],
highlight: extmark[3].hl_group,
}));
}