Skip to content

Commit

Permalink
feat(autcmd_update): improve cache handling; this improves coordinati…
Browse files Browse the repository at this point in the history
…on between updatable_components and components which require special post-processing
  • Loading branch information
rebelot committed Aug 30, 2022
1 parent a8f7b1f commit 08b7998
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions lua/heirline/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ end
local function _eval(statusline, winnr, full_width)
statusline.winnr = winnr
statusline._flexible_components = {}
statusline._updatable_components = {}
statusline._buflist = {}
local out = statusline:eval()
local buflist = statusline._buflist[1]
Expand All @@ -80,6 +81,8 @@ local function _eval(statusline, winnr, full_width)
-- now the buflist is paged, and flexible components still have the same value, however, there might be more space now, depending on the page
utils.expand_or_contract_flexible_components(statusline._flexible_components, full_width, out) -- flexible components are re-adapting to paginated buflist
end

statusline:_freeze_cache()
return statusline:traverse()
end

Expand Down
17 changes: 15 additions & 2 deletions lua/heirline/statusline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ local default_restrict = {
---@field _win_cache? table
---@field _au_id? integer
---@field _tree table
---@field _updatable_components table
---@field _flexible_components table
---@field pick_child? table<integer>
local StatusLine = {
Expand Down Expand Up @@ -245,7 +246,7 @@ function StatusLine:_eval()

local win_cache = self:get_win_attr("_win_cache")
if win_cache then
table.insert(self._tree, win_cache)
self._tree[1] = win_cache
return
end
end
Expand Down Expand Up @@ -308,7 +309,8 @@ function StatusLine:_eval()
end

if self.update then
self:set_win_attr("_win_cache", self:traverse())
self:set_win_attr("_win_cache", self._tree)
table.insert(self._updatable_components, self)
end
end

Expand Down Expand Up @@ -340,6 +342,17 @@ function StatusLine:clear_tree()
end
end

-- this MUST be called at the end of the main loop
function StatusLine:_freeze_cache()
for _, component in ipairs(self._updatable_components) do
local win_cache = component:get_win_attr("_win_cache") -- check nil?
local fixed_cache = component:traverse(win_cache)
component:set_win_attr("_win_cache", fixed_cache)
component:clear_tree()
component._tree[1] = fixed_cache
end
end

function StatusLine:eval()
self:_eval()
return self:traverse()
Expand Down

0 comments on commit 08b7998

Please sign in to comment.