@@ -5,6 +5,8 @@ import * as nvimFn from "../function/nvim/mod.ts";
5
5
import * as itertools from "https://deno.land/x/itertools@v1.0.2/mod.ts" ;
6
6
import { unreachable } from "https://deno.land/x/unreachable@v0.1.0/mod.ts" ;
7
7
8
+ const cacheKey = Symbol ( "denops_std/buffer/decoration/vimDecorate/rs" ) ;
9
+
8
10
export type Decoration = {
9
11
// Line number
10
12
line : number ;
@@ -34,26 +36,42 @@ export function decorate(
34
36
}
35
37
}
36
38
39
+ function uniq < T > ( array : T [ ] ) : T [ ] {
40
+ return [ ...new Set ( array ) ] ;
41
+ }
42
+
37
43
async function vimDecorate (
38
44
denops : Denops ,
39
45
bufnr : number ,
40
46
decorations : Decoration [ ] ,
41
47
) : 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 ,
53
60
} ) ;
61
+ rs . add ( highlight ) ;
54
62
}
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
+ } ) ;
57
75
}
58
76
}
59
77
@@ -64,7 +82,7 @@ async function nvimDecorate(
64
82
) : Promise < void > {
65
83
const ns = await nvimFn . nvim_create_namespace (
66
84
denops ,
67
- "gin :decoration:decorate" ,
85
+ "denops_std:buffer :decoration:decorate" ,
68
86
) ;
69
87
for ( const chunk of itertools . chunked ( decorations , 1000 ) ) {
70
88
await batch . batch ( denops , async ( denops ) => {
0 commit comments