Skip to content

Commit

Permalink
feat: debounce results from code actions
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jul 16, 2021
1 parent 26fd12a commit 310d49c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
29 changes: 29 additions & 0 deletions lua/null-ls/handlers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
local utils = require("null-ls.utils")

local M = {}

function M.setup()
M.combine("textDocument/codeAction")
end

-- this will override a handler, batch results and debounce them
function M.combine(method, ms)
ms = ms or 100
local orig = vim.lsp.handlers[method]

local results = {}

local handler = utils.debounce(ms, function()
if #results > 0 then
orig(nil, nil, results)
results = {}
end
end)

vim.lsp.handlers[method] = function(_, _, actions)
vim.list_extend(results, actions or {})
handler()
end
end

return M
1 change: 1 addition & 0 deletions lua/null-ls/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ M.config = function(user_config)
config.setup(user_config or {})
require("null-ls.rpc").setup()
require("null-ls.lspconfig").setup()
require("null-ls.handlers").setup()
end

M.setup = function(user_config)
Expand Down
28 changes: 28 additions & 0 deletions lua/null-ls/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,32 @@ M.table = {
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

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

return M

0 comments on commit 310d49c

Please sign in to comment.