From a6c80d13c01cd3426174592d5fd5d885e8a1e79b Mon Sep 17 00:00:00 2001 From: Lukas Reineke Date: Sun, 10 Sep 2023 10:32:57 +0900 Subject: [PATCH] Better debounce logic fix #624 --- doc/indent_blankline.txt | 2 +- lua/ibl/autocmds.lua | 10 ++-------- lua/ibl/config.lua | 2 +- lua/ibl/init.lua | 28 +++++++++++++++++----------- 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/doc/indent_blankline.txt b/doc/indent_blankline.txt index 2f4c3a92..97948a52 100644 --- a/doc/indent_blankline.txt +++ b/doc/indent_blankline.txt @@ -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 diff --git a/lua/ibl/autocmds.lua b/lua/ibl/autocmds.lua index 1accee7d..91c6950d 100644 --- a/lua/ibl/autocmds.lua +++ b/lua/ibl/autocmds.lua @@ -12,6 +12,7 @@ M.setup = function() callback = ibl.refresh_all, }) vim.api.nvim_create_autocmd({ + "CursorMoved", "BufWinEnter", "CompleteChanged", "FileChangedShellPost", @@ -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, diff --git a/lua/ibl/config.lua b/lua/ibl/config.lua index 056a2231..8c5f654c 100644 --- a/lua/ibl/config.lua +++ b/lua/ibl/config.lua @@ -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, diff --git a/lua/ibl/init.lua b/lua/ibl/init.lua index 8888a7d0..f47e19de 100644 --- a/lua/ibl/init.lua +++ b/lua/ibl/init.lua @@ -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, })