Skip to content

Commit

Permalink
feat: extensions can hook into pre or post load (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Sep 15, 2023
1 parent dc44ed5 commit dbcb8fc
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 13 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,14 @@ end

---Restore the extension state
---@param data The value returned from on_save
M.on_load = function(data)
--
M.on_pre_load = function(data)
-- This is run before the buffers, windows, and tabs are restored
end

---Restore the extension state
---@param data The value returned from on_save
M.on_post_load = function(data)
-- This is run after the buffers, windows, and tabs are restored
end

---Called when resession gets configured
Expand Down
2 changes: 1 addition & 1 deletion lua/resession/extensions/quickfix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ M.on_save = function()
end, vim.fn.getqflist())
end

M.on_load = function(data)
M.on_pre_load = function(data)
vim.fn.setqflist(data)
end

Expand Down
24 changes: 20 additions & 4 deletions lua/resession/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ M.list = function(opts)
return {}
end
---@diagnostic disable-next-line: param-type-mismatch
local fd = assert(uv.fs_opendir(session_dir, nil, 32))
local fd = assert(uv.fs_opendir(session_dir, nil, 256))
---@diagnostic disable-next-line: cast-type-mismatch
---@cast fd luv_dir_t
local entries = uv.fs_readdir(fd)
Expand Down Expand Up @@ -467,6 +467,22 @@ M.load = function(name, opts)
-- Set the options immediately
util.restore_global_options(data.global.options)
end

for ext_name in pairs(config.extensions) do
if data[ext_name] then
local ext = util.get_extension(ext_name)
if ext and ext.on_pre_load then
local ok, err = pcall(ext.on_pre_load, data[ext_name])
if not ok then
vim.notify(
string.format("[resession] Extension %s on_pre_load error: %s", ext_name, err),
vim.log.levels.ERROR
)
end
end
end
end

local scale = {
vim.o.columns / data.global.width,
(vim.o.lines - vim.o.cmdheight) / data.global.height,
Expand Down Expand Up @@ -527,11 +543,11 @@ M.load = function(name, opts)
for ext_name in pairs(config.extensions) do
if data[ext_name] then
local ext = util.get_extension(ext_name)
if ext then
local ok, err = pcall(ext.on_load, data[ext_name])
if ext and ext.on_post_load then
local ok, err = pcall(ext.on_post_load, data[ext_name])
if not ok then
vim.notify(
string.format('[resession] Extension "%s" load error: %s', ext_name, err),
string.format('[resession] Extension "%s" on_post_load error: %s', ext_name, err),
vim.log.levels.ERROR
)
end
Expand Down
13 changes: 7 additions & 6 deletions lua/resession/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
---@field dir nil|string Name of directory to load from (overrides config.dir)

---@class resession.Extension
---@field on_save fun():any
---@field on_load fun(data: any)
---@field config nil|fun(options: table)
---@field is_win_supported nil|fun(winid: integer, bufnr: integer): boolean
---@field save_win nil|fun(winid: integer): any
---@field load_win nil|fun(winid: integer, data: any): nil|integer
---@field on_save? fun():any
---@field on_pre_load? fun(data: any)
---@field on_post_load? fun(data: any)
---@field config? fun(options: table)
---@field is_win_supported? fun(winid: integer, bufnr: integer): boolean
---@field save_win? fun(winid: integer): any
---@field load_win? fun(winid: integer, data: any): nil|integer
4 changes: 4 additions & 0 deletions lua/resession/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ M.get_extension = function(name)
return
end
end
if ext.on_load then
-- TODO maybe add some deprecation notice in the future
ext.on_post_load = ext.on_load
end
ext_cache[name] = ext
return ext
else
Expand Down

0 comments on commit dbcb8fc

Please sign in to comment.