Skip to content

Commit

Permalink
feat(make_buflist): allow passing reference to custom cache table
Browse files Browse the repository at this point in the history
  • Loading branch information
rebelot committed Sep 6, 2022
1 parent 440da6b commit d340f95
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lua/heirline/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,20 @@ function M.pick_child_on_condition(component)
end
end

local buflist_cache = {}
local with_cache_release_au

local function with_cache(func, cache)
if not with_cache_release_au then
with_cache_release_au = vim.api.nvim_create_autocmd({ "BufNew", "BufDelete" }, {
cache = cache or {}
if not cache.au_id then
cache.au_id = vim.api.nvim_create_autocmd({ "BufAdd", "BufDelete" }, {
callback = function()
for i, _ in ipairs(cache) do
for i = 1, #cache do
cache[i] = nil
end
end,
desc = "Heirline: cache for buflist get_bufs()",
desc = "Heirline: release cache for buflist get_bufs()",
})
end
return function()
if cache and next(cache) ~= nil then
if cache and cache[1] ~= nil then
return cache
else
local res = func()
Expand Down Expand Up @@ -380,9 +378,9 @@ local NTABLINES = 0
---@param right_trunc? table right truncation marker, shown is buffer list is too long
---@param buf_func? function return a list of <integer> bufnr handlers.
---@return table
function M.make_buflist(buffer_component, left_trunc, right_trunc, buf_func)
function M.make_buflist(buffer_component, left_trunc, right_trunc, buf_func, buf_cache)
buf_func = buf_func or get_bufs
buf_func = with_cache(buf_func, buflist_cache)
buf_func = with_cache(buf_func, buf_cache)

left_trunc = left_trunc or {
provider = "<",
Expand Down Expand Up @@ -442,7 +440,11 @@ function M.make_buflist(buffer_component, left_trunc, right_trunc, buf_func)

self.active_child = false
local bufs = buf_func()
bufs = vim.tbl_filter(function(bufnr)
return vim.api.nvim_buf_is_valid(bufnr)
end, bufs)
local visible_buffers = bufs_in_tab()

for i, bufnr in ipairs(bufs) do
local child = self[i]
if not (child and child.bufnr == bufnr) then
Expand All @@ -465,7 +467,7 @@ function M.make_buflist(buffer_component, left_trunc, right_trunc, buf_func)
end
end
if #self > #bufs then
for i = #self, #bufs + 1, -1 do
for i = #bufs + 1, #self do
self[i] = nil
end
end
Expand Down

0 comments on commit d340f95

Please sign in to comment.