Skip to content

Commit

Permalink
fix: save cmdheight per-tabpage (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc-stripe committed Apr 22, 2023
1 parent 6bda7fd commit c57ff6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 = {}
Expand Down
22 changes: 22 additions & 0 deletions lua/resession/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ M.save_buf_options = function(bufnr)
return ret
end

---@param bufnr integer
---@return table<string, any>
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<string, any>
M.restore_global_options = function(opts)
for opt, val in pairs(opts) do
Expand Down Expand Up @@ -82,6 +95,15 @@ M.restore_buf_options = function(bufnr, opts)
end
end

---@param opts table<string, any>
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)
Expand Down

0 comments on commit c57ff6f

Please sign in to comment.