Skip to content

Commit

Permalink
fix(layout): always do instant resize when views were added or deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jun 5, 2023
1 parent 9eededc commit 483861d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
1 change: 1 addition & 0 deletions lua/edgy/animate.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function M.schedule()
M.schedule()
else
M.restore_views(nil, true)
Util.debug("animation complete")
Config.animate.on_end()
end
end, 1000 / Config.animate.fps)
Expand Down
16 changes: 12 additions & 4 deletions lua/edgy/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ local function restore_state()
end
end

---@return boolean changed whether the layout changed
local function update()
---@type table<string, number[]>
local wins = {}
Expand All @@ -128,23 +129,30 @@ local function update()
table.insert(wins[ft], win)
end
end

local changed = false
-- Update the windows in each sidebar
M.foreach({ "bottom", "top", "left", "right" }, function(sidebar)
sidebar:update(wins)
if sidebar:update(wins) then
changed = true
end
end)
return changed
end

---@param opts? {full: boolean}
function M.layout(opts)
opts = opts or {}

update()
local changed = update()
local needs_layout = M.needs_layout()

if opts.full and not M.needs_layout() then
if opts.full and not (changed or needs_layout) then
return false
end

if opts.full then
if opts.full and needs_layout then
Util.debug("full layout")
M.foreach({ "bottom", "top", "left", "right" }, function(sidebar)
sidebar:layout()
end)
Expand Down
8 changes: 7 additions & 1 deletion lua/edgy/sidebar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ function M:on_hide(win)
end

---@param wins table<string, number[]>
---@return boolean updated if the sidebar was updated
function M:update(wins)
self.visible = 0
local updated = false
for _, view in ipairs(self.views) do
self.visible = self.visible + view:update(wins[view.ft] or {})
if view:update(wins[view.ft] or {}) then
updated = true
end
self.visible = self.visible + #view.wins
wins[view.ft] = vim.tbl_filter(function(w)
for _, win in ipairs(view.wins) do
if win.win == w then
Expand All @@ -131,6 +136,7 @@ function M:update(wins)
end, wins[view.ft] or {})
end
self:_update({ check = true })
return updated
end

---@param opts? {check: boolean}
Expand Down
10 changes: 10 additions & 0 deletions lua/edgy/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ function M.warn(msg)
M.notify(msg, vim.log.levels.WARN)
end

function M.info(msg)
M.notify(msg, vim.log.levels.INFO)
end

function M.debug(msg)
if require("edgy.config").debug then
M.info(msg)
end
end

---@generic F: fun()
---@param fn F
---@param max_retries? number
Expand Down
4 changes: 3 additions & 1 deletion lua/edgy/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,22 @@ function M.new(opts, sidebar)
end

---@param wins window[]
---@return boolean updated Whether the view was updated
function M:update(wins)
---@type table<window, Edgy.Window>
local index = {}
for _, w in ipairs(self.wins) do
index[w.win] = w
end
local old = self.wins
self.wins = {}
for _, win in ipairs(wins) do
local buf = vim.api.nvim_win_get_buf(win)
if not self.filter or self.filter(buf, win) then
self.wins[#self.wins + 1] = index[win] or Window.new(win, self)
end
end
return #self.wins
return not vim.deep_equal(old, self.wins)
end

---@param opts? {check: boolean}
Expand Down

0 comments on commit 483861d

Please sign in to comment.