refactor: move load session to utils

main
olimorris 2022-06-25 22:48:09 +01:00
parent 0c3c133a7f
commit 4e5b8b4a10
2 changed files with 24 additions and 10 deletions

View File

@ -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

View File

@ -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