@@ -15,6 +15,12 @@ import { fromFileUrl } from "@std/path/from-file-url";
1515import { join } from "@std/path/join" ;
1616import { dirname } from "@std/path/dirname" ;
1717
18+ // Cache size limit: in practice only a handful of distinct keywordPattern /
19+ // iskeyword combinations appear, so 64 entries is more than enough.
20+ const KEYWORD_CACHE_MAX = 64 ;
21+ const convertKeywordPatternCache = new Map < string , string > ( ) ;
22+ const keywordRegExpCache = new Map < string , RegExp > ( ) ;
23+
1824export async function convertKeywordPattern (
1925 denops : Denops ,
2026 keywordPattern : string ,
@@ -23,13 +29,37 @@ export async function convertKeywordPattern(
2329 const iskeyword = bufnr === undefined
2430 ? await op . iskeyword . getLocal ( denops )
2531 : await op . iskeyword . getBuffer ( denops , bufnr ) ;
32+ // Neither iskeyword nor keywordPattern contain NUL bytes, so this
33+ // composite key is unambiguous.
34+ const cacheKey = keywordPattern + "\0" + iskeyword ;
35+ const cached = convertKeywordPatternCache . get ( cacheKey ) ;
36+ if ( cached !== undefined ) {
37+ return cached ;
38+ }
2639 const keyword = vimoption2ts ( iskeyword ) ;
2740 const replaced = keywordPattern
2841 . replaceAll ( "\\k" , "[" + keyword + "]" )
2942 . replaceAll ( "[:keyword:]" , keyword ) ;
43+ if ( convertKeywordPatternCache . size >= KEYWORD_CACHE_MAX ) {
44+ convertKeywordPatternCache . clear ( ) ;
45+ }
46+ convertKeywordPatternCache . set ( cacheKey , replaced ) ;
3047 return replaced ;
3148}
3249
50+ export function getKeywordRegExp ( expandedPattern : string ) : RegExp {
51+ const cached = keywordRegExpCache . get ( expandedPattern ) ;
52+ if ( cached !== undefined ) {
53+ return cached ;
54+ }
55+ const re = new RegExp ( expandedPattern ) ;
56+ if ( keywordRegExpCache . size >= KEYWORD_CACHE_MAX ) {
57+ keywordRegExpCache . clear ( ) ;
58+ }
59+ keywordRegExpCache . set ( expandedPattern , re ) ;
60+ return re ;
61+ }
62+
3363// See https://github.com/vim-denops/denops.vim/issues/358 for details
3464export function isDenoCacheIssueError ( e : unknown ) : boolean {
3565 const expects = [
0 commit comments