Skip to content

refactor: handle commenting on the last line

Tomas Vik (OOO back on 2024-07-02) requested to merge 342-handle-no-new-lines into main

If found three issues related to the diff parsing algorithm:

1) No new line at the end of the file

This is a diff when we change a file without a new line at the end of the file.

@@ -1 +1,4 @@
-# Initial readme
\ No newline at end of file
+1
+2
+3
+4
\ No newline at end of file

The parsing logic was unable to handle the \ No newline at end of file

2) The line count in diff hunk header is optional

In the previous section's diff you can see @@ -1 +1,4 @@, it's missing the line count for the old diff version (usually this header looks like: @@ -13,6 +12,7 @@). I assume thanks to the no new line, the line count is zero and the diff output omits it.

I had to change the regex to make the count number optional.

3) Algorithm can't handle commenting on the last empty line

When I implemented the algorithm, I didn't realize that there is always an implicit unchanged empty line at the end of the diff. The algorithm can't handle the last empty line without partial rewrite. The solution is to not allow users to comment on the last unchanged empty line.

Related to #342 (closed)

Merge request reports