Skip to content

Commit ce10f64

Browse files
committed
fix: when comment chunk blink
1 parent 4a943d2 commit ce10f64

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lua/hlchunk/mods/chunk/init.lua

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,25 +194,30 @@ function ChunkMod:createAutocmd()
194194
self:notify("[hlchunk.chunk]: no parser for " .. vim.bo[bufnr].ft, nil, { once = true })
195195
end
196196
end
197-
local debounce_render_cb = debounce(render_cb, self.conf.delay)
198-
local debounce_render_cb_with_pre_hook = function(event, opts)
197+
local db_render_cb = debounce(render_cb, self.conf.delay, false)
198+
local db_render_cb_imm = debounce(render_cb, self.conf.delay, true)
199+
local db_render_cb_with_pre_hook = function(event, opts)
199200
opts = opts or { lazy = false }
200201
local bufnr = event.buf
201202
if not (api.nvim_buf_is_valid(bufnr) and self:shouldRender(bufnr)) then
202203
return
203204
end
204-
debounce_render_cb(event, opts)
205+
if opts.lazy then
206+
db_render_cb(event, opts)
207+
else
208+
db_render_cb_imm(event, opts)
209+
end
205210
end
206211
api.nvim_create_autocmd({ "CursorMovedI", "CursorMoved" }, {
207212
group = self.meta.augroup_name,
208213
callback = function(e)
209-
debounce_render_cb_with_pre_hook(e, { lazy = true })
214+
db_render_cb_with_pre_hook(e, { lazy = true })
210215
end,
211216
})
212217
api.nvim_create_autocmd({ "TextChangedI", "TextChanged" }, {
213218
group = self.meta.augroup_name,
214219
callback = function(e)
215-
debounce_render_cb_with_pre_hook(e, { lazy = false })
220+
db_render_cb_with_pre_hook(e, { lazy = false })
216221
end,
217222
})
218223
api.nvim_create_autocmd({ "UIEnter", "BufWinEnter" }, {

lua/hlchunk/utils/timer.lua

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,32 @@ function M.setInterval(fn, interval, ...)
3737
return timer
3838
end
3939

40+
---debounce funciton, assume we call a debounced func every 300ms for 3 times, and delay set to 1000ms
41+
---then will actually call 1 times, and timeline as follow:
42+
---0ms 300ms `600ms` call debounced func, 600ms will tigger the timer, other 2 will be ignored
43+
---and notice that the actually executed function's args will be the last call's args
4044
---@param fn function
4145
---@param delay integer The delay in milliseconds
46+
---@param first? boolean Whether to call the function immediately
4247
---@return function
43-
function M.debounce(fn, delay)
48+
function M.debounce(fn, delay, first)
4449
---@type uv_timer_t | nil
4550
local timer = nil
51+
local scheduled = false
52+
first = first or false
4653
return function(...)
54+
local args = { ... }
55+
if first and not scheduled then
56+
scheduled = true
57+
fn(...)
58+
end
4759
if timer then
4860
timer:stop()
4961
end
50-
timer = M.setTimeout(fn, delay, ...)
62+
timer = M.setTimeout(function()
63+
scheduled = false
64+
fn(unpack(args))
65+
end, delay)
5166
end
5267
end
5368

0 commit comments

Comments
 (0)