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
98 lines
2.9 KiB
Lua
98 lines
2.9 KiB
Lua
require('spec.helpers')
|
|
|
|
local commands = require('diffs.commands')
|
|
|
|
describe('commands', function()
|
|
describe('setup', function()
|
|
it('registers Gdiff, Gvdiff, and Ghdiff commands', function()
|
|
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)
|
|
|
|
describe('unified diff generation', function()
|
|
local old_lines = { 'local M = {}', 'return M' }
|
|
local new_lines = { 'local M = {}', 'local x = 1', 'return M' }
|
|
local diff_fn = vim.text and vim.text.diff or vim.diff
|
|
|
|
it('generates valid unified diff', function()
|
|
local old_content = table.concat(old_lines, '\n')
|
|
local new_content = table.concat(new_lines, '\n')
|
|
local diff_output = diff_fn(old_content, new_content, {
|
|
result_type = 'unified',
|
|
ctxlen = 3,
|
|
})
|
|
assert.is_not_nil(diff_output)
|
|
assert.is_true(diff_output:find('@@ ') ~= nil)
|
|
assert.is_true(diff_output:find('+local x = 1') ~= nil)
|
|
end)
|
|
|
|
it('returns empty for identical content', function()
|
|
local content = table.concat(old_lines, '\n')
|
|
local diff_output = diff_fn(content, content, {
|
|
result_type = 'unified',
|
|
ctxlen = 3,
|
|
})
|
|
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)
|