diff --git a/doc/persisted.nvim.txt b/doc/persisted.nvim.txt index 89c98a9..96dc80f 100644 --- a/doc/persisted.nvim.txt +++ b/doc/persisted.nvim.txt @@ -42,7 +42,7 @@ Install the plugin with your preferred package manager: < - **Note:** The `lazy = true` option may be useful if you use a dashboard + **Note**The `lazy = true` option may be useful if you use a dashboard **Packer** >lua @@ -151,7 +151,8 @@ The plugin comes with the following defaults: require("persisted").setup({ save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved silent = false, -- silent nvim message when sourcing session file - use_git_branch = false, -- create session files based on the branch of the git enabled repository + use_git_branch = false, -- create session files based on the branch of a git enabled repository + default_branch = "main", -- the branch to load if a session file is not found for the current branch autosave = true, -- automatically save session files when exiting Neovim should_autosave = nil, -- function to determine if a session should be autosaved autoload = false, -- automatically load the session for the cwd on Neovim startup @@ -159,8 +160,8 @@ The plugin comes with the following defaults: follow_cwd = true, -- change session file name to match current working directory if it changes 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 - telescope = { -- options for the telescope extension - reset_prompt_after_deletion = true, -- whether to reset prompt after session deleted + telescope = { + reset_prompt = true, -- Reset prompt after a telescope action? }, }) < @@ -202,10 +203,11 @@ files for a given project, by using git branches. To enable git branching: }) < -If you switch branches in a repository, the plugin will try to load a session -which corresponds to that branch. If it can’t find one, then it will load the -session from the `main` branch. + **Note**If you initiate git in a repository which has an existing session file, + you’ll need to add it’s branch name to the session name. To do this from + within Neovim, use the `:Sessions` command, navigate to the session and press + ``. AUTOSAVING ~ diff --git a/lua/telescope/_extensions/persisted.lua b/lua/telescope/_extensions/persisted.lua index 476ba42..d4128fa 100644 --- a/lua/telescope/_extensions/persisted.lua +++ b/lua/telescope/_extensions/persisted.lua @@ -26,8 +26,12 @@ local function search_sessions(opts) }) end + _actions.add_branch:enhance({ post = refresh_sessions }) _actions.delete_session:enhance({ post = refresh_sessions }) + map("i", "", function() + return _actions.add_branch(config) + end) map("i", "", _actions.delete_session) actions.select_default:replace(function() diff --git a/lua/telescope/_extensions/persisted/actions.lua b/lua/telescope/_extensions/persisted/actions.lua index a58fb90..4333595 100644 --- a/lua/telescope/_extensions/persisted/actions.lua +++ b/lua/telescope/_extensions/persisted/actions.lua @@ -25,7 +25,7 @@ M.load_session = function(session, config) end ---Delete the selected session from disk ---@return string +--@return nil M.delete_session = function() local session = get_selected_session() local path = session.file_path @@ -35,4 +35,32 @@ M.delete_session = function() end end +---Add a branch to an existing session +---@return nil +M.add_branch = function(config) + local session = get_selected_session() + local path = session.file_path + + local branch = vim.fn.input("Branch name: ") + + if vim.fn.confirm("Add/update branch to [" .. branch .. "]?", "&Yes\n&No") == 1 then + local ext = path:match("^.+(%..+)$") + + -- Check for existing branch name in the filename + local branch_separator = config.branch_separator or "@@" + local pattern = "(.*)" .. branch_separator .. ".+" .. ext .. "$" + local base = path:match(pattern) or path:sub(1, #path - #ext) + + -- Replace or add the new branch name + local new_path = "" + if branch == "" then + new_path = base .. ext + else + new_path = base .. branch_separator .. branch .. ext + end + + os.rename(path, new_path) + end +end + return transform_mod(M)