Skip to content

Commit 27edd51

Browse files
authored
Merge pull request #143 from vim-denops/improve-decoration
🐛 Fix `decoration` module on Vim
2 parents 363b3fd + 034f227 commit 27edd51

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

denops_std/buffer/decoration.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as nvimFn from "../function/nvim/mod.ts";
55
import * as itertools from "https://deno.land/x/itertools@v1.0.2/mod.ts";
66
import { unreachable } from "https://deno.land/x/unreachable@v0.1.0/mod.ts";
77

8+
const cacheKey = Symbol("denops_std/buffer/decoration/vimDecorate/rs");
9+
810
export type Decoration = {
911
// Line number
1012
line: number;
@@ -34,26 +36,42 @@ export function decorate(
3436
}
3537
}
3638

39+
function uniq<T>(array: T[]): T[] {
40+
return [...new Set(array)];
41+
}
42+
3743
async function vimDecorate(
3844
denops: Denops,
3945
bufnr: number,
4046
decorations: Decoration[],
4147
): Promise<void> {
42-
const toPropType = (n: string) => `gin:decoration:decorate:${n}`;
43-
try {
44-
for (const chunk of itertools.chunked(decorations, 1000)) {
45-
await batch.batch(denops, async (denops) => {
46-
for (const deco of chunk) {
47-
await vimFn.prop_add(denops, deco.line, deco.column, {
48-
bufnr,
49-
length: deco.length,
50-
type: toPropType(deco.highlight),
51-
});
52-
}
48+
const toPropType = (n: string) => `denps_std:buffer:decoration:decorate:${n}`;
49+
const rs = (denops.context[cacheKey] ?? new Set()) as Set<string>;
50+
denops.context[cacheKey] = rs;
51+
const hs = uniq(decorations.map((v) => v.highlight)).filter((v) =>
52+
!rs.has(v)
53+
);
54+
await batch.batch(denops, async (denops) => {
55+
for (const highlight of hs) {
56+
const propType = toPropType(highlight);
57+
await vimFn.prop_type_add(denops, propType, {
58+
highlight,
59+
combine: false,
5360
});
61+
rs.add(highlight);
5462
}
55-
} catch {
56-
// Fail silently
63+
});
64+
for (const chunk of itertools.chunked(decorations, 1000)) {
65+
await batch.batch(denops, async (denops) => {
66+
for (const deco of chunk) {
67+
const propType = toPropType(deco.highlight);
68+
await vimFn.prop_add(denops, deco.line, deco.column, {
69+
bufnr,
70+
length: deco.length,
71+
type: propType,
72+
});
73+
}
74+
});
5775
}
5876
}
5977

@@ -64,7 +82,7 @@ async function nvimDecorate(
6482
): Promise<void> {
6583
const ns = await nvimFn.nvim_create_namespace(
6684
denops,
67-
"gin:decoration:decorate",
85+
"denops_std:buffer:decoration:decorate",
6886
);
6987
for (const chunk of itertools.chunked(decorations, 1000)) {
7088
await batch.batch(denops, async (denops) => {

0 commit comments

Comments
 (0)