Skip to content

Commit

Permalink
feat(util): make throttles configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Mar 28, 2024
1 parent 9f43620 commit 4800f83
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
18 changes: 13 additions & 5 deletions lua/trouble/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ local M = {}
---@field sorters? table<string, trouble.SorterFn> custom sorters
local defaults = {
debug = false,
throttle = 100,
pinned = false, -- When pinned, the opened trouble window will be bound to the current buffer
focus = true, -- Focus the window when opened. Defaults to true.
results = {
---@type trouble.Window.opts
win = {},
win = {}, -- window options for the results window. Can be a split or a floating window.
indent_guides = true, -- show indent guides
multiline = true, -- render multi-line messages
max_items = 200, -- limit number of items that can be displayed per section
Expand All @@ -27,13 +26,22 @@ local defaults = {
auto_refresh = true, -- auto refresh when open
},
preview = {
-- preview window, to show the preview in
-- the main editor window.
-- Set type to `main` to show the preview in the main editor window.
-- Window options for the preview window. Can be a split, floating window,
-- or `main` to show the preview in the main editor window.
---@type trouble.Window.opts
win = { type = "main" },
auto_open = true, -- automatically open preview when on an item
},
-- Throttle/Debounce settings. Should usually not be changed.
---@type table<string, number|{ms:number, debounce?:boolean}>
throttle = {
refresh = 20, -- fetches new data when needed
update = 10, -- updates the window
render = 10, -- renders the window
preview = { ms = 100, debounce = true }, -- shows the preview for the current item
},
-- Key mappings can be set to the name of a builtin action,
-- or you can define your own custom action.
---@type table<string, string|trouble.Action>
keys = {
["?"] = "help",
Expand Down
15 changes: 14 additions & 1 deletion lua/trouble/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,23 @@ function M.camel(str, sep)
)
end

---@alias ThrottleOpts {ms:number, debounce?:boolean, is_running?:fun():boolean}

---@param opts? {ms?: number, debounce?: boolean}|number
---@param default ThrottleOpts
---@return ThrottleOpts
function M.throttle_opts(opts, default)
opts = opts or {}
if type(opts) == "number" then
opts = { ms = opts }
end
return vim.tbl_deep_extend("force", default, opts)
end

-- throttle with trailing execution
---@generic T: fun()
---@param fn T
---@param opts? {ms:number, debounce?:boolean, is_running?:fun():boolean}
---@param opts? ThrottleOpts
---@return T
function M.throttle(fn, opts)
opts = opts or {}
Expand Down
7 changes: 4 additions & 3 deletions lua/trouble/view/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ function M.new(opts)
padding = vim.tbl_get(self.opts.results.win, "padding", "left") or 0,
multiline = self.opts.results.multiline,
})
self.update = Util.throttle(M.update, { ms = 10 })
self.render = Util.throttle(M.render, { ms = 10 })
self.update = Util.throttle(M.update, Util.throttle_opts(self.opts.throttle.update, { ms = 10 }))
self.render = Util.throttle(M.render, Util.throttle_opts(self.opts.throttle.render, { ms = 10 }))

if self.opts.results.auto_open then
self:listen()
Expand Down Expand Up @@ -94,7 +94,8 @@ function M:on_mount()

local _self = Util.weak(self)

local preview = Util.throttle(M.preview, { ms = 100, debounce = true })
local preview =
Util.throttle(M.preview, Util.throttle_opts(self.opts.throttle.preview, { ms = 100, debounce = true }))
self.win:on("CursorMoved", function()
local this = _self()
if not this then
Expand Down
18 changes: 11 additions & 7 deletions lua/trouble/view/section.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,17 @@ function M.new(section, opts)
self:main()

local _self = Util.weak(self)
self.refresh = Util.throttle(M.refresh, {
ms = 20,
is_running = function()
local this = _self()
return this and this.fetching
end,
})

self.refresh = Util.throttle(
M.refresh,
Util.throttle_opts(opts.throttle.refresh, {
ms = 20,
is_running = function()
local this = _self()
return this and this.fetching
end,
})
)

return self
end
Expand Down

0 comments on commit 4800f83

Please sign in to comment.