Skip to content

Commit

Permalink
feat: autosave manually named sessions, fixes rmagatti#386
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cameronr committed Oct 9, 2024
1 parent 9d02776 commit 753ac11
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
33 changes: 32 additions & 1 deletion lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
41 changes: 41 additions & 0 deletions tests/manually_named_autosave_spec.lua
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit 753ac11

Please sign in to comment.