Skip to content

Commit

Permalink
Merge pull request rmagatti#392 from cameronr/main
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronr authored Nov 18, 2024
2 parents 542d338 + 40b65c1 commit 8d2eddb
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ use {
# 💡 Behaviour

1. When starting `nvim` with no arguments, AutoSession will try to restore an existing session for the current `cwd` if one exists.
2. When starting `nvim .` (or another directory), AutoSession will try to restore the session for that directory.
3. When starting `nvim some_file.txt` (or multiple files), by default, AutoSession won't do anything. See [argument handling](#argument-handling) for more details.
2. When starting `nvim .` (or another directory), AutoSession will try to restore the session for that directory. See [argument handling](#🗃%EF%B8%8F-argument-handling) for more details.
3. When starting `nvim some_file.txt` (or multiple files), by default, AutoSession won't do anything. See [argument handling](#🗃%EF%B8%8F-argument-handling) for more details.
4. Even after starting `nvim` with a file argument, a session can still be manually restored by running `:SessionRestore`.
5. Any session saving and restoration takes into consideration the current working directory `cwd`.
6. When piping to `nvim`, e.g: `cat myfile | nvim`, AutoSession won't do anything.
Expand Down Expand Up @@ -71,6 +71,7 @@ Here are the default settings:
args_allow_single_directory = true, -- Follow normal sesion save/load logic if launched with a single directory as the only argument
args_allow_files_auto_save = false, -- Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
continue_restore_on_error = true, -- Keep loading the session even if there's an error
show_auto_restore_notif = false, -- Whether to show a notification when auto-restoring
cwd_change_handling = false, -- Follow cwd changes, saving a session before change and restoring after
log_level = "error", -- Sets the log level of the plugin (debug, info, warn, error).

Expand Down Expand Up @@ -416,6 +417,8 @@ For `args_allow_single_directory`, if you frequently use `netrw` to look at dire
bypass_save_filetypes = { 'netrw' }
```

Also, if you use a plugin that handles directory arguments (e.g. file trees/explorers), it may prevent AutoSession from loading or saving sessions when launched with a directory argument. You can avoid that by lazy loading that plugin (e.g. [Oil](https://github.com/rmagatti/auto-session/issues/372#issuecomment-2471077783), [NvimTree](https://github.com/rmagatti/auto-session/issues/393#issuecomment-2474797271)).

If `args_allow_files_auto_save` is true, AutoSession won't load any session when `nvim` is launched with file argument(s) but it will save on exit. What's probably more useful is to set `args_allow_files_auto_save` to a function that returns true if a session should be saved and false otherwise. AutoSession will call that function on auto save when run with arguments. Here's one example config where it will save the session if at least two buffers are open after being launched with arguments:

```lua
Expand Down
1 change: 1 addition & 0 deletions doc/auto-session.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ AutoSession.Config *AutoSession.Config*
Argv Handling
{args_allow_files_auto_save?} (boolean|function) Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
{continue_restore_on_error?} (boolean) Keep loading the session even if there's an error. Set to false to get the line number of an error when loading a session
{show_auto_restore_notif?} (boolean) Whether to show a notification when auto-restoring
{log_level?} (string|integer) "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
{cwd_change_handling?} (boolean) Follow cwd changes, saving a session before change and restoring after
{session_lens?} (SessionLens) Session lens configuration options
Expand Down
24 changes: 14 additions & 10 deletions lua/auto-session/autocmds.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ local function handle_autosession_command(data)
local files = Lib.get_session_list(M.AutoSession.get_root_dir())
if data.args:match "search" then
open_picker(files, "Select a session:", function(choice)
M.AutoSession.autosave_and_restore(choice.session_name)
-- Defer session loading function to fix issue with Fzf and terminal sessions:
-- https://github.com/rmagatti/auto-session/issues/391
vim.defer_fn(function()
M.AutoSession.autosave_and_restore(choice.session_name)
end, 50)
end)
elseif data.args:match "delete" then
open_picker(files, "Delete a session:", function(choice)
Expand Down Expand Up @@ -183,14 +187,14 @@ local function setup_dirchanged_autocmds(AutoSession)
return
end

local success = AutoSession.AutoRestoreSession()

if not success then
Lib.logger.info("Could not load session for: " .. vim.fn.getcwd())
-- Don't return, still dispatch the hook below
end

AutoSession.run_cmds "post_cwd_changed"
-- If we're restoring a session with a terminal, we can get an
-- "Invalid argument: buftype=terminal" error when restoring the
-- session directly in this callback. To workaround, we schedule
-- the restore for the next run of the event loop
vim.schedule(function()
AutoSession.AutoRestoreSession()
AutoSession.run_cmds "post_cwd_changed"
end)
end,
pattern = "global",
})
Expand Down Expand Up @@ -258,7 +262,7 @@ function M.setup_autocmds(AutoSession)
return
end

handle_autosession_command { "search" }
handle_autosession_command { args = "search" }
end, {
desc = "Open a session picker",
})
Expand Down
2 changes: 2 additions & 0 deletions lua/auto-session/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local M = {}
---@field args_allow_single_directory? boolean Follow normal sesion save/load logic if launched with a single directory as the only argument
---@field args_allow_files_auto_save? boolean|function Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
---@field continue_restore_on_error? boolean Keep loading the session even if there's an error. Set to false to get the line number of an error when loading a session
---@field show_auto_restore_notif? boolean Whether to show a notification when auto-restoring
---@field log_level? string|integer "debug", "info", "warn", "error" or vim.log.levels.DEBUG, vim.log.levels.INFO, vim.log.levels.WARN, vim.log.levels.ERROR
---@field cwd_change_handling? boolean Follow cwd changes, saving a session before change and restoring after
---@field session_lens? SessionLens Session lens configuration options
Expand Down Expand Up @@ -76,6 +77,7 @@ local defaults = {
args_allow_single_directory = true, -- Follow normal sesion save/load logic if launched with a single directory as the only argument
args_allow_files_auto_save = false, -- Allow saving a session even when launched with a file argument (or multiple files/dirs). It does not load any existing session first. While you can just set this to true, you probably want to set it to a function that decides when to save a session when launched with file args. See documentation for more detail
continue_restore_on_error = true, -- Keep loading the session even if there's an error
show_auto_restore_notif = false, -- Whether to show a notification when auto-restoring
cwd_change_handling = false, -- Follow cwd changes, saving a session before change and restoring after
log_level = "error", -- Sets the log level of the plugin (debug, info, warn, error).

Expand Down
4 changes: 2 additions & 2 deletions lua/auto-session/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ function AutoSession.AutoRestoreSession(session_name)
return false
end

return AutoSession.RestoreSession(session_name, false)
return AutoSession.RestoreSession(session_name, Config.show_auto_restore_notif)
end

---@private
Expand Down Expand Up @@ -493,7 +493,7 @@ function AutoSession.auto_restore_session_at_vim_enter()
local last_session_name = Lib.get_latest_session(AutoSession.get_root_dir())
if last_session_name then
Lib.logger.debug("Found last session: " .. last_session_name)
if AutoSession.RestoreSession(last_session_name, false) then
if AutoSession.RestoreSession(last_session_name, Config.show_auto_restore_notif) then
return true
end
end
Expand Down
3 changes: 3 additions & 0 deletions tests/cwd_change_handling_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe("The cwd_change_handling config", function()
assert.equals(false, post_cwd_changed_hook_called)

vim.cmd "cd tests"
vim.wait(0)

assert.equals(0, vim.fn.bufexists(TL.test_file))
assert.equals(true, pre_cwd_changed_hook_called)
Expand All @@ -48,6 +49,7 @@ describe("The cwd_change_handling config", function()
assert.equals(0, vim.fn.bufexists(TL.test_file))

vim.cmd "cd .."
vim.wait(0)

assert.equals(vim.fn.getcwd(), require("auto-session.lib").current_session_name())

Expand All @@ -57,6 +59,7 @@ describe("The cwd_change_handling config", function()
it("does not double load a session when using SessionRestore", function()
-- Move to different directory
vim.cmd "cd tests"
vim.wait(0)

pre_cwd_changed_hook_called = false
post_cwd_changed_hook_called = false
Expand Down

0 comments on commit 8d2eddb

Please sign in to comment.