Skip to content

Commit dbcb8fc

Browse files
committed
feat: extensions can hook into pre or post load (#30)
1 parent dc44ed5 commit dbcb8fc

File tree

5 files changed

+40
-13
lines changed

5 files changed

+40
-13
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,14 @@ end
224224

225225
---Restore the extension state
226226
---@param data The value returned from on_save
227-
M.on_load = function(data)
228-
--
227+
M.on_pre_load = function(data)
228+
-- This is run before the buffers, windows, and tabs are restored
229+
end
230+
231+
---Restore the extension state
232+
---@param data The value returned from on_save
233+
M.on_post_load = function(data)
234+
-- This is run after the buffers, windows, and tabs are restored
229235
end
230236

231237
---Called when resession gets configured

lua/resession/extensions/quickfix.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ M.on_save = function()
1919
end, vim.fn.getqflist())
2020
end
2121

22-
M.on_load = function(data)
22+
M.on_pre_load = function(data)
2323
vim.fn.setqflist(data)
2424
end
2525

lua/resession/init.lua

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ M.list = function(opts)
9090
return {}
9191
end
9292
---@diagnostic disable-next-line: param-type-mismatch
93-
local fd = assert(uv.fs_opendir(session_dir, nil, 32))
93+
local fd = assert(uv.fs_opendir(session_dir, nil, 256))
9494
---@diagnostic disable-next-line: cast-type-mismatch
9595
---@cast fd luv_dir_t
9696
local entries = uv.fs_readdir(fd)
@@ -467,6 +467,22 @@ M.load = function(name, opts)
467467
-- Set the options immediately
468468
util.restore_global_options(data.global.options)
469469
end
470+
471+
for ext_name in pairs(config.extensions) do
472+
if data[ext_name] then
473+
local ext = util.get_extension(ext_name)
474+
if ext and ext.on_pre_load then
475+
local ok, err = pcall(ext.on_pre_load, data[ext_name])
476+
if not ok then
477+
vim.notify(
478+
string.format("[resession] Extension %s on_pre_load error: %s", ext_name, err),
479+
vim.log.levels.ERROR
480+
)
481+
end
482+
end
483+
end
484+
end
485+
470486
local scale = {
471487
vim.o.columns / data.global.width,
472488
(vim.o.lines - vim.o.cmdheight) / data.global.height,
@@ -527,11 +543,11 @@ M.load = function(name, opts)
527543
for ext_name in pairs(config.extensions) do
528544
if data[ext_name] then
529545
local ext = util.get_extension(ext_name)
530-
if ext then
531-
local ok, err = pcall(ext.on_load, data[ext_name])
546+
if ext and ext.on_post_load then
547+
local ok, err = pcall(ext.on_post_load, data[ext_name])
532548
if not ok then
533549
vim.notify(
534-
string.format('[resession] Extension "%s" load error: %s', ext_name, err),
550+
string.format('[resession] Extension "%s" on_post_load error: %s', ext_name, err),
535551
vim.log.levels.ERROR
536552
)
537553
end

lua/resession/types.lua

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
---@field dir nil|string Name of directory to load from (overrides config.dir)
1818

1919
---@class resession.Extension
20-
---@field on_save fun():any
21-
---@field on_load fun(data: any)
22-
---@field config nil|fun(options: table)
23-
---@field is_win_supported nil|fun(winid: integer, bufnr: integer): boolean
24-
---@field save_win nil|fun(winid: integer): any
25-
---@field load_win nil|fun(winid: integer, data: any): nil|integer
20+
---@field on_save? fun():any
21+
---@field on_pre_load? fun(data: any)
22+
---@field on_post_load? fun(data: any)
23+
---@field config? fun(options: table)
24+
---@field is_win_supported? fun(winid: integer, bufnr: integer): boolean
25+
---@field save_win? fun(winid: integer): any
26+
---@field load_win? fun(winid: integer, data: any): nil|integer

lua/resession/util.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ M.get_extension = function(name)
3232
return
3333
end
3434
end
35+
if ext.on_load then
36+
-- TODO maybe add some deprecation notice in the future
37+
ext.on_post_load = ext.on_load
38+
end
3539
ext_cache[name] = ext
3640
return ext
3741
else

0 commit comments

Comments
 (0)