@@ -5,6 +5,8 @@ import * as nvimFn from "../function/nvim/mod.ts";
55import * as itertools from "https://deno.land/x/[email protected] /mod.ts" ; 66import { unreachable } from "https://deno.land/x/[email protected] /mod.ts" ; 77
8+ const cacheKey = Symbol ( "denops_std/buffer/decoration/vimDecorate/rs" ) ;
9+
810export 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+
3743async function vimDecorate (
3844 denops : Denops ,
3945 bufnr : number ,
4046 decorations : Decoration [ ] ,
4147) : Promise < void > {
42- const toPropType = ( n : string ) => `denops_std:buffer: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
0 commit comments