Skip to content

Commit

Permalink
Merge pull request #387 from cameronr/main
Browse files Browse the repository at this point in the history
feat: autosave manually named sessions,  #386, #389
  • Loading branch information
cameronr authored Oct 18, 2024
2 parents 9d02776 + 77e0923 commit 3cd531c
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 34 additions & 2 deletions lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -315,7 +323,7 @@ function AutoSession.AutoSaveSession()
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 +534,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 +601,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 { auto_create = false }

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 3cd531c

Please sign in to comment.