Skip to content

Commit

Permalink
Fix tried to close last window error
Browse files Browse the repository at this point in the history
Easiest repro is only have a new buffer (not saved to a file) and exit.
We'll  try to close that window and generate an error.

To fix, we wrap `Lib.close_unsupported_windows` in a `pcall`. In
general, not great to eat all errors but we're making our best effort to
close all of the unsupported windows and there are a bunch of edge cases
there so this seems like the most reasonable path to me.

Also added a missiing local
  • Loading branch information
cameronr committed Jun 24, 2024
1 parent e5ffe23 commit 518a775
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
7 changes: 4 additions & 3 deletions lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ end
-- This is useful for starter plugins which don't want to display 'restore session'
-- unless a session for the current working directory exists.
function AutoSession.session_exists_for_cwd()
session_file = get_session_file_name(vim.fn.getcwd())
local session_file = get_session_file_name(vim.fn.getcwd())
return Lib.is_readable(session_file)
end

Expand All @@ -396,7 +396,8 @@ function AutoSession.AutoSaveSession(sessions_dir)
end

if AutoSession.conf.close_unsupported_windows then
Lib.close_unsupported_windows()
-- Swallow errors as we may end up trying to close the last window
pcall(Lib.close_unsupported_windows)
end

AutoSession.SaveSession(sessions_dir, true)
Expand Down Expand Up @@ -907,7 +908,7 @@ function SetupAutocmds()
vim.api.nvim_create_user_command(
"SessionSave",
SaveSession,
{ bang = true, nargs = '?', desc = "Save the current session. Based in cwd if no arguments are passed" }
{ bang = true, nargs = "?", desc = "Save the current session. Based in cwd if no arguments are passed" }
)

vim.api.nvim_create_user_command("SessionRestore", SessionRestore, { bang = true, desc = "Restore Session" })
Expand Down
2 changes: 1 addition & 1 deletion lua/auto-session/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function Lib.close_unsupported_windows()
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;
break
end
end
end
Expand Down

0 comments on commit 518a775

Please sign in to comment.