chriskuehl / fluffy

A file-sharing web app that doesn't suck.
https://fluffy.cc
Other
175 stars 22 forks source link

Fix line number mismatch when switching between side-by-side and unified diffs #166

Closed chriskuehl closed 1 year ago

chriskuehl commented 1 year ago

Fixes #163

The idea is to introduce an optional line number mapping when rendering text, such that one line of text in one rendered text view can map to (at least one, potentially multiple) different line numbers in the source text.

For example, if you have a unified diff like

--- a/testing/__init__.py  # 1
+++ b/testing/__init__.py  # 2
 existing line             # 3
+new line                  # 4

...then the left-hand diff would look like:

--- a/testing/__init__.py  # 1 => [1, 2]
 existing line             # 2 => [3]
                           # 3 => [4]  (empty line)

...and the right-hand diff would look like:

+++ b/testing/__init__.py  # 1 => [1, 2]
 existing line             # 2 => [3]
+new line                  # 3 => [4]

The side-by-side diffs have fewer rendered lines since +/- lines are rendered only on one side of the diff each at the same line position.

This PR introduces this mapping and updates all of the line selection logic so that lines are always selected against the mapped lines (which correspond the unified diff in this case, since it is the "raw" text for the paste and also always at least as long as the side-by-diff). So if you select "1" on the above side-by-side diffs, it will actually select both lines 1 and 2 behind the scenes and update the location hash and unified diff view that way. From the user perspective though, it behaves exactly as normal except that the lines in the location hash match the unified diff rather than the side-by-side diff.

This allows you to select lines on either the side-by-side or unified diff view and switch between the two while keeping selected lines at the right line. This is especially useful when sharing links with selected lines as otherwise if you send a link to someone with a different diff option, it will be highlighting the wrong lines.

Need a little more testing before merging.

chriskuehl commented 1 year ago

Before merging I want to change the behavior for mapped lines to keep counting up by 1 rather than showing the mapped line values for a line. You can see here that the side-by-side diff skips line 12 in the line numbers (since line 11 really maps to both [11, 12]) and goes straight to 13:

Screenshot_2023-05-12_01-19-19

vs

Screenshot_2023-05-12_01-19-38

This makes the line numbers line up with the unified diff but I think overall is more confusing. We can keep the mapping behind the scenes while still just showing regular line numbers and I don't think users will notice or care if they don't match up between side-by-side or unified.