Skip to content

Commit

Permalink
Never try to close the last window in the last tab.
Browse files Browse the repository at this point in the history
If we do close a window in a tab, keep checking other windows in that
tab (that's why the `break` was removed).

Log an error (debug level) if `close_unsupported_windows` fails. Only
useful for debugging (not worth bothering the user about it)
  • Loading branch information
cameronr committed Jun 26, 2024
1 parent 518a775 commit ada2575
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
11 changes: 7 additions & 4 deletions lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ local luaOnlyConf = {
control_filename = "session_control.json", -- File name of the session control file
},
},
silent_restore = true
silent_restore = true,
}

-- Set default config on plugin load
Expand Down Expand Up @@ -396,8 +396,11 @@ function AutoSession.AutoSaveSession(sessions_dir)
end

if AutoSession.conf.close_unsupported_windows then
-- Swallow errors as we may end up trying to close the last window
pcall(Lib.close_unsupported_windows)
-- Wrap in pcall in case there's an error while trying to close windows
local success, result = pcall(Lib.close_unsupported_windows)
if not success then
Lib.logger.debug("Error closing unsupported windows: " .. result)
end
end

AutoSession.SaveSession(sessions_dir, true)
Expand Down Expand Up @@ -857,7 +860,7 @@ function AutoSession.PurgeOrphanedSessions()
local orphaned_sessions = {}

for _, session in ipairs(AutoSession.get_session_files()) do
if session.display_name:find('^/.*') and vim.fn.isdirectory(session.display_name) == Lib._VIM_FALSE then
if session.display_name:find "^/.*" and vim.fn.isdirectory(session.display_name) == Lib._VIM_FALSE then
table.insert(orphaned_sessions, session.display_name)
end
end
Expand Down
5 changes: 4 additions & 1 deletion lua/auto-session/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,14 @@ function Lib.close_unsupported_windows()
for _, tabpage in ipairs(tabpages) do
local windows = vim.api.nvim_tabpage_list_wins(tabpage)
for _, window in ipairs(windows) do
-- Never try to close the last window of the last tab
if vim.fn.tabpagenr "$" == 1 and vim.fn.winnr "$" == 1 then
return
end
local buffer = vim.api.nvim_win_get_buf(window)
local file_name = vim.api.nvim_buf_get_name(buffer)
if not Lib.is_readable(file_name) then
vim.api.nvim_win_close(window, true)
break
end
end
end
Expand Down

0 comments on commit ada2575

Please sign in to comment.