diff --git a/lua/auto-session/autocmds.lua b/lua/auto-session/autocmds.lua index b11a01f..eb5e955 100644 --- a/lua/auto-session/autocmds.lua +++ b/lua/auto-session/autocmds.lua @@ -152,6 +152,8 @@ local function setup_dirchanged_autocmds(AutoSession) AutoSession.AutoSaveSession() AutoSession.run_cmds "pre_cwd_changed" + -- clear vim.v.this_session so it doesn't leak into another directory + vim.v.this_session = "" end, pattern = "global", }) diff --git a/lua/auto-session/init.lua b/lua/auto-session/init.lua index b7402be..d8289ed 100644 --- a/lua/auto-session/init.lua +++ b/lua/auto-session/init.lua @@ -314,8 +314,16 @@ function AutoSession.AutoSaveSession() end end + local current_session = nil + + -- If there's an existing session, use that name + if vim.v.this_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 diff --git a/tests/allowed_dirs_spec.lua b/tests/allowed_dirs_spec.lua index 293239e..8ca9cc2 100644 --- a/tests/allowed_dirs_spec.lua +++ b/tests/allowed_dirs_spec.lua @@ -40,6 +40,9 @@ describe("The allowed dirs config", function() -- Change to a sub directory to see if it's allowed vim.cmd "cd tests/test_files" + -- clear out any existing session + vim.v.this_session = "" + local session_path = TL.makeSessionPath(vim.fn.getcwd()) assert.equals(0, vim.fn.filereadable(session_path)) @@ -57,6 +60,9 @@ describe("The allowed dirs config", function() vim.cmd("e " .. TL.test_file) c.allowed_dirs = { vim.fn.getcwd() .. "/tests/symlink-test" } + -- clear out any existing session + vim.v.this_session = "" + vim.fn.system "ln -snf test_files tests/symlink-test" vim.cmd "cd tests/symlink-test" diff --git a/tests/named_session_auto_save_spec.lua b/tests/named_session_auto_save_spec.lua new file mode 100644 index 0000000..66a6406 --- /dev/null +++ b/tests/named_session_auto_save_spec.lua @@ -0,0 +1,41 @@ +---@diagnostic disable: undefined-field +local TL = require "tests/test_lib" + +describe("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)