diff --git a/lua/persisted/init.lua b/lua/persisted/init.lua index 934e496..5f36dfa 100644 --- a/lua/persisted/init.lua +++ b/lua/persisted/init.lua @@ -163,6 +163,26 @@ function M.branch() end end +-- Switch sessions +---@param session_file_path string +function M.switch(session_file_path) + vim.api.nvim_create_autocmd("User", { + pattern = "PersistedSavePost", + callback = function() + vim.print("delete and load") + for _, buf in ipairs(vim.api.nvim_list_bufs()) do + if vim.api.nvim_buf_is_loaded(buf) then + vim.api.nvim_buf_delete(buf, {}) + end + end + M.load({ session = session_file_path }) + return true + end, + }) + + M.save({ session = vim.g.persisted_loaded_session }) +end + ---Select a session to load function M.select() ---@type { session: string, dir: string, branch?: string }[] @@ -193,9 +213,7 @@ function M.select() end, }, function(item) if item then - -- vim.fn.chdir(item.dir) - -- M.load() - vim.print(item.session) + M.switch(item.session) end end) end diff --git a/lua/telescope/_extensions/persisted/actions.lua b/lua/telescope/_extensions/persisted/actions.lua index aaccf58..fc4e9ef 100644 --- a/lua/telescope/_extensions/persisted/actions.lua +++ b/lua/telescope/_extensions/persisted/actions.lua @@ -33,21 +33,22 @@ function M.load_session(session) end, }) - vim.api.nvim_create_autocmd("User", { - pattern = "PersistedSavePost", - callback = function() - vim.print("delete and load") - for _, buf in ipairs(vim.api.nvim_list_bufs()) do - if vim.api.nvim_buf_is_loaded(buf) then - vim.api.nvim_buf_delete(buf, {}) - end - end - persisted.load({ session = session.file_path }) - return true - end, - }) - - persisted.save({ session = vim.g.persisted_loaded_session }) + -- vim.api.nvim_create_autocmd("User", { + -- pattern = "PersistedSavePost", + -- callback = function() + -- vim.print("delete and load") + -- for _, buf in ipairs(vim.api.nvim_list_bufs()) do + -- if vim.api.nvim_buf_is_loaded(buf) then + -- vim.api.nvim_buf_delete(buf, {}) + -- end + -- end + -- persisted.load({ session = session.file_path }) + -- return true + -- end, + -- }) + -- + -- persisted.save({ session = vim.g.persisted_loaded_session }) + persisted.switch(session.file_path) end ---Delete the selected session from disk