Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

F/ordered autoload #126

Merged
merged 5 commits into from
Jun 4, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions lua/session_manager/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function session_manager.load_last_session(discard_current)
local last_session = utils.get_last_session_filename()
if last_session then
utils.load_session(last_session, discard_current)
return true
end
return false
end

--- Loads a session for the current working directory.
Expand All @@ -56,8 +58,10 @@ function session_manager.load_current_dir_session(discard_current)
local session = config.dir_to_session_filename(cwd)
if session:exists() then
utils.load_session(session.filename, discard_current)
return true
end
end
return false
end

--- Saves a session for the current working directory.
Expand All @@ -68,13 +72,31 @@ function session_manager.save_current_session()
end
end

local get_autoload_mode = function()
if config.autoload_mode == AutoloadMode.Disabled or
vim.fn.argc() > 0
or vim.g.started_with_stdin then
return { AutoloadMode.Disabled }
end
if config.autoload_mode == AutoloadMode.CurrentDir or
config.autoload_mode == AutoloadMode.LastSession then
-- Don't break existing configurations
return { config.autoload_mode }
end
return config.autoload_mode
end

local autoloaders = {
[AutoloadMode.Disabled] = function() return true end,
[AutoloadMode.CurrentDir] = session_manager.load_current_dir_session,
[AutoloadMode.LastSession] = session_manager.load_last_session,
}

--- Loads a session based on settings. Executed after starting the editor.
function session_manager.autoload_session()
if config.autoload_mode ~= AutoloadMode.Disabled and vim.fn.argc() == 0 and not vim.g.started_with_stdin then
Shatur marked this conversation as resolved.
Show resolved Hide resolved
if config.autoload_mode == AutoloadMode.CurrentDir then
session_manager.load_current_dir_session()
elseif config.autoload_mode == AutoloadMode.LastSession then
session_manager.load_last_session()
for _, mode in ipairs(get_autoload_mode()) do
if autoloaders[mode]() then
return
end
end
end
Expand Down