feat: add copy session to Telescope actions
parent
3e0c504ba2
commit
ff261c2d22
11
README.md
11
README.md
|
|
@ -118,13 +118,12 @@ The plugin comes with a number of commands:
|
||||||
|
|
||||||
### Telescope extension
|
### 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>` - Open/source the session file
|
||||||
|
- `<C-b>` - Add/update the git branch for the session file
|
||||||
- `<CR>` - Source the session file
|
- `<C-c>` - Copy the session file
|
||||||
- `<C-d>` - Delete the session file
|
- `<C-d>` - Delete the session file
|
||||||
- `<C-a>` - Add/update a git branch to the session file
|
|
||||||
|
|
||||||
### Global variables
|
### 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
|
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
|
||||||
telescope = {
|
telescope = {
|
||||||
reset_prompt = true, -- Reset prompt after a telescope action?
|
reset_prompt = true, -- Reset the Telescope prompt after an action?
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
local M = {}
|
local M = {}
|
||||||
local e = vim.fn.fnameescape
|
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
|
---Print an error message
|
||||||
--@param msg string
|
--@param msg string
|
||||||
|
|
@ -46,22 +45,20 @@ end
|
||||||
function M.dirs_match(dir, dirs_table)
|
function M.dirs_match(dir, dirs_table)
|
||||||
dir = vim.fn.expand(dir)
|
dir = vim.fn.expand(dir)
|
||||||
return dirs_table
|
return dirs_table
|
||||||
and next(vim.tbl_filter(
|
and next(vim.tbl_filter(function(pattern)
|
||||||
function(pattern)
|
if pattern.exact then
|
||||||
if pattern.exact then
|
-- The pattern is actually a table
|
||||||
-- The pattern is actually a table
|
pattern = pattern[1]
|
||||||
pattern = pattern[1]
|
-- Stripping off the trailing backslash that a user might put here,
|
||||||
-- Stripping off the trailing backslash that a user might put here,
|
-- but only if we aren't looking at the root directory
|
||||||
-- but only if we aren't looking at the root directory
|
if pattern:sub(-1) == fp_sep and pattern:len() > 1 then
|
||||||
if pattern:sub(-1) == fp_sep and pattern:len() > 1 then
|
pattern = pattern:sub(1, -2)
|
||||||
pattern = pattern:sub(1, -2)
|
|
||||||
end
|
|
||||||
return dir == pattern
|
|
||||||
else
|
|
||||||
return dir:find(escape_pattern(vim.fn.expand(pattern)))
|
|
||||||
end
|
end
|
||||||
end,
|
return dir == pattern
|
||||||
dirs_table))
|
else
|
||||||
|
return dir:find(escape_pattern(vim.fn.expand(pattern)))
|
||||||
|
end
|
||||||
|
end, dirs_table))
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the directory pattern based on OS
|
---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_exists = true
|
||||||
vim.g.persisted_loaded_session = session
|
vim.g.persisted_loaded_session = session
|
||||||
|
|
||||||
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session })
|
vim.api.nvim_exec_autocmds("User", { pattern = "PersistedLoadPost", data = session })
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,16 +21,20 @@ local function search_sessions(opts)
|
||||||
local refresh_sessions = function()
|
local refresh_sessions = function()
|
||||||
local picker = action_state.get_current_picker(prompt_bufnr)
|
local picker = action_state.get_current_picker(prompt_bufnr)
|
||||||
picker:refresh(_finders.session_finder(require("persisted").list()), {
|
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,
|
reset_prompt = config.telescope.reset_prompt or config.telescope.reset_prompt_after_deletion,
|
||||||
})
|
})
|
||||||
end
|
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 })
|
_actions.delete_session:enhance({ post = refresh_sessions })
|
||||||
|
|
||||||
map("i", "<c-a>", function()
|
map("i", "<c-b>", function()
|
||||||
return _actions.add_branch(config)
|
return _actions.change_branch(config)
|
||||||
|
end)
|
||||||
|
map("i", "<c-c>", function()
|
||||||
|
return _actions.copy_session(config)
|
||||||
end)
|
end)
|
||||||
map("i", "<c-d>", _actions.delete_session)
|
map("i", "<c-d>", _actions.delete_session)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,9 +35,10 @@ M.delete_session = function()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---Add a branch to an existing session
|
---Change the branch of an existing session
|
||||||
|
---@param config table
|
||||||
---@return nil
|
---@return nil
|
||||||
M.add_branch = function(config)
|
M.change_branch = function(config)
|
||||||
local session = get_selected_session()
|
local session = get_selected_session()
|
||||||
local path = session.file_path
|
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
|
if vim.fn.confirm("Add/update branch to [" .. branch .. "]?", "&Yes\n&No") == 1 then
|
||||||
local ext = path:match("^.+(%..+)$")
|
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 branch_separator = config.branch_separator or "@@"
|
||||||
local pattern = "(.*)" .. branch_separator .. ".+" .. ext .. "$"
|
local pattern = "(.*)" .. branch_separator .. ".+" .. ext .. "$"
|
||||||
local base = path:match(pattern) or path:sub(1, #path - #ext)
|
local base = path:match(pattern) or path:sub(1, #path - #ext)
|
||||||
|
|
@ -63,4 +64,17 @@ M.add_branch = function(config)
|
||||||
end
|
end
|
||||||
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)
|
return transform_mod(M)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue