Repository navigation
feat: make the words that changed within a line stand out in a diff - #48
Merged
Merged
Conversation
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
In a diff, a line that replaces another now shows which words changed: in the removed line the words that went, in the added line the words that came, each on a stronger shade of red or green. A renamed variable, a new argument, a changed type or a shifted indent is easy to spot without reading both lines side by side. A line rewritten as a whole keeps the plain line colour, so only changes worth pointing at are marked.
How it works: while parsing a diff, the k-th removed line of a block of changes pairs with the k-th added line after it (
Line::pair). For a pair on screen, both lines are split into words, runs of white space and single other characters; whatever they start and end with in common is set aside, and the longest run of tokens both share in order is found for the middle (up to 256 tokens each; beyond that only the common start and end count). The rest is marked, when at least half of the two lines, white space aside, stayed the same. The result is kept in egui's frame cache, so a line is compared once while it stays on screen, not every frame (a typical line takes ~4 µs, the worst case at the limit ~0.4 ms in a release build). Opening or reloading a diff costs nothing extra.This is the first part of "Better diffs"; side by side, syntax colouring and images follow.
How it is tested
Core:
lines_that_replace_each_other_pair_up(pairs in blocks of different sizes, also across "No newline at end of file"),words_that_changed_within_a_line(words, joined neighbours, white space, non-ASCII),lines_with_little_in_common_have_no_words,long_lines_compare_on_what_they_start_and_end_with, anda_changed_line_knows_its_words_that_changed, which reads a realgit diffwith a changed signature, a changed word, and an added line without a pair.App:
a_changed_line_marks_the_words_that_changedopens a changed file and checks which painted text is on the word colours: only "count" and "amount", not the line rewritten as a whole. It fails without the marking.Tried in the app on a small repository (types, an argument, an indent, a prose comment, a picked line) and on commits in git.git.
cargo fmt --all,cargo clippy --workspace --all-targets -- -D warningsandcargo test --workspacepassIt adds as little code as the change needs, and removes what it makes unnecessary
Nothing in it comes from GitKraken or another closed-source client (see CONTRIBUTING.md)