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:
Folke Lemaitre 2025-10-21 12:43:00 +02:00
parent 050052494e
commit 2b22fe7861
No known key found for this signature in database
GPG key ID: 9B52594D560070AB
2 changed files with 37 additions and 0 deletions

View file

@ -349,6 +349,42 @@ function M.git_stage(picker)
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)
if not item then
return

View file

@ -321,6 +321,7 @@ M.git_status = {
input = {
keys = {
["<Tab>"] = { "git_stage", mode = { "n", "i" } },
["<c-r>"] = { "git_restore", mode = { "n", "i" } },
},
},
},