Skip to content

feat: make the words that changed within a line stand out in a diff - #48

Merged
Lars- merged 1 commit into
mainfrom
feat/word-diff
Nov 16, 2026
Merged

Lars- merged 1 commit into
mainfrom
feat/word-diff

Conversation

@Lars-

@Lars- Lars- commented Nov 16, 2026

Copy link
Copy Markdown
Owner

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, and a_changed_line_knows_its_words_that_changed, which reads a real git diff with a changed signature, a changed word, and an added line without a pair.

  • App: a_changed_line_marks_the_words_that_changed opens 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 warnings and cargo test --workspace pass

  • It 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)

@Lars-
Lars- merged commit 7fbecd4 into main Nov 16, 2026
7 checks passed
@Lars- Lars- mentioned this pull request Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant