mirror of
https://github.com/folke/snacks.nvim
synced 2025-12-23 08:47:57 +00:00
feat(picker): add git_restore action for git_status picker
Adds a git_restore action that discards changes to files in the git_status picker. Includes confirmation prompt before discarding changes to prevent accidental data loss. Usage: - `<C-r>` in git_status picker to restore selected file(s) - Works with multi-select (select multiple files and restore all) - Shows different confirmation messages for single vs multiple files Implementation: - Added git_restore() action in actions.lua - Bound to <C-r> in git_status picker - Supports both single and multi-select - Uses Snacks.picker.select for confirmation - Refreshes picker and returns to insert mode after restore Closes #2298 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
050052494e
commit
2b22fe7861
2 changed files with 37 additions and 0 deletions
|
|
@ -349,6 +349,42 @@ function M.git_stage(picker)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function M.git_restore(picker)
|
||||||
|
local items = picker:selected({ fallback = true })
|
||||||
|
if #items == 0 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Confirm before discarding changes
|
||||||
|
---@param item snacks.picker.Item
|
||||||
|
local files = vim.tbl_map(function(item)
|
||||||
|
return Snacks.picker.util.path(item)
|
||||||
|
end, items)
|
||||||
|
local msg = #items == 1 and ("Discard changes to `%s`?"):format(files[1])
|
||||||
|
or ("Discard changes to %d files?"):format(#items)
|
||||||
|
|
||||||
|
Snacks.picker.select({ "No", "Yes" }, { prompt = msg }, function(_, idx)
|
||||||
|
if not idx and idx == 2 then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
local done = 0
|
||||||
|
for _, item in ipairs(items) do
|
||||||
|
local cmd = { "git", "restore", item.file }
|
||||||
|
Snacks.picker.util.cmd(cmd, function(data, code)
|
||||||
|
done = done + 1
|
||||||
|
if done == #items then
|
||||||
|
vim.schedule(function()
|
||||||
|
picker.list:set_selected()
|
||||||
|
picker.list:set_target()
|
||||||
|
picker:find()
|
||||||
|
vim.cmd.startinsert()
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end, { cwd = item.cwd })
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
function M.git_stash_apply(_, item)
|
function M.git_stash_apply(_, item)
|
||||||
if not item then
|
if not item then
|
||||||
return
|
return
|
||||||
|
|
|
||||||
|
|
@ -321,6 +321,7 @@ M.git_status = {
|
||||||
input = {
|
input = {
|
||||||
keys = {
|
keys = {
|
||||||
["<Tab>"] = { "git_stage", mode = { "n", "i" } },
|
["<Tab>"] = { "git_stage", mode = { "n", "i" } },
|
||||||
|
["<c-r>"] = { "git_restore", mode = { "n", "i" } },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue