Skip to content

Commit

Permalink
perf: much faster async preview
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed May 10, 2021
1 parent 0c9ca5e commit 2c9b319
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
17 changes: 17 additions & 0 deletions lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ function M.debounce(ms, fn)
end
end

function M.throttle(ms, fn)
local timer = vim.loop.new_timer()
local running = false
return function(...)
if not running then
local argv = {...}
local argc = select('#', ...)

timer:start(ms, 0, function()
running = false
pcall(vim.schedule_wrap(fn), unpack(argv, 1, argc))
end)
running = true
end
end
end

M.severity = {
[0] = "Other",
[1] = "Error",
Expand Down
6 changes: 5 additions & 1 deletion lua/trouble/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function View:toggle_fold()
self:update()
end

function View:preview()
function View:_preview()
if not vim.api.nvim_win_is_valid(self.parent) then return end
util.debug("preview")

Expand Down Expand Up @@ -421,4 +421,8 @@ function View:preview()
end
end

-- View.preview = View._preview

View.preview = util.throttle(50, View._preview)

return View

0 comments on commit 2c9b319

Please sign in to comment.