Skip to content

Commit

Permalink
fix: restore correct tab on load
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc-stripe committed May 4, 2023
1 parent 8fe4571 commit 29ba485
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
3 changes: 2 additions & 1 deletion lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ local function save(name, opts, target_tabpage)
options = target_tabpage and {} or util.save_global_options(),
},
}
local current_win = vim.api.nvim_get_current_win()
local tabpage_bufs = {}
if target_tabpage then
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(target_tabpage)) do
Expand Down Expand Up @@ -201,7 +202,7 @@ local function save(name, opts, target_tabpage)
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)
tab.wins = layout.add_win_info_to_layout(tabnr, winlayout, current_win)
end
vim.api.nvim_set_current_tabpage(current_tabpage)

Expand Down
12 changes: 7 additions & 5 deletions lua/resession/layout.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ local M = {}
---@private
---@param tabnr integer
---@param winid integer
---@param current_win integer
---@return table|false
M.get_win_info = function(tabnr, winid)
M.get_win_info = function(tabnr, winid, current_win)
local bufnr = vim.api.nvim_win_get_buf(winid)
local win = {}
local supported_by_ext = false
Expand All @@ -33,7 +34,7 @@ M.get_win_info = function(tabnr, winid)
end
win = vim.tbl_extend("error", win, {
bufname = vim.api.nvim_buf_get_name(bufnr),
current = vim.api.nvim_get_current_win() == winid,
current = winid == current_win,
cursor = vim.api.nvim_win_get_cursor(winid),
width = vim.api.nvim_win_get_width(winid),
height = vim.api.nvim_win_get_height(winid),
Expand All @@ -48,18 +49,19 @@ end

---@param tabnr integer
---@param layout table
M.add_win_info_to_layout = function(tabnr, layout)
---@param current_win integer
M.add_win_info_to_layout = function(tabnr, layout, current_win)
local type = layout[1]
if type == "leaf" then
layout[2] = M.get_win_info(tabnr, layout[2])
layout[2] = M.get_win_info(tabnr, layout[2], current_win)
if not layout[2] then
return false
end
else
local last_slot = 1
local items = layout[2]
for _, v in ipairs(items) do
local ret = M.add_win_info_to_layout(tabnr, v)
local ret = M.add_win_info_to_layout(tabnr, v, current_win)
if ret then
items[last_slot] = ret
last_slot = last_slot + 1
Expand Down

0 comments on commit 29ba485

Please sign in to comment.