Skip to content

Commit

Permalink
feat(persistence): add pre- and post- load hooks (#24)
Browse files Browse the repository at this point in the history
* Add optional hook typings to config object

* Add optional hooks conditional calls

* Add hooks description to main readme
  • Loading branch information
5c077m4n authored May 16, 2024
1 parent 4982499 commit 3d443bd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ Persistence comes with the following defaults:
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving
pre_save = nil, -- a function to call before saving the session
post_save = nil, -- a function to call after saving the session
save_empty = false, -- don't save if there are no open file buffers
pre_load = nil, -- a function to call before loading the session
post_load = nil, -- a function to call after loading the session
}
```

Expand Down
3 changes: 3 additions & 0 deletions lua/persistence/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local M = {}

---@class PersistenceOptions
---@field pre_save? fun()
---@field post_save? fun()
---@field pre_load? fun()
---@field post_load? fun()
local defaults = {
dir = vim.fn.expand(vim.fn.stdpath("state") .. "/sessions/"), -- directory where session files are saved
options = { "buffers", "curdir", "tabpages", "winsize", "skiprtp" }, -- sessionoptions used for saving
Expand Down
12 changes: 12 additions & 0 deletions lua/persistence/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ function M.start()
end

M.save()

if type(Config.options.post_save) == "function" then
Config.options.post_save()
end
end,
})
end
Expand All @@ -76,7 +80,15 @@ function M.load(opt)
opt = opt or {}
local sfile = opt.last and M.get_last() or M.get_current()
if sfile and vim.fn.filereadable(sfile) ~= 0 then
if type(Config.options.pre_load) == "function" then
Config.options.pre_load()
end

vim.cmd("silent! source " .. e(sfile))

if type(Config.options.post_load) == "function" then
Config.options.post_load()
end
end
end

Expand Down

0 comments on commit 3d443bd

Please sign in to comment.