diff --git a/lua/auto-session/init.lua b/lua/auto-session/init.lua index b7402be..80a424d 100644 --- a/lua/auto-session/init.lua +++ b/lua/auto-session/init.lua @@ -314,8 +314,15 @@ function AutoSession.AutoSaveSession() end end + -- If there's a manually named session, use that on exit instead of one named for cwd + local current_session = nil + if AutoSession.manually_named_session then + current_session = Lib.escaped_session_name_to_session_name(vim.fn.fnamemodify(vim.v.this_session, ":t")) + Lib.logger.debug("Using existing session name: " .. current_session) + end + -- Don't try to show a message as we're exiting - return AutoSession.SaveSession(nil, false) + return AutoSession.SaveSession(current_session, false) end ---@private @@ -526,6 +533,18 @@ function AutoSession.SaveSessionToDir(session_dir, session_name, show_message) Lib.logger.debug("SaveSessionToDir escaped session name: " .. escaped_session_name) + -- If a session_name was passed in and it's different than the one for + -- the cwd, we know it's a manually named session. We track that so we + -- can write to that session on exit + if session_name then + local cwd_escaped_session_name = get_session_file_name(nil) + + if escaped_session_name ~= cwd_escaped_session_name then + AutoSession.manually_named_session = true + Lib.logger.debug "Session is manually named" + end + end + local session_path = session_dir .. escaped_session_name AutoSession.run_cmds "pre_save" @@ -581,6 +600,18 @@ function AutoSession.RestoreSessionFromDir(session_dir, session_name, show_messa local session_path = session_dir .. escaped_session_name + -- If a session_name was passed in and it's different than the one for + -- the cwd, we know it's a manually named session. We track that so we + -- can write to that session on exit + if session_name then + local cwd_escaped_session_name = get_session_file_name(nil) + + if escaped_session_name ~= cwd_escaped_session_name then + AutoSession.manually_named_session = true + Lib.logger.debug "Session is manually named" + end + end + if vim.fn.filereadable(session_path) ~= 1 then Lib.logger.debug("RestoreSessionFromDir session does not exist: " .. session_path) diff --git a/tests/manually_named_autosave_spec.lua b/tests/manually_named_autosave_spec.lua new file mode 100644 index 0000000..e925bcf --- /dev/null +++ b/tests/manually_named_autosave_spec.lua @@ -0,0 +1,41 @@ +---@diagnostic disable: undefined-field +local TL = require "tests/test_lib" + +describe("Manually named sessions", function() + require("auto-session").setup {} + + it("can autosave", function() + TL.clearSessionFilesAndBuffers() + vim.cmd("e " .. TL.test_file) + + require("auto-session").SaveSession(TL.named_session_name) + + vim.cmd("e " .. TL.other_file) + + require("auto-session").AutoSaveSession() + + -- Make sure the session was not created + assert.equals(0, vim.fn.filereadable(TL.default_session_path)) + assert.equals(1, vim.fn.filereadable(TL.named_session_path)) + TL.assertSessionHasFile(TL.named_session_path, TL.test_file) + TL.assertSessionHasFile(TL.named_session_path, TL.other_file) + end) + + it("autosaving doesn't break normal autosaving", function() + TL.clearSessionFilesAndBuffers() + vim.cmd("e " .. TL.test_file) + + require("auto-session").SaveSession() + + vim.cmd("e " .. TL.other_file) + assert.equals(1, vim.fn.bufexists(TL.other_file)) + + require("auto-session").AutoSaveSession() + + -- Make sure the session was not created + assert.equals(0, vim.fn.filereadable(TL.named_session_path)) + assert.equals(1, vim.fn.filereadable(TL.default_session_path)) + TL.assertSessionHasFile(TL.default_session_path, TL.test_file) + TL.assertSessionHasFile(TL.default_session_path, TL.other_file) + end) +end)