Skip to content

Commit c0c6748

Browse files
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.
1 parent c73d229 commit c0c6748

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

lua/gitlab/hunks/init.lua

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,25 @@ local line_was_added = function(linnr, hunk, all_diff_output)
6464
for matching_line_index, line in ipairs(all_diff_output) do
6565
local found_hunk = M.parse_possible_hunk_headers(line)
6666
if found_hunk ~= nil and vim.deep_equal(found_hunk, hunk) then
67-
-- For added lines, we only want to iterate over the part of the diff that has has new lines,
68-
-- so we skip over the old range. We then keep track of the increment to the original new line index,
69-
-- and iterate until we reach the end of the total range of this hunk. If we arrive at the matching
70-
-- index for the line number, we check to see if the line was added.
71-
local i = 0
72-
local old_range = (found_hunk.old_range == 0 and found_hunk.old_line ~= 0) and 1 or found_hunk.old_range
73-
for hunk_line_index = matching_line_index + old_range, matching_line_index + old_range + found_hunk.new_range, 1 do
74-
local line_content = all_diff_output[hunk_line_index]
75-
if (found_hunk.new_line + i) == linnr then
76-
if string.match(line_content, "^%+") then
77-
return true
78-
end
67+
-- Parse the lines from the hunk and return only the added lines
68+
local hunk_lines = {}
69+
local i = 1
70+
local line_content = all_diff_output[matching_line_index + i]
71+
while line_content ~= nil and line_content:sub(1, 2) ~= "@@" do
72+
if string.match(line_content, "^%+") then
73+
table.insert(hunk_lines, line_content)
7974
end
8075
i = i + 1
76+
line_content = all_diff_output[matching_line_index + i]
77+
end
78+
79+
-- We are only looking at added lines in the changed hunk to see if their index
80+
-- matches the index of a line that was added
81+
local starting_index = found_hunk.new_line - 1 -- The "+j" will add one
82+
for j, _ in ipairs(hunk_lines) do
83+
if (starting_index + j) == linnr then
84+
return true
85+
end
8186
end
8287
end
8388
end
@@ -274,7 +279,7 @@ M.calculate_matching_line_new = function(old_sha, new_sha, file_path, line_numbe
274279
end
275280

276281
-- TODO: Possibly handle lines that are out of range in the new files
277-
return line_number
282+
return line_number + net_change + 1
278283
end
279284

280285
return M

0 commit comments

Comments
 (0)