diff --git a/lua/session_manager/init.lua b/lua/session_manager/init.lua index 837de22..481b01b 100644 --- a/lua/session_manager/init.lua +++ b/lua/session_manager/init.lua @@ -109,7 +109,7 @@ function session_manager.autosave_session() return end - if config.autosave_only_in_session and not utils.is_session then + if config.autosave_only_in_session and not utils.is_exist_in_session() then return end diff --git a/lua/session_manager/utils.lua b/lua/session_manager/utils.lua index 447405e..9f857d7 100644 --- a/lua/session_manager/utils.lua +++ b/lua/session_manager/utils.lua @@ -1,7 +1,7 @@ local config = require('session_manager.config') local scandir = require('plenary.scandir') local Path = require('plenary.path') -local utils = { is_session = false } +local utils = {} --- A small wrapper around `vim.notify` that adds plugin title. ---@param msg string @@ -58,7 +58,6 @@ function utils.load_session(filename, discard_current) local swapfile = vim.o.swapfile vim.o.swapfile = false - utils.is_session = true vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPre' }) vim.api.nvim_command('silent source ' .. filename) vim.api.nvim_exec_autocmds('User', { pattern = 'SessionLoadPost' }) @@ -84,20 +83,13 @@ function utils.save_session(filename) vim.api.nvim_command('%argdel') end - utils.is_session = true vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePre' }) vim.api.nvim_command('mksession! ' .. filename) vim.api.nvim_exec_autocmds('User', { pattern = 'SessionSavePost' }) end ---@param filename string -function utils.delete_session(filename) - Path:new(filename):rm() - local cwd = vim.uv.cwd() - if utils.is_session and cwd and filename == config.dir_to_session_filename(cwd).filename then - utils.is_session = false - end -end +function utils.delete_session(filename) Path:new(filename):rm() end ---@param opts table?: Additional arguments. Currently only `silent` is supported. ---@return table @@ -114,7 +106,7 @@ function utils.get_sessions(opts) table.sort(sessions, function(a, b) return a.timestamp > b.timestamp end) -- If we are in a session already, don't list the current session. - if utils.is_session then + if utils.is_exist_in_session() then local cwd = vim.uv.cwd() local is_current_session = cwd and config.dir_to_session_filename(cwd).filename == sessions[1].filename if is_current_session then @@ -171,6 +163,17 @@ function utils.is_restorable_buffer_present() return false end +---@return boolean +function utils.is_exist_in_session() + local cwd = vim.uv.cwd() + for _, session_filename in ipairs(scandir.scan_dir(tostring(config.sessions_dir))) do + if config.dir_to_session_filename(cwd).filename == session_filename then + return true + end + end + return false +end + ---@return boolean function utils.is_dir_in_ignore_list() local cwd = vim.uv.cwd()