Skip to content

Commit

Permalink
feat: add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 9, 2022
1 parent 1b6268f commit 350abfc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ This is a work in progress
TODO:
- periodic saving
- tab-scoped sessions
- hooks for plugins
- window size
- documentation
53 changes: 51 additions & 2 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ M.setup = function(config)
pending_config = config or {}
end

local hooks = {}

---@class resession.Hook
---@field on_save fun(): any
---@field on_load fun(data: any)

---@param name string
---@param hook resession.Hook
M.add_hook = function(name, hook)
hooks[name] = hook
end

---@param name string
M.remove_hook = function(name)
hooks[name] = nil
end

---@param name string
---@return string
local function get_session_file(name)
Expand All @@ -25,12 +42,14 @@ local function get_session_file(name)
)
end

---Get the name of the current session
---@return string|nil
M.get_current_session = function()
M.get_current = function()
return current_session
end

M.detach_session = function()
---Detach from the current session
M.detach = function()
current_session = nil
end

Expand Down Expand Up @@ -134,6 +153,26 @@ M.save = function(name, opts)
local winlayout = vim.fn.winlayout(tabnr)
tab.wins = layout.add_win_info_to_layout(tabnr, winlayout)
end

for k, hook in pairs(hooks) do
if data[k] then
vim.notify(
string.format("[resession] Cannot run hook named '%s'; it conflicts with built-in data", k),
vim.log.levels.WARN
)
else
local ok, hookdata = pcall(hook.on_save)
if ok then
data[k] = hookdata
else
vim.notify(
string.format("[resession] Hook %s error: %s", k, hookdata),
vim.log.levels.ERROR
)
end
end
end

files.write_json_file(filename, data)
if not opts.detach then
current_session = name
Expand Down Expand Up @@ -214,6 +253,16 @@ M.load = function(name, opts)
end
end
vim.api.nvim_set_current_win(curwin.winid)

for k, hook in pairs(hooks) do
if data[k] then
local ok, err = pcall(hook.on_load)
if not ok then
vim.notify(string.format("[resession] Hook %s error: %s", k, err), vim.log.levels.ERROR)
end
end
end

if not opts.detach then
current_session = name
end
Expand Down

0 comments on commit 350abfc

Please sign in to comment.