feat: #31 should_autosave callback

main
olimorris 2022-10-07 12:46:58 +01:00
parent 3bf38d98b7
commit df435cee43
3 changed files with 25 additions and 1 deletions

View File

@ -130,6 +130,7 @@ require("persisted").setup({
use_git_branch = false, -- create session files based on the branch of the git enabled repository use_git_branch = false, -- create session files based on the branch of the git enabled repository
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
should_autosave = nil, -- function to determine if a session should be autosaved
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 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
@ -197,6 +198,21 @@ require("persisted").setup({
Autosaving can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`. Autosaving can be further controlled for certain directories by specifying `allowed_dirs` and `ignored_dirs`.
There may be occasions when you do not wish to autosave; perhaps when a dashboard or terminal are open. To control this,
a callback function, `should_autosave`, may be specified. This function should return a boolean value.
```lua
require("persisted").setup({
should_autosave = function()
-- do not autosave if the alpha dashboard is the current filetype
if vim.bo.filetype == "alpha" then
return false
end
return true
end,
})
```
### Autoloading ### Autoloading
The plugin can be enabled to automatically load sessions when Neovim is started. Whilst off by default, this can be turned on by: The plugin can be enabled to automatically load sessions when Neovim is started. Whilst off by default, this can be turned on by:

View File

@ -8,6 +8,7 @@ local defaults = {
use_git_branch = false, -- create session files based on the branch of the git enabled repository use_git_branch = false, -- create session files based on the branch of the git enabled repository
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
should_autosave = nil, -- function to determine if a session should be autosaved
autoload = false, -- automatically load the session for the cwd on Neovim startup autoload = false, -- automatically load the session for the cwd on Neovim startup
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 for auto-saving and auto-loading ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading

View File

@ -105,7 +105,7 @@ function M.load(opt)
if session then if session then
if vim.fn.filereadable(session) ~= 0 then if vim.fn.filereadable(session) ~= 0 then
utils.load_session(session, config.options.before_source, config.options.after_source, config.options.silent) utils.load_session(session, config.options.before_source, config.options.after_source, config.options.silent)
elseif type(config.options.on_autoload_no_session) == 'function' then elseif type(config.options.on_autoload_no_session) == "function" then
config.options.on_autoload_no_session() config.options.on_autoload_no_session()
end end
end end
@ -149,6 +149,13 @@ function M.save()
config.options.before_save() config.options.before_save()
end end
if
(config.options.autosave and type(config.options.should_autosave) == "function")
and not config.options.should_autosave()
then
return
end
vim.cmd("mks! " .. e(get_current())) vim.cmd("mks! " .. e(get_current()))
vim.g.persisting = true vim.g.persisting = true