Skip to content

Commit

Permalink
feat(hooks): add support for close hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
sstallion committed Jun 6, 2023
1 parent 4d26053 commit 2487169
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Flexible session management for Neovim.
* Keep track of last used session
* Sessions stored in JSON files
* Store arbitrary data in the session file
* User hooks before/after save/load
* User hooks before/after save/load/close
* Uses good old `:mksession` under the hood
* Configurable automatic save
* Out of the box [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim) integration
Expand Down Expand Up @@ -89,6 +89,8 @@ require('possession').setup {
after_save = function(name, user_data, aborted) end,
before_load = function(name, user_data) return user_data end,
after_load = function(name, user_data) end,
before_close = function(name) end,
after_close = function(name) end,
},
plugins = {
close_windows = {
Expand Down
8 changes: 8 additions & 0 deletions doc/possession.txt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,14 @@ hooks.after_load~
Invoked just before `possession.load` returns. Receives the `user_data`
from the `before_load` hook.

hooks.before_close~
`function(name: string)
Invoked just before `possession.close` deletes buffers.

hooks.after_close~
`function(name: string)
Invoked just before `possession.close` returns.

*possession-plugins-config*
plugins~
`table`
Expand Down
2 changes: 2 additions & 0 deletions lua/possession/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ local function defaults()
after_save = function(name, user_data, aborted) end,
before_load = function(name, user_data) return user_data end,
after_load = function(name, user_data) end,
before_close = function(name) end,
after_close = function(name) end,
},
plugins = {
close_windows = {
Expand Down
18 changes: 18 additions & 0 deletions lua/possession/plugins/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ function M.after_load(name, plugin_data)
end
end

function M.before_close(name, plugin_data)
for _, p in ipairs(get_enabled('before_close')) do
call_plugin('before_close', p, name, plugin_data[p])
end
end

function M.after_close(name, plugin_data)
for _, p in ipairs(get_enabled('after_close')) do
call_plugin('after_close', p, name, plugin_data[p])
end
end

-- Crate a basic implementation of plugin hooks that does not store any session data.
--@param fn function: f(opts) -> boolean that will receive plugin config and should
-- return `true` on success
Expand All @@ -121,6 +133,12 @@ function M.implement_basic_hooks(fn)
after_load = function(opts, name, plugin_data)
fn(opts)
end,
before_close = function(opts, name, plugin_data)
fn(opts)
end,
after_close = function(opts, name, plugin_data)
fn(opts)
end,
}
end

Expand Down
9 changes: 9 additions & 0 deletions lua/possession/session.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,17 @@ function M.close(force)
if not M.session_name then
return
end
local path = paths.session(M.session_name)
local session_data = vim.json.decode(path:read())
local plugin_data = session_data.plugins or {}

plugins.before_close(session_data.name, plugin_data)
config.hooks.before_close(session_data.name)

utils.delete_all_buffers(force)

plugins.after_close(session_data.name, plugin_data)
config.hooks.after_close(session_data.name)
M.session_name = nil
end

Expand Down

0 comments on commit 2487169

Please sign in to comment.