diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index a44612f..9de1123 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -100,13 +100,7 @@ function M.load(opt) local session = opt.last and get_last() or get_current() if session and vim.fn.filereadable(session) ~= 0 then - vim.schedule(function() - local ok, result = pcall(vim.cmd, "source " .. e(session)) - if not ok then - return utils.echoerr("Error loading the session! ", result) - end - config.options.after_source() - end) + utils.load_session(session, _, config.options.after_source()) end if config.options.autosave and (allow_dir() and not ignore_dir()) then diff --git a/lua/persisted/utils.lua b/lua/persisted/utils.lua index 6eb2482..f130686 100644 --- a/lua/persisted/utils.lua +++ b/lua/persisted/utils.lua @@ -30,9 +30,10 @@ end ---@param dir_table table ---@return boolean function M.dirs_match(dir, dirs_table) - local dir = vim.fn.expand(dir) - return dirs_table and next(vim.tbl_filter(function(pattern) - return dir:match(vim.fn.expand(pattern)) + local dir = vim.fn.expand(dir) + return dirs_table + and next(vim.tbl_filter(function(pattern) + return dir:match(vim.fn.expand(pattern)) end, dirs_table)) end @@ -58,4 +59,23 @@ function M.echoerr(msg, error) }, true, {}) end +---Load the given session +---@param session table +---@param before_callback function +---@param after_callback function +function M.load_session(session, before_callback, after_callback) + vim.schedule(function() + if type(before_callback) == "function" then + before_callback() + end + local ok, result = pcall(vim.cmd, "source " .. vim.fn.fnameescape(session)) + if not ok then + return M.echoerr("Error loading the session! ", result) + end + if type(after_callback) == "function" then + after_callback() + end + end) +end + return M