Skip to content

Commit dd0f5eb

Browse files
svanharmelenShatur
authored andcommitted
Track active session and use it to determine exists_in_session
1 parent dc08d96 commit dd0f5eb

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

lua/session_manager/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ function session_manager.autosave_session()
149149
return
150150
end
151151

152-
if config.autosave_only_in_session and not utils.is_exist_in_session() then
152+
if config.autosave_only_in_session and not utils.exists_in_session() then
153153
return
154154
end
155155

lua/session_manager/utils.lua

+25-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
local config = require('session_manager.config')
22
local scandir = require('plenary.scandir')
33
local Path = require('plenary.path')
4-
local utils = {}
4+
local utils = { active_session_filename = nil }
55

66
--- A small wrapper around `vim.notify` that adds plugin title.
77
---@param msg string
@@ -56,6 +56,9 @@ function utils.load_session(filename, discard_current)
5656
end
5757
vim.api.nvim_buf_delete(current_buffer, { force = true })
5858

59+
-- Set the active session filename.
60+
utils.active_session_filename = filename
61+
5962
local swapfile = vim.o.swapfile
6063
vim.o.swapfile = false
6164
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPre' })
@@ -83,37 +86,41 @@ function utils.save_session(filename)
8386
vim.api.nvim_command('%argdel')
8487
end
8588

89+
-- Set the active session filename.
90+
utils.active_session_filename = filename
91+
8692
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' })
8793
vim.api.nvim_command('mksession! ' .. filename)
8894
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePost' })
8995
end
9096

9197
---@param filename string
92-
function utils.delete_session(filename) Path:new(filename):rm() end
98+
function utils.delete_session(filename)
99+
Path:new(filename):rm()
100+
101+
-- Clear the active session filename if deleted.
102+
if filename == utils.active_session_filename then
103+
utils.active_session_filename = nil
104+
end
105+
end
93106

94107
---@param opts table?: Additional arguments. Currently only `silent` is supported.
95108
---@return table
96109
function utils.get_sessions(opts)
97110
local sessions = {}
98111
for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir), opts)) do
99-
local dir = config.session_filename_to_dir(session_filename)
100-
if dir:is_dir() then
101-
table.insert(sessions, { timestamp = vim.fn.getftime(session_filename), filename = session_filename, dir = dir })
102-
else
103-
Path:new(session_filename):rm()
112+
-- Add all but the active session to the list.
113+
if session_filename ~= utils.active_session_filename then
114+
local dir = config.session_filename_to_dir(session_filename)
115+
if dir:is_dir() then
116+
table.insert(sessions, { timestamp = vim.fn.getftime(session_filename), filename = session_filename, dir = dir })
117+
else
118+
Path:new(session_filename):rm()
119+
end
104120
end
105121
end
106122
table.sort(sessions, function(a, b) return a.timestamp > b.timestamp end)
107123

108-
-- If we are in a session already, don't list the current session.
109-
if utils.is_exist_in_session() then
110-
local cwd = vim.uv.cwd()
111-
local is_current_session = cwd and config.dir_to_session_filename(cwd).filename == sessions[1].filename
112-
if is_current_session then
113-
table.remove(sessions, 1)
114-
end
115-
end
116-
117124
-- If no sessions to list, send a notification.
118125
if not (opts and opts.silent) and #sessions == 0 then
119126
vim.notify('The only available session is your current session. Nothing to select from.', vim.log.levels.INFO)
@@ -164,14 +171,9 @@ function utils.is_restorable_buffer_present()
164171
end
165172

166173
---@return boolean
167-
function utils.is_exist_in_session()
174+
function utils.exists_in_session()
168175
local cwd = vim.uv.cwd()
169-
for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir))) do
170-
if config.dir_to_session_filename(cwd).filename == session_filename then
171-
return true
172-
end
173-
end
174-
return false
176+
return config.dir_to_session_filename(cwd).filename == utils.active_session_filename
175177
end
176178

177179
---@return boolean

0 commit comments

Comments
 (0)