diff --git a/README.md b/README.md index 4a31678..d9d982c 100644 --- a/README.md +++ b/README.md @@ -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. @@ -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). @@ -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 diff --git a/doc/auto-session.txt b/doc/auto-session.txt index 408fce3..beb506b 100644 --- a/doc/auto-session.txt +++ b/doc/auto-session.txt @@ -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 diff --git a/lua/auto-session/autocmds.lua b/lua/auto-session/autocmds.lua index b11a01f..f86c810 100644 --- a/lua/auto-session/autocmds.lua +++ b/lua/auto-session/autocmds.lua @@ -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) @@ -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", }) @@ -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", }) diff --git a/lua/auto-session/config.lua b/lua/auto-session/config.lua index 4ec6a3f..175d50b 100644 --- a/lua/auto-session/config.lua +++ b/lua/auto-session/config.lua @@ -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 @@ -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). diff --git a/lua/auto-session/init.lua b/lua/auto-session/init.lua index d959196..baeec0f 100644 --- a/lua/auto-session/init.lua +++ b/lua/auto-session/init.lua @@ -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 @@ -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 diff --git a/tests/cwd_change_handling_spec.lua b/tests/cwd_change_handling_spec.lua index f014f28..98f57ac 100644 --- a/tests/cwd_change_handling_spec.lua +++ b/tests/cwd_change_handling_spec.lua @@ -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) @@ -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()) @@ -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