fix: ranged comment issues (#214)

This MR changes the hunk parsing strategy for ranged comments in the new SHA. Now, we're only parsing and comparing the new lines. Fixes an issue where you could leave a ranged comment in the new buffer on an unchanged line and the plugin would not detect the line number correctly.
This commit is contained in:
Harrison (Harry) Cramer
2024-03-04 10:52:13 -05:00
committed by GitHub
parent c73d22996e
commit c0c67486d1

View File

@@ -64,20 +64,25 @@ local line_was_added = function(linnr, hunk, all_diff_output)
for matching_line_index, line in ipairs(all_diff_output) do for matching_line_index, line in ipairs(all_diff_output) do
local found_hunk = M.parse_possible_hunk_headers(line) local found_hunk = M.parse_possible_hunk_headers(line)
if found_hunk ~= nil and vim.deep_equal(found_hunk, hunk) then if found_hunk ~= nil and vim.deep_equal(found_hunk, hunk) then
-- For added lines, we only want to iterate over the part of the diff that has has new lines, -- Parse the lines from the hunk and return only the added lines
-- so we skip over the old range. We then keep track of the increment to the original new line index, local hunk_lines = {}
-- and iterate until we reach the end of the total range of this hunk. If we arrive at the matching local i = 1
-- index for the line number, we check to see if the line was added. local line_content = all_diff_output[matching_line_index + i]
local i = 0 while line_content ~= nil and line_content:sub(1, 2) ~= "@@" do
local old_range = (found_hunk.old_range == 0 and found_hunk.old_line ~= 0) and 1 or found_hunk.old_range
for hunk_line_index = matching_line_index + old_range, matching_line_index + old_range + found_hunk.new_range, 1 do
local line_content = all_diff_output[hunk_line_index]
if (found_hunk.new_line + i) == linnr then
if string.match(line_content, "^%+") then if string.match(line_content, "^%+") then
return true table.insert(hunk_lines, line_content)
end
end end
i = i + 1 i = i + 1
line_content = all_diff_output[matching_line_index + i]
end
-- We are only looking at added lines in the changed hunk to see if their index
-- matches the index of a line that was added
local starting_index = found_hunk.new_line - 1 -- The "+j" will add one
for j, _ in ipairs(hunk_lines) do
if (starting_index + j) == linnr then
return true
end
end end
end end
end end
@@ -274,7 +279,7 @@ M.calculate_matching_line_new = function(old_sha, new_sha, file_path, line_numbe
end end
-- TODO: Possibly handle lines that are out of range in the new files -- TODO: Possibly handle lines that are out of range in the new files
return line_number return line_number + net_change + 1
end end
return M return M