Skip to content

Commit d4c0de6

Browse files
committed
fix: add Cache class, cache with bufnr and key
1 parent 44ee797 commit d4c0de6

File tree

2 files changed

+50
-7
lines changed

2 files changed

+50
-7
lines changed

lua/hlchunk/mods/indent/init.lua

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local IndentConf = require("hlchunk.mods.indent.indent_conf")
33
local class = require("hlchunk.utils.class")
44
local indentHelper = require("hlchunk.utils.indentHelper")
55
local Scope = require("hlchunk.utils.scope")
6+
local Cache = require("hlchunk.utils.cache")
67
local throttle = require("hlchunk.utils.timer").throttle
78
local cFunc = require("hlchunk.utils.cFunc")
89

@@ -11,7 +12,7 @@ local fn = vim.fn
1112
local ROWS_INDENT_RETCODE = indentHelper.ROWS_INDENT_RETCODE
1213

1314
---@class IndentMetaInfo : MetaInfo
14-
---@field cache table<number, number>
15+
---@field cache Cache
1516

1617
local constructor = function(self, conf, meta)
1718
local default_meta = {
@@ -21,7 +22,7 @@ local constructor = function(self, conf, meta)
2122
ns_id = api.nvim_create_namespace("indent"),
2223
shiftwidth = fn.shiftwidth(),
2324
leftcol = fn.winsaveview().leftcol,
24-
cache = {},
25+
cache = Cache(),
2526
}
2627

2728
BaseMod.init(self, conf, meta)
@@ -62,13 +63,13 @@ function IndentMod:render(range)
6263
local non_cached_start = range.start
6364
local non_cached_finish = range.finish
6465
for i = range.start, range.finish do
65-
if not self.meta.cache[i] then
66+
if not self.meta.cache:has(range.bufnr, i) then
6667
non_cached_start = i
6768
break
6869
end
6970
end
7071
for i = non_cached_start, range.finish do
71-
if self.meta.cache[i] then
72+
if self.meta.cache:has(range.bufnr, i) then
7273
non_cached_finish = i - 1
7374
break
7475
end
@@ -85,10 +86,10 @@ function IndentMod:render(range)
8586
return
8687
end
8788
for lnum, indent in pairs(rows_indent) do
88-
self.meta.cache[lnum] = indent
89+
self.meta.cache:set(range.bufnr, lnum, indent)
8990
end
9091
for lnum = range.start, range.finish do
91-
self:renderLine(range.bufnr, lnum, self.meta.cache[lnum])
92+
self:renderLine(range.bufnr, lnum, self.meta.cache:get(range.bufnr, lnum))
9293
end
9394
end
9495

@@ -128,7 +129,7 @@ function IndentMod:createAutocmd()
128129
api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
129130
group = self.meta.augroup_name,
130131
callback = function(e)
131-
self.meta.cache = {}
132+
self.meta.cache:clear(e.buf)
132133
throttle_render_cb_with_pre_hook(e)
133134
end,
134135
})

lua/hlchunk/utils/cache.lua

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
local class = require("hlchunk.utils.class")
2+
3+
---@class Cache
4+
---@field private cache table<number, table<string|number, any>>
5+
---@overload fun():Cache
6+
local Cache = class(function (self)
7+
self.cache = {}
8+
end)
9+
10+
---@param bufnr number
11+
---@param key string|number
12+
---@return any
13+
function Cache:get(bufnr, key)
14+
if not self.cache[bufnr] then
15+
return nil
16+
end
17+
return self.cache[bufnr][key]
18+
end
19+
20+
---@param bufnr number
21+
---@param key string|number
22+
---@param value any
23+
function Cache:set(bufnr, key, value)
24+
if not self.cache[bufnr] then
25+
self.cache[bufnr] = {}
26+
end
27+
self.cache[bufnr][key] = value
28+
end
29+
30+
---@param bufnr number
31+
---@param key string|number
32+
---@return boolean
33+
function Cache:has(bufnr, key)
34+
return self.cache[bufnr] and self.cache[bufnr][key]
35+
end
36+
37+
---@param bufnr number
38+
function Cache:clear(bufnr)
39+
self.cache[bufnr] = {}
40+
end
41+
42+
return Cache

0 commit comments

Comments
 (0)