feat(fugitive): line position tracking for keymaps

When pressing `du`/`dU` from a hunk line in the fugitive status buffer
(after expanding with `=`), the unified diff now opens at the
corresponding line instead of line 1.

Implementation:
- `fugitive.get_hunk_position()` returns @@ header and offset when on a hunk line
- `commands.find_hunk_line()` finds matching @@ header in diff buffer
- `commands.gdiff_file()` accepts optional `hunk_position` and jumps after opening

Also updates @phanen's README credit for the previous two fixes.

Closes #65
This commit is contained in:
Barrett Ruth 2026-02-05 00:27:35 -05:00
parent a6d4dcff1f
commit 9e857d4b29
5 changed files with 247 additions and 8 deletions

View file

@ -1,13 +1,15 @@
require('spec.helpers')
local commands = require('diffs.commands')
describe('commands', function()
describe('setup', function()
it('registers Gdiff, Gvdiff, and Ghdiff commands', function()
require('diffs.commands').setup()
local commands = vim.api.nvim_get_commands({})
assert.is_not_nil(commands.Gdiff)
assert.is_not_nil(commands.Gvdiff)
assert.is_not_nil(commands.Ghdiff)
commands.setup()
local cmds = vim.api.nvim_get_commands({})
assert.is_not_nil(cmds.Gdiff)
assert.is_not_nil(cmds.Gvdiff)
assert.is_not_nil(cmds.Ghdiff)
end)
end)
@ -37,4 +39,60 @@ describe('commands', function()
assert.are.equal('', diff_output)
end)
end)
describe('find_hunk_line', function()
it('finds matching @@ header and returns target line', function()
local diff_lines = {
'diff --git a/file.lua b/file.lua',
'--- a/file.lua',
'+++ b/file.lua',
'@@ -1,3 +1,4 @@',
' local M = {}',
'+local new = true',
' return M',
}
local hunk_position = {
hunk_header = '@@ -1,3 +1,4 @@',
offset = 2,
}
local target_line = commands.find_hunk_line(diff_lines, hunk_position)
assert.equals(6, target_line)
end)
it('returns nil when hunk header not found', function()
local diff_lines = {
'diff --git a/file.lua b/file.lua',
'@@ -1,3 +1,4 @@',
' local M = {}',
}
local hunk_position = {
hunk_header = '@@ -99,3 +99,4 @@',
offset = 1,
}
local target_line = commands.find_hunk_line(diff_lines, hunk_position)
assert.is_nil(target_line)
end)
it('handles multiple hunks and finds correct one', function()
local diff_lines = {
'diff --git a/file.lua b/file.lua',
'--- a/file.lua',
'+++ b/file.lua',
'@@ -1,3 +1,4 @@',
' local M = {}',
'+local x = 1',
' ',
'@@ -10,3 +11,4 @@',
' function M.foo()',
'+ print("hello")',
' end',
}
local hunk_position = {
hunk_header = '@@ -10,3 +11,4 @@',
offset = 2,
}
local target_line = commands.find_hunk_line(diff_lines, hunk_position)
assert.equals(10, target_line)
end)
end)
end)