Skip to content

Commit 8d2eddb

Browse files
authored
Merge pull request #392 from cameronr/main
Fix #391, #372, #394, and some more
2 parents 542d338 + 40b65c1 commit 8d2eddb

File tree

6 files changed

+27
-14
lines changed

6 files changed

+27
-14
lines changed

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ use {
4141
# 💡 Behaviour
4242

4343
1. When starting `nvim` with no arguments, AutoSession will try to restore an existing session for the current `cwd` if one exists.
44-
2. When starting `nvim .` (or another directory), AutoSession will try to restore the session for that directory.
45-
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.
44+
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.
45+
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.
4646
4. Even after starting `nvim` with a file argument, a session can still be manually restored by running `:SessionRestore`.
4747
5. Any session saving and restoration takes into consideration the current working directory `cwd`.
4848
6. When piping to `nvim`, e.g: `cat myfile | nvim`, AutoSession won't do anything.
@@ -71,6 +71,7 @@ Here are the default settings:
7171
args_allow_single_directory = true, -- Follow normal sesion save/load logic if launched with a single directory as the only argument
7272
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
7373
continue_restore_on_error = true, -- Keep loading the session even if there's an error
74+
show_auto_restore_notif = false, -- Whether to show a notification when auto-restoring
7475
cwd_change_handling = false, -- Follow cwd changes, saving a session before change and restoring after
7576
log_level = "error", -- Sets the log level of the plugin (debug, info, warn, error).
7677

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

420+
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)).
421+
419422
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:
420423

421424
```lua

doc/auto-session.txt

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ AutoSession.Config *AutoSession.Config*
2727
Argv Handling
2828
{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
2929
{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
30+
{show_auto_restore_notif?} (boolean) Whether to show a notification when auto-restoring
3031
{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
3132
{cwd_change_handling?} (boolean) Follow cwd changes, saving a session before change and restoring after
3233
{session_lens?} (SessionLens) Session lens configuration options

lua/auto-session/autocmds.lua

+14-10
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,11 @@ local function handle_autosession_command(data)
5959
local files = Lib.get_session_list(M.AutoSession.get_root_dir())
6060
if data.args:match "search" then
6161
open_picker(files, "Select a session:", function(choice)
62-
M.AutoSession.autosave_and_restore(choice.session_name)
62+
-- Defer session loading function to fix issue with Fzf and terminal sessions:
63+
-- https://github.com/rmagatti/auto-session/issues/391
64+
vim.defer_fn(function()
65+
M.AutoSession.autosave_and_restore(choice.session_name)
66+
end, 50)
6367
end)
6468
elseif data.args:match "delete" then
6569
open_picker(files, "Delete a session:", function(choice)
@@ -183,14 +187,14 @@ local function setup_dirchanged_autocmds(AutoSession)
183187
return
184188
end
185189

186-
local success = AutoSession.AutoRestoreSession()
187-
188-
if not success then
189-
Lib.logger.info("Could not load session for: " .. vim.fn.getcwd())
190-
-- Don't return, still dispatch the hook below
191-
end
192-
193-
AutoSession.run_cmds "post_cwd_changed"
190+
-- If we're restoring a session with a terminal, we can get an
191+
-- "Invalid argument: buftype=terminal" error when restoring the
192+
-- session directly in this callback. To workaround, we schedule
193+
-- the restore for the next run of the event loop
194+
vim.schedule(function()
195+
AutoSession.AutoRestoreSession()
196+
AutoSession.run_cmds "post_cwd_changed"
197+
end)
194198
end,
195199
pattern = "global",
196200
})
@@ -258,7 +262,7 @@ function M.setup_autocmds(AutoSession)
258262
return
259263
end
260264

261-
handle_autosession_command { "search" }
265+
handle_autosession_command { args = "search" }
262266
end, {
263267
desc = "Open a session picker",
264268
})

lua/auto-session/config.lua

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ local M = {}
2323
---@field args_allow_single_directory? boolean Follow normal sesion save/load logic if launched with a single directory as the only argument
2424
---@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
2525
---@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
26+
---@field show_auto_restore_notif? boolean Whether to show a notification when auto-restoring
2627
---@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
2728
---@field cwd_change_handling? boolean Follow cwd changes, saving a session before change and restoring after
2829
---@field session_lens? SessionLens Session lens configuration options
@@ -76,6 +77,7 @@ local defaults = {
7677
args_allow_single_directory = true, -- Follow normal sesion save/load logic if launched with a single directory as the only argument
7778
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
7879
continue_restore_on_error = true, -- Keep loading the session even if there's an error
80+
show_auto_restore_notif = false, -- Whether to show a notification when auto-restoring
7981
cwd_change_handling = false, -- Follow cwd changes, saving a session before change and restoring after
8082
log_level = "error", -- Sets the log level of the plugin (debug, info, warn, error).
8183

lua/auto-session/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ function AutoSession.AutoRestoreSession(session_name)
445445
return false
446446
end
447447

448-
return AutoSession.RestoreSession(session_name, false)
448+
return AutoSession.RestoreSession(session_name, Config.show_auto_restore_notif)
449449
end
450450

451451
---@private
@@ -493,7 +493,7 @@ function AutoSession.auto_restore_session_at_vim_enter()
493493
local last_session_name = Lib.get_latest_session(AutoSession.get_root_dir())
494494
if last_session_name then
495495
Lib.logger.debug("Found last session: " .. last_session_name)
496-
if AutoSession.RestoreSession(last_session_name, false) then
496+
if AutoSession.RestoreSession(last_session_name, Config.show_auto_restore_notif) then
497497
return true
498498
end
499499
end

tests/cwd_change_handling_spec.lua

+3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ describe("The cwd_change_handling config", function()
3838
assert.equals(false, post_cwd_changed_hook_called)
3939

4040
vim.cmd "cd tests"
41+
vim.wait(0)
4142

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

5051
vim.cmd "cd .."
52+
vim.wait(0)
5153

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

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

6164
pre_cwd_changed_hook_called = false
6265
post_cwd_changed_hook_called = false

0 commit comments

Comments
 (0)