Skip to content

Commit

Permalink
Better debounce logic
Browse files Browse the repository at this point in the history
fix #624
  • Loading branch information
lukas-reineke committed Sep 10, 2023
1 parent 3484e85 commit a6c80d1
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion doc/indent_blankline.txt
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ config *ibl.config*
Sets the amount indent-blankline debounces
refreshes in milliseconds

Default: `50` ~
Default: `200` ~

• {viewport_buffer} (|ibl.config.viewport_buffer|)
Configures the viewport of where indentation guides
Expand Down
10 changes: 2 additions & 8 deletions lua/ibl/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ M.setup = function()
callback = ibl.refresh_all,
})
vim.api.nvim_create_autocmd({
"CursorMoved",
"BufWinEnter",
"CompleteChanged",
"FileChangedShellPost",
Expand All @@ -22,19 +23,12 @@ M.setup = function()
group = group,
pattern = "*",
callback = function(opts)
ibl.refresh(opts.buf)
ibl.debounced_refresh(opts.buf)
end,
})
vim.api.nvim_create_autocmd("OptionSet", {
group = group,
pattern = "list,listchars,shiftwidth,tabstop,vartabstop",
callback = function(opts)
ibl.refresh(opts.buf)
end,
})
vim.api.nvim_create_autocmd("CursorMoved", {
group = group,
pattern = "*",
callback = function(opts)
ibl.debounced_refresh(opts.buf)
end,
Expand Down
2 changes: 1 addition & 1 deletion lua/ibl/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ M.buffer_config = {}
---@type ibl.config.full
M.default_config = {
enabled = true,
debounce = 50,
debounce = 200,
viewport_buffer = {
min = 30,
max = 500,
Expand Down
28 changes: 17 additions & 11 deletions lua/ibl/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -110,22 +110,28 @@ end

local debounced_refresh = setmetatable({
timers = {},
queued_buffers = {},
}, {
---@param bufnr number
__call = function(self, bufnr)
bufnr = utils.get_bufnr(bufnr)
local config = conf.get_config(bufnr)
if not self.timers[bufnr] or self.timers[bufnr]:is_closing() then
if vim.uv then
self.timers[bufnr] = vim.uv.new_timer()
else
self.timers[bufnr] = vim.loop.new_timer()
end
local uv = vim.uv or vim.loop
if not self.timers[bufnr] then
self.timers[bufnr] = uv.new_timer()
end
if uv.timer_get_due_in(self.timers[bufnr]) <= 50 then
M.refresh(bufnr)

local config = conf.get_config(bufnr)
self.timers[bufnr]:start(config.debounce, 0, function()
if self.queued_buffers[bufnr] then
self.queued_buffers[bufnr] = nil
vim.schedule_wrap(M.refresh)(bufnr)
end
end)
else
self.queued_buffers[bufnr] = true
end
self.timers[bufnr]:start(config.debounce, 0, function()
vim.schedule_wrap(M.refresh)(bufnr)
self.timers[bufnr]:stop()
end)
end,
})

Expand Down

0 comments on commit a6c80d1

Please sign in to comment.