Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lua/octo/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1842,8 +1842,15 @@ end
function M.generate_position2line_map(diffhunk)
local diffhunk_lines = vim.split(diffhunk, "\n")
local diff_directive = diffhunk_lines[1]
---@type integer, integer
local left_offset, right_offset = string.match(diff_directive, "@@%s*%-(%d+),%d+%s%+(%d+)")
local left_offset_str, right_offset_str = string.match(diff_directive, "@@%s*%-(%d+),?%d*%s%+(%d+),?%d*")
---@type integer?, integer?
local left_offset, right_offset = tonumber(left_offset_str), tonumber(right_offset_str)
if left_offset == nil then
left_offset = 0
end
if right_offset == nil then
right_offset = 0
end
local right_side_lines = {} ---@type table<integer, integer>
local left_side_lines = {} ---@type table<integer, integer>
local right_side_line = right_offset
Expand All @@ -1863,12 +1870,6 @@ function M.generate_position2line_map(diffhunk)
left_side_line = left_side_line + 1
end
end
if left_offset == nil then
left_offset = 0
end
if right_offset == nil then
right_offset = 0
end
---@type { left_side_lines: table<integer, integer>, right_side_lines: table<integer, integer>, right_offset: integer, left_offset: integer }
return {
left_side_lines = left_side_lines,
Expand Down
44 changes: 44 additions & 0 deletions lua/tests/plenary/utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,47 @@ describe("string methods", function()
end)
end)
end)

describe("generate_position2line_map()", function()
it("parses a hunk header with explicit counts on both sides", function()
local diffhunk = table.concat({
"@@ -5,3 +10,3 @@",
" context1",
"+added1",
" context2",
}, "\n")
local map = this.generate_position2line_map(diffhunk)
eq(map.left_offset, 5)
eq(map.right_offset, 10)
eq(map.right_side_lines, { [2] = 10, [3] = 11, [4] = 12 })
eq(map.left_side_lines, { [2] = 5, [4] = 6 })
end)

it("parses a hunk header where the left side omits the count for a single-line range", function()
local diffhunk = table.concat({
"@@ -5 +10,3 @@",
" context1",
"+added1",
" context2",
}, "\n")
local map = this.generate_position2line_map(diffhunk)
eq(map.left_offset, 5)
eq(map.right_offset, 10)
eq(map.right_side_lines, { [2] = 10, [3] = 11, [4] = 12 })
eq(map.left_side_lines, { [2] = 5, [4] = 6 })
end)

it("parses a hunk header where the right side omits the count for a single-line range", function()
local diffhunk = table.concat({
"@@ -5,3 +10 @@",
" context1",
"-removed1",
" context2",
}, "\n")
local map = this.generate_position2line_map(diffhunk)
eq(map.left_offset, 5)
eq(map.right_offset, 10)
eq(map.right_side_lines, { [2] = 10, [4] = 11 })
eq(map.left_side_lines, { [2] = 5, [3] = 6, [4] = 7 })
end)
end)
Loading