feat(picker.git): added git_diff picker for diff hunks (#519)

Hi and thank you for the useful plugin!

## Description

I have been exploring the new picker api and got this git diff picker
working which I think can be a nice addition to the existing set of
picker. The key difference from the existing git_status picker is that
it separates each hunk into its own item similar to `git add --patch`
and when you select an item it takes you to the first hunk's line
instead of top of the buffer. Before I put more effort into it I would
like to see if this is a welcome change first.

This implementation can be further extended by allowing to support
additional arguments such as `--staged`, `--ignore-all-space` or both.

A few points on current implementations. I could not get the per line
parsing to work using the
`require("snacks.picker.source.proc").proc(...)` and fell back to
manually parsing the output after the command finished executing. There
are likely many ways to improve it. I have been using it with telescope
for about a year and so far it worked great. I think more people can
benefit from this picker.

## Related Issue(s)

none

## Screenshots

Demo


https://github.com/user-attachments/assets/6cdfa48e-bc29-4194-8430-092fbc5f3dcd

---------

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
Anton Kastritskii 2025-01-16 08:54:31 +00:00 committed by GitHub
parent f06f14c4ae
commit cc69043689
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 68 additions and 2 deletions

View file

@ -46,13 +46,17 @@ function M.preview(ctx)
ctx.preview:reset()
local lines = vim.split(ctx.item.preview.text, "\n")
vim.api.nvim_buf_set_lines(ctx.buf, 0, -1, false, lines)
ctx.preview:highlight({ ft = ctx.item.preview.ft })
if ctx.item.preview.ft then
ctx.preview:highlight({ ft = ctx.item.preview.ft })
end
for _, extmark in ipairs(ctx.item.preview.extmarks or {}) do
local e = vim.deepcopy(extmark)
e.col, e.row = nil, nil
vim.api.nvim_buf_set_extmark(ctx.buf, ns, (extmark.row or 1) - 1, extmark.col, e)
end
ctx.preview:loc()
if ctx.item.preview.loc ~= false then
ctx.preview:loc()
end
end
---@param ctx snacks.picker.preview.ctx