feat(parser): support combined diff format
Problem: `parse_buffer` didn't recognize `diff --combined` /
`diff --cc` headers, couldn't parse `@@@` old-side ranges, and
synthesized empty body lines as single-space regardless of prefix
width.
Solution: Add `diff --combined` and `diff --cc` filename patterns,
parse old-side `-start,count` from `@@@` lines, remove
`is_unified_diff` guard on empty body lines, and use
`string.rep(' ', hunk_prefix_width)` for synthetic context lines.
This commit is contained in:
parent
38e0187998
commit
bb87ae279b
2 changed files with 14 additions and 5 deletions
|
|
@ -234,6 +234,8 @@ function M.parse_buffer(bufnr)
|
|||
end
|
||||
|
||||
local diff_git_file = logical:match('^diff %-%-git a/.+ b/(.+)$')
|
||||
or logical:match('^diff %-%-combined (.+)$')
|
||||
or logical:match('^diff %-%-cc (.+)$')
|
||||
local neogit_file = logical:match('^modified%s+(.+)$')
|
||||
or (not logical:match('^new file mode') and logical:match('^new file%s+(.+)$'))
|
||||
or (not logical:match('^deleted file mode') and logical:match('^deleted%s+(.+)$'))
|
||||
|
|
@ -286,10 +288,17 @@ function M.parse_buffer(bufnr)
|
|||
new_remaining = file_new_count
|
||||
end
|
||||
else
|
||||
local hs, hc = logical:match('%-(%d+),?(%d*)')
|
||||
if hs then
|
||||
file_old_start = tonumber(hs)
|
||||
file_old_count = tonumber(hc) or 1
|
||||
old_remaining = file_old_count
|
||||
end
|
||||
local hs2, hc2 = logical:match('%+(%d+),?(%d*) @@')
|
||||
if hs2 then
|
||||
file_new_start = tonumber(hs2)
|
||||
file_new_count = tonumber(hc2) or 1
|
||||
new_remaining = file_new_count
|
||||
end
|
||||
end
|
||||
local at_end, context = logical:match('^(@@+.-@@+%s*)(.*)')
|
||||
|
|
@ -312,13 +321,12 @@ function M.parse_buffer(bufnr)
|
|||
end
|
||||
elseif
|
||||
logical == ''
|
||||
and is_unified_diff
|
||||
and old_remaining
|
||||
and old_remaining > 0
|
||||
and new_remaining
|
||||
and new_remaining > 0
|
||||
then
|
||||
table.insert(hunk_lines, ' ')
|
||||
table.insert(hunk_lines, string.rep(' ', hunk_prefix_width))
|
||||
old_remaining = old_remaining - 1
|
||||
new_remaining = new_remaining - 1
|
||||
elseif
|
||||
|
|
|
|||
|
|
@ -163,10 +163,10 @@ describe('parser', function()
|
|||
end
|
||||
end)
|
||||
|
||||
it('stops hunk at blank line', function()
|
||||
it('stops hunk at blank line when remaining counts exhausted', function()
|
||||
local bufnr = create_buffer({
|
||||
'M test.lua',
|
||||
'@@ -1,2 +1,3 @@',
|
||||
'@@ -1,1 +1,2 @@',
|
||||
' local x = 1',
|
||||
'+local y = 2',
|
||||
'',
|
||||
|
|
@ -529,7 +529,8 @@ describe('parser', function()
|
|||
assert.are.equal(1, #hunks)
|
||||
assert.are.equal(1, hunks[1].file_new_start)
|
||||
assert.are.equal(9, hunks[1].file_new_count)
|
||||
assert.is_nil(hunks[1].file_old_start)
|
||||
assert.are.equal(1, hunks[1].file_old_start)
|
||||
assert.are.equal(3, hunks[1].file_old_count)
|
||||
delete_buffer(bufnr)
|
||||
end)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue