@@ -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
0 commit comments