From 4ea216b202f8fb30e39e2ea2ab5ec84bf586cd1e Mon Sep 17 00:00:00 2001 From: Will Hopkins Date: Mon, 2 Oct 2023 21:59:10 -0700 Subject: [PATCH] fix: save and restore fixed win dimensions on resize --- lua/focus/modules/resizer.lua | 38 ++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/lua/focus/modules/resizer.lua b/lua/focus/modules/resizer.lua index b0cbe9e..b63d6c1 100644 --- a/lua/focus/modules/resizer.lua +++ b/lua/focus/modules/resizer.lua @@ -21,6 +21,31 @@ local golden_ratio_minheight = function() return math.floor(golden_ratio_height() / (3 * golden_ratio)) end +local function save_fixed_win_dims() + local fixed_dims = {} + + for _, win in ipairs(vim.api.nvim_list_wins()) do + local buf = vim.api.nvim_win_get_buf(win) + if vim.w[win].focus_disable or vim.b[buf].focus_disable then + fixed_dims[win] = { + width = vim.api.nvim_win_get_width(win), + height = vim.api.nvim_win_get_height(win), + } + end + end + + return fixed_dims +end + +local function restore_fixed_win_dims(fixed_dims) + for win, dims in pairs(fixed_dims) do + if vim.api.nvim_win_is_valid(win) then + vim.api.nvim_win_set_width(win, dims.width) + vim.api.nvim_win_set_height(win, dims.height) + end + end +end + function M.autoresize(config) local width if config.autoresize.width > 0 then @@ -49,9 +74,13 @@ function M.autoresize(config) -- save cmdheight to ensure it is not changed by nvim_win_set_height local cmdheight = vim.o.cmdheight + local fixed = save_fixed_win_dims() + vim.api.nvim_win_set_width(0, width) vim.api.nvim_win_set_height(0, height) + restore_fixed_win_dims(fixed) + vim.o.cmdheight = cmdheight end @@ -62,9 +91,12 @@ end function M.maximise() local width, height = vim.o.columns, vim.o.lines - local win = vim.api.nvim_get_current_win() - vim.api.nvim_win_set_width(win, width) - vim.api.nvim_win_set_height(win, height) + local fixed = save_fixed_win_dims() + + vim.api.nvim_win_set_width(0, width) + vim.api.nvim_win_set_height(0, height) + + restore_fixed_win_dims(fixed) end M.goal = 'autoresize'