@@ -22,36 +22,47 @@ def create_diff_and_linenums(
2222 original_lines : list [str ], new_lines : list [str ]
2323) -> tuple [str , list [int ]]:
2424 diff_lines = list (difflib .unified_diff (original_lines , new_lines ))
25- return difflines_to_str (diff_lines ), calc_new_line_nums (diff_lines )
25+ return difflines_to_str (diff_lines ), calc_line_num_changes (diff_lines )
2626
2727
28- def calc_new_line_nums (diff_lines : list [str ]) -> list [int ]:
28+ def calc_line_num_changes (diff_lines : list [str ]) -> list [int ]:
29+ """
30+ Calculates the line numbers changed from a list of diff lines
31+ Returns a list with unique elements.
32+ """
2933 if not diff_lines :
3034 return []
3135
32- added_line_nums = []
36+ changed_line_nums : list [ int ] = []
3337 current_line_number = 0
38+ original_line_number = 0
3439
3540 for line in diff_lines :
3641 if line .startswith ("@@" ):
3742 # Extract the starting line number for the updated file from the diff metadata.
3843 # The format is @@ -x,y +a,b @@, where a is the starting line number in the updated file.
39- start_line = line .split (" " )[2 ]
40- current_line_number = (
41- int (start_line .split ("," )[0 ][1 :]) - 1
42- ) # Subtract 1 because line numbers are 1-indexed
44+ start_line_original , start_line_updated = line .split (" " )[1 :3 ]
45+ original_line_number = int (start_line_original .split ("," )[0 ][1 :]) - 1
46+ current_line_number = int (start_line_updated .split ("," )[0 ][1 :]) - 1
4347
4448 elif line .startswith ("+" ):
4549 # Increment line number for each line in the updated file
4650 current_line_number += 1
47- if not line .startswith ("++" ): # Ignore the diff metadata lines
48- added_line_nums .append (current_line_number )
49-
50- elif not line .startswith ("-" ):
51- # Increment line number for unchanged/context lines
51+ if not line .startswith ("+++" ): # Ignore the diff metadata lines
52+ changed_line_nums .append (current_line_number )
53+
54+ elif line .startswith ("-" ):
55+ # Increment line number for each line in the original file
56+ original_line_number += 1
57+ if not line .startswith ("---" ): # Ignore the diff metadata lines
58+ changed_line_nums .append (original_line_number )
59+
60+ else :
61+ # Increment line numbers for unchanged/context lines
62+ original_line_number += 1
5263 current_line_number += 1
5364
54- return added_line_nums
65+ return list ( set ( changed_line_nums ))
5566
5667
5768def difflines_to_str (diff_lines : list [str ]) -> str :
0 commit comments