feat: #27 add `on_autoload_no_session` hook

* Add `on_autoload_no_session` hook

* Update README.md
main
Mat Jones 2022-08-23 21:02:30 +00:00 committed by GitHub
parent e994852d86
commit 321ba42367
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View File

@ -130,6 +130,7 @@ require("persisted").setup({
branch_separator = "_", -- string used to separate session directory name from branch name branch_separator = "_", -- string used to separate session directory name from branch name
autosave = true, -- automatically save session files when exiting Neovim autosave = true, -- automatically save session files when exiting Neovim
autoload = false, -- automatically load the session for the cwd on Neovim startup autoload = false, -- automatically load the session for the cwd on Neovim startup
on_autoload_no_session = nil, -- function to run when `autoload = true` but there is no session to load
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading
before_save = nil, -- function to run before the session is saved to disk before_save = nil, -- function to run before the session is saved to disk
@ -205,6 +206,17 @@ require("persisted").setup({
}) })
``` ```
You can also provide a function to run when `autoload = true` but there is no session to be loaded:
```lua
require("persisted").setup({
autoload = true,
on_autoload_no_session = function()
vim.notify("No existing session to load.")
end
})
```
Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`. Autoloading can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.

View File

@ -103,10 +103,15 @@ function M.load(opt)
opt = opt or {} opt = opt or {}
local session = opt.last and get_last() or get_current() local session = opt.last and get_last() or get_current()
if session and vim.fn.filereadable(session) ~= 0 then if session then
utils.load_session(session, config.options.before_source, config.options.after_source) if vim.fn.filereadable(session) ~= 0 then
utils.load_session(session, config.options.before_source, config.options.after_source)
elseif type(config.options.on_autoload_no_session) == 'function' then
config.options.on_autoload_no_session()
end
end end
if config.options.autosave and (allow_dir() and not ignore_dir()) then if config.options.autosave and (allow_dir() and not ignore_dir()) then
vim.schedule(function() vim.schedule(function()
M.start() M.start()