From 8396da9e95e6fb6d720029267e3d36b508285029 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Mon, 11 Nov 2024 10:57:09 -0800 Subject: [PATCH 01/10] fix: SessionSearch w/o Telescope The fallback case for SessionSearch when Telescope isn't installed wasn't setting the argument to handle_autosession_command correctly --- lua/auto-session/autocmds.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/auto-session/autocmds.lua b/lua/auto-session/autocmds.lua index b11a01f..64ee82c 100644 --- a/lua/auto-session/autocmds.lua +++ b/lua/auto-session/autocmds.lua @@ -258,7 +258,7 @@ function M.setup_autocmds(AutoSession) return end - handle_autosession_command { "search" } + handle_autosession_command { args = "search" } end, { desc = "Open a session picker", }) From 498ddfb3c74c619d94aba64f27e9447ac54ba1d7 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Mon, 11 Nov 2024 10:58:02 -0800 Subject: [PATCH 02/10] fix: #391 err loading session w/ terminal from Fzf --- lua/auto-session/autocmds.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lua/auto-session/autocmds.lua b/lua/auto-session/autocmds.lua index 64ee82c..87c7152 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) From 69a7a52f5b45f66414e668520f470becffcd686f Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Tue, 12 Nov 2024 09:40:22 -0800 Subject: [PATCH 03/10] fix(README): #372 Oil may conflict with dir arg --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4a31678..f87dc0e 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. @@ -416,6 +416,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. [Oil](https://github.com/stevearc/oil.nvim)), it may prevent AutoSession from loading the session when launched with a directory argument. You can avoid that by lazy loading that plugin. + 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 From 167892f6677ad7489b236e9730a705b5a7ebde4d Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Tue, 12 Nov 2024 11:13:50 -0800 Subject: [PATCH 04/10] chore(README): link to example of lazy loading oil --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f87dc0e..8987e8a 100644 --- a/README.md +++ b/README.md @@ -416,7 +416,7 @@ 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. [Oil](https://github.com/stevearc/oil.nvim)), it may prevent AutoSession from loading the session when launched with a directory argument. You can avoid that by lazy loading that plugin. +Also, if you use a plugin that handles directory arguments (e.g. [Oil](https://github.com/stevearc/oil.nvim)), it may prevent AutoSession from loading the session when launched with a directory argument. You can avoid that by [lazy loading that plugin](https://github.com/rmagatti/auto-session/issues/372#issuecomment-2471077783). 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: From 3699fb46dd1ec1e8c3ddc078767700d9d14700b8 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Wed, 13 Nov 2024 13:20:48 -0800 Subject: [PATCH 05/10] fix(README): add NvimTree to dir arg section --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8987e8a..70030a7 100644 --- a/README.md +++ b/README.md @@ -416,7 +416,7 @@ 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. [Oil](https://github.com/stevearc/oil.nvim)), it may prevent AutoSession from loading the session when launched with a directory argument. You can avoid that by [lazy loading that plugin](https://github.com/rmagatti/auto-session/issues/372#issuecomment-2471077783). +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: From 3b3209861c64b586e4b97b6977f8a4056f425020 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Sat, 16 Nov 2024 19:54:20 -0800 Subject: [PATCH 06/10] fix: cwd_change_handling error with terminal buf If cwd_change_handling is enabled and the session being restored contains a terminal, we might get a "Invalid argument: buftype=terminal". Rather than restoring the session in the callback, we schedule the restore for the next run of the event loop. --- lua/auto-session/autocmds.lua | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lua/auto-session/autocmds.lua b/lua/auto-session/autocmds.lua index 87c7152..f86c810 100644 --- a/lua/auto-session/autocmds.lua +++ b/lua/auto-session/autocmds.lua @@ -187,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", }) From e3d96acd9042a0034c15784096987512d29c16e4 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Sat, 16 Nov 2024 20:06:59 -0800 Subject: [PATCH 07/10] fix(cwd_change_handling_spec): handle vim.schedule Since cwd_change_handling now restores in vim.schedule, we have to wait for the next event loop in the tests. `vim.wait` works but using the async testing is probably the better way in the future: https://github.com/nvim-lua/plenary.nvim/issues/271 --- tests/cwd_change_handling_spec.lua | 3 +++ 1 file changed, 3 insertions(+) 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 From 6ebd713b90634878a601eeef3ffc0eb6961937a5 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Sat, 16 Nov 2024 20:12:21 -0800 Subject: [PATCH 08/10] feat: fixes #394 option for auto-restore notif --- lua/auto-session/config.lua | 2 ++ lua/auto-session/init.lua | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) 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 From 29fdf04b819451cc3b78c84f59a303fb69f9ad66 Mon Sep 17 00:00:00 2001 From: cameronr Date: Sun, 17 Nov 2024 04:13:43 +0000 Subject: [PATCH 09/10] chore(docs): auto-generate vimdoc --- doc/auto-session.txt | 1 + 1 file changed, 1 insertion(+) 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 From 40b65c151e6eb451ea2fb14b999fc6ef1a0d0ce6 Mon Sep 17 00:00:00 2001 From: Cameron Ring Date: Sat, 16 Nov 2024 20:24:32 -0800 Subject: [PATCH 10/10] fix(README): add show_auto_restore_notif --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 70030a7..d9d982c 100644 --- a/README.md +++ b/README.md @@ -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).