Add function to parse jj diff output

This commit is contained in:
themodrnhakr 2025-10-02 16:11:23 -05:00
parent 9a58ea573b
commit 5c75ec687c

View File

@ -39,6 +39,48 @@ local function _get_jj_diff_for_buffer()
return diff_output
end
-- Parse jj diff output
local function _parse_diff_output(diff_output)
local added_lines = {}
local changed_lines = {}
local deleted_lines = {}
local current_line_num = 0
local lines = vim.split(diff_output, "\n", { plain = true })
local prev_line_was_deleted = false
for _, line in ipairs(lines) do
if line:match("^@@ .- +(%d+)") then
-- Extract new_start_line from hunk header
local _, _, new_start_line_str = line:find("^@@ .- +(%d+)")
current_line_num = tonumber(new_start_line_str) - 1
prev_line_was_deleted = false
elseif line:match("^[ ]") then
current_line_num = current_line_num + 1
prev_line_was_deleted = false
elseif line:match("^[+]") then
current_line_num = current_line_num + 1
if prev_line_was_deleted then
table.insert(changed_lines, current_line_num)
else
table.insert(added_lines, current_line_num)
end
prev_line_was_deleted = false
elseif line:match("^[-]") then
-- For deleted lines, we mark the line *before* the deletion as changed,
-- or if it's the first line, we can't mark it.
-- This is a simplification for now.
if current_line_num > 0 then
table.insert(deleted_lines, current_line_num)
end
prev_line_was_deleted = true
end
end
return added_lines, changed_lines, deleted_lines
end
function M.setup(opts)
opts = opts or {}
_define_signs() -- Call to define signs