From 753ac113763131ad37e84657a56e41830476ed1e Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Wed, 9 Oct 2024 14:02:36 -0700 Subject: [PATCH 1/4] feat: autosave manually named sessions, fixes #386 If you're using a manually named session (either through ':SessionSave mysession', `:SessionSearch`, or `:SessionRestore mysession`) then we'll now auto-save to that session on exit instead of always autosaving to the session derived from the cwd. --- lua/auto-session/init.lua | 33 ++++++++++++++++++++- tests/manually_named_autosave_spec.lua | 41 ++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 tests/manually_named_autosave_spec.lua 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) From 5a9d07b307322c7febd54a1b1f2af45bc502f73a Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Wed, 9 Oct 2024 23:12:54 -0700 Subject: [PATCH 2/4] docs(README): explain manually named autosaving --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index d39da6c..4a31678 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,8 @@ AutoSession exposes the following commands that can be used or mapped to any key :Autosession delete " open a vim.ui.select picker to choose a session to delete. ``` +If you create a manually named session via `SessionSave my_session` or you restore one, that same session will be auto-saved (assuming that's enabled) when you exit. + # 📖 More Configuration Details ## 🔭 Session Lens From 59f397468593c3f2bf7720bec874d1daf9998edd Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Mon, 14 Oct 2024 13:44:58 -0700 Subject: [PATCH 3/4] fix: #386 use right session name in auto create When auto-saving, if the session has been manually named use that session name instead of the cwd session name. Fixes manually named session not auto-saving on exit. --- lua/auto-session/init.lua | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lua/auto-session/init.lua b/lua/auto-session/init.lua index 80a424d..d959196 100644 --- a/lua/auto-session/init.lua +++ b/lua/auto-session/init.lua @@ -298,8 +298,16 @@ function AutoSession.AutoSaveSession() return false 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 + if not is_auto_create_enabled() then - local session_file_name = get_session_file_name() + local session_file_name = get_session_file_name(current_session) if vim.fn.filereadable(AutoSession.get_root_dir() .. session_file_name) == 0 then Lib.logger.debug "Create not enabled and no existing session, not creating session" return false @@ -314,13 +322,6 @@ 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(current_session, false) end From 77e0923b2e98c72d744d781b921fba25510207c6 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Mon, 14 Oct 2024 13:56:56 -0700 Subject: [PATCH 4/4] tests(manually_named_autosave): auto_create=false Turn off auto_create to make sure correct session name is checked when deciding to allow session saving or not --- tests/manually_named_autosave_spec.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/manually_named_autosave_spec.lua b/tests/manually_named_autosave_spec.lua index e925bcf..aeb183c 100644 --- a/tests/manually_named_autosave_spec.lua +++ b/tests/manually_named_autosave_spec.lua @@ -2,7 +2,7 @@ local TL = require "tests/test_lib" describe("Manually named sessions", function() - require("auto-session").setup {} + require("auto-session").setup { auto_create = false } it("can autosave", function() TL.clearSessionFilesAndBuffers()