fix: autosaving (#56)

* fix: autosaving

* chore: newline

* feat: allow autosave override
main
Oli 2023-02-28 17:28:45 +00:00 committed by GitHub
parent 218fcb7eb9
commit 03f11b519f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -130,8 +130,11 @@ function M.stop()
end end
---Save the session ---Save the session
---@param opt? table
---@return nil ---@return nil
function M.save() function M.save(opt)
opt = opt or {}
-- If the user has stopped the session, then do not save -- If the user has stopped the session, then do not save
if vim.g.persisting == false then if vim.g.persisting == false then
return return
@ -145,10 +148,12 @@ function M.save()
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" }) vim.api.nvim_exec_autocmds("User", { pattern = "PersistedSavePre" })
if -- Autosave config option takes priority unless it's overriden
(config.options.autosave and type(config.options.should_autosave) == "function") if not config.options.autosave and not opt.override then
and not config.options.should_autosave() return
then end
if type(config.options.should_autosave) == "function" and not config.options.should_autosave() then
return return
end end

View File

@ -7,7 +7,7 @@ local persisted = require("persisted")
-- Create the user commands -- Create the user commands
vim.cmd([[command! SessionStart :lua require("persisted").start()]]) vim.cmd([[command! SessionStart :lua require("persisted").start()]])
vim.cmd([[command! SessionStop :lua require("persisted").stop()]]) vim.cmd([[command! SessionStop :lua require("persisted").stop()]])
vim.cmd([[command! SessionSave :lua require("persisted").save()]]) vim.cmd([[command! SessionSave :lua require("persisted").save({ override = true })]])
vim.cmd([[command! SessionLoad :lua require("persisted").load()]]) vim.cmd([[command! SessionLoad :lua require("persisted").load()]])
vim.cmd([[command! SessionLoadLast :lua require("persisted").load({ last = true })]]) vim.cmd([[command! SessionLoadLast :lua require("persisted").load({ last = true })]])
vim.cmd([[command! -nargs=1 SessionLoadFromFile :lua require("persisted").load({ session = <f-args> })]]) vim.cmd([[command! -nargs=1 SessionLoadFromFile :lua require("persisted").load({ session = <f-args> })]])