feat: add copy session to Telescope actions

main
olimorris 2023-12-20 12:45:11 +00:00
parent 3e0c504ba2
commit ff261c2d22
4 changed files with 45 additions and 30 deletions

View File

@ -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:
- `<CR>` - Source the session file
- `<CR>` - Open/source the session file
- `<C-b>` - Add/update the git branch for the session file
- `<C-c>` - Copy the session file
- `<C-d>` - Delete the session file
- `<C-a>` - 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?
},
})
```

View File

@ -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,8 +45,7 @@ end
function M.dirs_match(dir, dirs_table)
dir = vim.fn.expand(dir)
return dirs_table
and next(vim.tbl_filter(
function(pattern)
and next(vim.tbl_filter(function(pattern)
if pattern.exact then
-- The pattern is actually a table
pattern = pattern[1]
@ -60,8 +58,7 @@ function M.dirs_match(dir, dirs_table)
else
return dir:find(escape_pattern(vim.fn.expand(pattern)))
end
end,
dirs_table))
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

View File

@ -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", "<c-a>", function()
return _actions.add_branch(config)
map("i", "<c-b>", function()
return _actions.change_branch(config)
end)
map("i", "<c-c>", function()
return _actions.copy_session(config)
end)
map("i", "<c-d>", _actions.delete_session)

View File

@ -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)