Skip to content

Commit b552ee8

Browse files
authored
Fix autosave_only_in_session (#122)
1 parent 4376507 commit b552ee8

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

lua/session_manager/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function session_manager.autosave_session()
109109
return
110110
end
111111

112-
if config.autosave_only_in_session and not utils.is_session then
112+
if config.autosave_only_in_session and not utils.is_exist_in_session() then
113113
return
114114
end
115115

lua/session_manager/utils.lua

+14-11
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 = { is_session = false }
4+
local utils = {}
55

66
--- A small wrapper around `vim.notify` that adds plugin title.
77
---@param msg string
@@ -58,7 +58,6 @@ function utils.load_session(filename, discard_current)
5858

5959
local swapfile = vim.o.swapfile
6060
vim.o.swapfile = false
61-
utils.is_session = true
6261
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPre' })
6362
vim.api.nvim_command('silent source ' .. filename)
6463
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPost' })
@@ -84,20 +83,13 @@ function utils.save_session(filename)
8483
vim.api.nvim_command('%argdel')
8584
end
8685

87-
utils.is_session = true
8886
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' })
8987
vim.api.nvim_command('mksession! ' .. filename)
9088
vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePost' })
9189
end
9290

9391
---@param filename string
94-
function utils.delete_session(filename)
95-
Path:new(filename):rm()
96-
local cwd = vim.uv.cwd()
97-
if utils.is_session and cwd and filename == config.dir_to_session_filename(cwd).filename then
98-
utils.is_session = false
99-
end
100-
end
92+
function utils.delete_session(filename) Path:new(filename):rm() end
10193

10294
---@param opts table?: Additional arguments. Currently only `silent` is supported.
10395
---@return table
@@ -114,7 +106,7 @@ function utils.get_sessions(opts)
114106
table.sort(sessions, function(a, b) return a.timestamp > b.timestamp end)
115107

116108
-- If we are in a session already, don't list the current session.
117-
if utils.is_session then
109+
if utils.is_exist_in_session() then
118110
local cwd = vim.uv.cwd()
119111
local is_current_session = cwd and config.dir_to_session_filename(cwd).filename == sessions[1].filename
120112
if is_current_session then
@@ -171,6 +163,17 @@ function utils.is_restorable_buffer_present()
171163
return false
172164
end
173165

166+
---@return boolean
167+
function utils.is_exist_in_session()
168+
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
175+
end
176+
174177
---@return boolean
175178
function utils.is_dir_in_ignore_list()
176179
local cwd = vim.uv.cwd()

0 commit comments

Comments
 (0)