fix: #15 callbacks being incorrectly detected

main
olimorris 2022-06-30 19:53:28 +01:00
parent 8e1da8a3d6
commit 0ad729ee38
5 changed files with 25 additions and 22 deletions

View File

@ -89,11 +89,11 @@ require("persisted").setup({
autoload = false, -- automatically load the session for the cwd on Neovim startup autoload = false, -- automatically load the session for the cwd on Neovim startup
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading
before_save = function() end, -- function to run before the session is saved to disk before_save = nil, -- function to run before the session is saved to disk
after_save = function() end, -- function to run after the session is saved to disk after_save = nil, -- function to run after the session is saved to disk
telescope = { -- options for the telescope extension telescope = { -- options for the telescope extension
before_source = function(session) end, -- function to run before the session is sourced via telescope before_source = nil, -- function to run before the session is sourced via telescope
after_source = function(session) end, -- function to run after the session is sourced via telescope after_source = nil, -- function to run after the session is sourced via telescope
}, },
}) })
``` ```
@ -243,7 +243,7 @@ The plugin allows for *before* and *after* callbacks to be used when sourcing a
```lua ```lua
require("persisted").setup({ require("persisted").setup({
telescope = { telescope = {
before_source = function() before_source = function(session)
-- Close all open buffers -- Close all open buffers
pcall(vim.cmd, "bufdo bwipeout") pcall(vim.cmd, "bufdo bwipeout")
end, end,
@ -254,10 +254,8 @@ require("persisted").setup({
}, },
}) })
``` ```
The callbacks must accept a *session* parameter which is a table that has the following properties:
A *session* table is exposed to the callback functions and has the following properties:
* name - The filename of the session. * name - The filename of the session.
* branch - The git branch of the session; *and*
* file_path - The file path to the session. * file_path - The file path to the session.
## :rocket: Usage ## :rocket: Usage

View File

@ -9,12 +9,12 @@ local defaults = {
autoload = false, -- automatically load the session for the cwd on Neovim startup autoload = false, -- automatically load the session for the cwd on Neovim startup
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading
before_save = function() end, -- function to run before the session is saved to disk before_save = nil, -- function to run before the session is saved to disk
after_save = function() end, -- function to run after the session is saved to disk after_save = nil, -- function to run after the session is saved to disk
after_source = function() end, -- function to run after the session is sourced after_source = nil, -- function to run after the session is sourced
telescope = { -- options for the telescope extension telescope = { -- options for the telescope extension
before_source = function(session) end, -- function to run before the session is sourced via telescope before_source = nil, -- function to run before the session is sourced via telescope
after_source = function(session) end, -- function to run after the session is sourced via telescope after_source = nil, -- function to run after the session is sourced via telescope
}, },
} }

View File

@ -102,7 +102,7 @@ function M.load(opt)
local session = opt.last and get_last() or get_current() local session = opt.last and get_last() or get_current()
if session and vim.fn.filereadable(session) ~= 0 then if session and vim.fn.filereadable(session) ~= 0 then
utils.load_session(session, _, config.options.after_source()) utils.load_session(session, config.options.before_save, config.options.after_save)
end end
if config.options.autosave and (allow_dir() and not ignore_dir()) then if config.options.autosave and (allow_dir() and not ignore_dir()) then
@ -140,12 +140,16 @@ end
---Save the session to disk ---Save the session to disk
---@return nil ---@return nil
function M.save() function M.save()
config.options.before_save() if type(config.options.before_save) == "function" then
config.options.before_save()
end
vim.cmd("mks! " .. e(get_current())) vim.cmd("mks! " .. e(get_current()))
vim.g.persisting = true vim.g.persisting = true
config.options.after_save() if type(config.options.after_save) == "function" then
config.options.after_save()
end
end end
---Delete the current session from disk ---Delete the current session from disk

View File

@ -1,4 +1,5 @@
local M = {} local M = {}
local e = vim.fn.fnameescape
--- Split a string into a table --- Split a string into a table
---@param input string ---@param input string
@ -68,7 +69,7 @@ function M.load_session(session, before_callback, after_callback)
if type(before_callback) == "function" then if type(before_callback) == "function" then
before_callback() before_callback()
end end
local ok, result = pcall(vim.cmd, "source " .. vim.fn.fnameescape(session)) local ok, result = pcall(vim.cmd, "source " .. e(session))
if not ok then if not ok then
return M.echoerr("Error loading the session! ", result) return M.echoerr("Error loading the session! ", result)
end end

View File

@ -28,11 +28,11 @@ local function search_sessions(opts)
local session = action_state.get_selected_entry() local session = action_state.get_selected_entry()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
utils.load_session(session.file_path, function() utils.load_session(
return config.telescope.before_source(session) session.file_path,
end, function() config.telescope.before_source and config.telescope.before_source(session) or _,
return config.telescope.after_source(session) config.telescope.after_source and config.telescope.after_source(session) or _
end) )
end) end)
return true return true
end, end,