From c57ff6fdcd4d9ea9a109bad23aa856be0a75232c Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 21 Apr 2023 18:19:32 -0700 Subject: [PATCH] fix: save cmdheight per-tabpage (#7) --- lua/resession/init.lua | 11 +++++++---- lua/resession/util.lua | 22 ++++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lua/resession/init.lua b/lua/resession/init.lua index 94c4c11..0d99006 100644 --- a/lua/resession/init.lua +++ b/lua/resession/init.lua @@ -187,17 +187,21 @@ local function save(name, opts, target_tabpage) table.insert(data.buffers, buf) end end + local current_tabpage = vim.api.nvim_get_current_tabpage() local tabpages = target_tabpage and { target_tabpage } or vim.api.nvim_list_tabpages() for _, tabpage in ipairs(tabpages) do + vim.api.nvim_set_current_tabpage(tabpage) local tab = {} local tabnr = vim.api.nvim_tabpage_get_number(tabpage) if target_tabpage or vim.fn.haslocaldir(-1, tabnr) == 1 then tab.cwd = vim.fn.getcwd(-1, tabnr) end + tab.options = util.save_tab_options(tabpage) table.insert(data.tabs, tab) local winlayout = vim.fn.winlayout(tabnr) tab.wins = layout.add_win_info_to_layout(tabnr, winlayout) end + vim.api.nvim_set_current_tabpage(current_tabpage) for ext_name, ext_config in pairs(config.extensions) do local ext = util.get_extension(ext_name) @@ -461,6 +465,9 @@ M.load = function(name, opts) if win then curwin = win end + if tab.options then + util.restore_tab_options(tab.options) + end end -- This can be nil if we saved a session in a window with an unsupported buffer if curwin then @@ -482,10 +489,6 @@ M.load = function(name, opts) end end - -- We re-apply the options because sometimes the cmdheight gets messed up for some reason - for k, v in pairs(data.global.options) do - vim.o[k] = v - end current_session = nil if opts.reset then tab_sessions = {} diff --git a/lua/resession/util.lua b/lua/resession/util.lua index acec04b..b833343 100644 --- a/lua/resession/util.lua +++ b/lua/resession/util.lua @@ -50,6 +50,19 @@ M.save_buf_options = function(bufnr) return ret end +---@param bufnr integer +---@return table +M.save_tab_options = function(bufnr) + local ret = {} + -- 'cmdheight' is the only tab-local option, but the scope from nvim_get_option_info is incorrect + -- since there's no way to fetch a tabpage-local option, we rely on this being called from inside + -- the relevant tabpage + if vim.tbl_contains(config.options, "cmdheight") then + ret.cmdheight = vim.o.cmdheight + end + return ret +end + ---@param opts table M.restore_global_options = function(opts) for opt, val in pairs(opts) do @@ -82,6 +95,15 @@ M.restore_buf_options = function(bufnr, opts) end end +---@param opts table +M.restore_tab_options = function(opts) + -- 'cmdheight' is the only tab-local option. See save_tab_options + if opts.cmdheight then + -- empirically, this seems to only set the local tab value + vim.o.cmdheight = opts.cmdheight + end +end + ---@param dirname? string ---@return string M.get_session_dir = function(dirname)