diff --git a/README.md b/README.md index 8905866..32958dd 100644 --- a/README.md +++ b/README.md @@ -118,13 +118,12 @@ The plugin comes with a number of commands: ### Telescope extension -The Telescope extension may be opened via `:Telescope persisted`. +The Telescope extension may be opened via `:Telescope persisted`. The available actions are: -Once opened, the available keymaps are: - -- `` - Source the session file +- `` - Open/source the session file +- `` - Add/update the git branch for the session file +- `` - Copy the session file - `` - Delete the session file -- `` - Add/update a git branch to the session file ### Global variables @@ -153,7 +152,7 @@ require("persisted").setup({ 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 = { - reset_prompt = true, -- Reset prompt after a telescope action? + reset_prompt = true, -- Reset the Telescope prompt after an action? }, }) ``` diff --git a/lua/persisted/utils.lua b/lua/persisted/utils.lua index 2024927..c2c088d 100644 --- a/lua/persisted/utils.lua +++ b/lua/persisted/utils.lua @@ -1,7 +1,6 @@ local M = {} local e = vim.fn.fnameescape -local fp_sep = vim.loop.os_uname().sysname:lower():match('windows') and '\\' or '/' -- \ for windows, mac and linux both use \ - +local fp_sep = vim.loop.os_uname().sysname:lower():match("windows") and "\\" or "/" -- \ for windows, mac and linux both use \ ---Print an error message --@param msg string @@ -46,22 +45,20 @@ end function M.dirs_match(dir, dirs_table) dir = vim.fn.expand(dir) return dirs_table - and next(vim.tbl_filter( - function(pattern) - if pattern.exact then - -- The pattern is actually a table - pattern = pattern[1] - -- Stripping off the trailing backslash that a user might put here, - -- but only if we aren't looking at the root directory - if pattern:sub(-1) == fp_sep and pattern:len() > 1 then - pattern = pattern:sub(1, -2) - end - return dir == pattern - else - return dir:find(escape_pattern(vim.fn.expand(pattern))) + and next(vim.tbl_filter(function(pattern) + if pattern.exact then + -- The pattern is actually a table + pattern = pattern[1] + -- Stripping off the trailing backslash that a user might put here, + -- but only if we aren't looking at the root directory + if pattern:sub(-1) == fp_sep and pattern:len() > 1 then + pattern = pattern:sub(1, -2) end - end, - dirs_table)) + return dir == pattern + else + return dir:find(escape_pattern(vim.fn.expand(pattern))) + end + end, dirs_table)) end ---Get the directory pattern based on OS @@ -88,6 +85,7 @@ function M.load_session(session, silent) vim.g.persisted_exists = true vim.g.persisted_loaded_session = session + vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session }) end diff --git a/lua/telescope/_extensions/persisted.lua b/lua/telescope/_extensions/persisted.lua index d4128fa..a99cc94 100644 --- a/lua/telescope/_extensions/persisted.lua +++ b/lua/telescope/_extensions/persisted.lua @@ -21,16 +21,20 @@ local function search_sessions(opts) local refresh_sessions = function() local picker = action_state.get_current_picker(prompt_bufnr) picker:refresh(_finders.session_finder(require("persisted").list()), { - -- INFO: Account for users who may have hard coded the old API in their code + -- INFO: Account for users who are still using the old API reset_prompt = config.telescope.reset_prompt or config.telescope.reset_prompt_after_deletion, }) end - _actions.add_branch:enhance({ post = refresh_sessions }) + _actions.change_branch:enhance({ post = refresh_sessions }) + _actions.copy_session:enhance({ post = refresh_sessions }) _actions.delete_session:enhance({ post = refresh_sessions }) - map("i", "", function() - return _actions.add_branch(config) + map("i", "", function() + return _actions.change_branch(config) + end) + map("i", "", function() + return _actions.copy_session(config) end) map("i", "", _actions.delete_session) diff --git a/lua/telescope/_extensions/persisted/actions.lua b/lua/telescope/_extensions/persisted/actions.lua index 4333595..3fc72ce 100644 --- a/lua/telescope/_extensions/persisted/actions.lua +++ b/lua/telescope/_extensions/persisted/actions.lua @@ -35,9 +35,10 @@ M.delete_session = function() end end ----Add a branch to an existing session +---Change the branch of an existing session +---@param config table ---@return nil -M.add_branch = function(config) +M.change_branch = function(config) local session = get_selected_session() local path = session.file_path @@ -46,7 +47,7 @@ M.add_branch = function(config) 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 + -- Check for existing branch 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) @@ -63,4 +64,17 @@ M.add_branch = function(config) end end +---Copy an existing session +---@return nil +M.copy_session = function(config) + local session = get_selected_session() + local old_name = session.file_path:gsub(config.save_dir, "") + + local new_name = vim.fn.input("New session name: ", old_name) + + if vim.fn.confirm("Rename session from [" .. old_name .. "] to [" .. new_name .. "]?", "&Yes\n&No") == 1 then + os.execute("cp " .. session.file_path .. " " .. config.save_dir .. new_name) + end +end + return transform_mod(M)