Skip to content

Commit

Permalink
perf: debounce auto refresh when diagnostics get updated
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 2, 2021
1 parent d7c915a commit 068476d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lua/trouble/init.lua
Expand Up @@ -65,9 +65,19 @@ function Trouble.help()
})
end

local updater = util.debounce(100, function()
util.debug("refresh: auto")
view:update({auto = true})
end)

function Trouble.refresh(opts)
if is_open() then
view:update(opts)
if opts and opts.auto then
updater()
else
util.debug("refresh")
view:update(opts)
end
elseif opts.auto and config.options.auto_open then
local count = util.count(lsp.diagnostics())
if count > 0 then Trouble.open(opts) end
Expand Down
12 changes: 12 additions & 0 deletions lua/trouble/util.lua
Expand Up @@ -16,4 +16,16 @@ end
function M.warn(msg) M.log(msg, "WarningMsg") end

function M.debug(msg) if config.options.debug then M.log(msg) end end

function M.debounce(ms, fn)
local timer = vim.loop.new_timer()
return function(...)
local argv = {...}
timer:start(ms, 0, function()
timer:stop()
vim.schedule_wrap(fn)(unpack(argv))
end)
end
end

return M

0 comments on commit 068476d

Please sign in to comment.