fix(diff): seed newLineNumber from the hunk's to-file start - #239
Open
codymikol wants to merge 3 commits into
Open
fix(diff): seed newLineNumber from the hunk's to-file start#239codymikol wants to merge 3 commits into
codymikol wants to merge 3 commits into
Conversation
added 3 commits
July 22, 2026 10:18
Hunk.make had no direct unit test; only HunkHeaderSpec exercised header parsing, so a wrong per-line numbering went unnoticed. Covers both the two-value and bare single-line header forms.
Hunk.make seeded both the original- and new-line-number incrementers from header.fromFileLineNumbersStart, so newLineNumber was correct only when a hunk's from/to starts happened to match. Any hunk where they diverge (any hunk after the first, or one following an uneven add/remove count) got every Unchanged/Added line numbered wrong, which made FullFileLineHighlighter.highlight's line-content check fail and silently fall back to per-line highlighting. The per-line fallback in Diff.kt itself stays: it's also the correct behavior for content FullFileLineHighlighter legitimately can't place (stash entries, oversized files), not just a symptom of this bug.
Adds an integration case with two hunks whose from/to line-number starts diverge, asserting highlight() succeeds on the second hunk's line - and produces a real highlighted span, not just a non-null result - instead of returning null and forcing the per-line fallback.
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.
Summary
Root-causes and fixes the "always falls back to
highlightLine" bug:Hunk.makeseeded both the original- and new-line-number incrementers fromheader.fromFileLineNumbersStart, sonewLineNumberwas only correct when a hunk's from/to starts happened to match (typically only the first hunk in a file). Any later hunk got everyUnchanged/Addedline mis-numbered, which madeFullFileLineHighlighter.highlight's line-content check fail and silently fall back to per-line highlighting.src/main/kotlin/com/codymikol/data/diff/Hunk.kt— seednewLineNumberIncrementerfromheader.toFileLineNumbersStartinstead.src/test/kotlin/com/codymikol/data/diff/HunkSpec.kt— new unit coverage forHunk.make's per-line numbering (previously untested; onlyHunkHeaderSpeccovered header parsing), including the bare single-line header form (@@ -20 +25 @@).src/test/kotlin/com/codymikol/highlighting/FullFileLineHighlighterSpec.kt— new integration case with a real two-hunk diff whose from/to starts diverge, provinghighlight()succeeds (and produces a real span) on the second hunk's line instead of returning null.src/main/kotlin/com/codymikol/components/commit/diff/Diff.kt— comment only, explaining why thehighlightLinefallback call is kept.Closes #238
Note for reviewer — one open question
The issue text says the
highlightLinefallback inDiff.kt:180"should be removed" and Diff.kt "should always be usinghighlightLineFromFullFile". I did not do that, and want a maintainer to confirm before it happens:FullFileLineHighlighter.highlightlegitimately returnsnullin cases that aren't bugs — a stash entry (getFullContentcan't produce one), or an implausibly large file (theMAX_FULL_FILE_CHARSguard) — both of which have existing passing tests assertingnull. The oldhighlightLineis what currently renders any highlighting at all for those cases (it only needs the single line's text, not full-file content). Removing it outright would silently regress the Stash view and any 2MB+ file to zero syntax highlighting, not fix a bug.The issue's own attached root-cause research comment (marked "Verdict: recommend") diagnoses the actual defect as the
Hunk.ktnumbering bug fixed here, and prescribes that fix specifically — it does not recommend deleting the fallback. My read is the "should be removed" line in the issue body was written before that root-cause was known, on the assumption the fallback itself was the bug. Two independent review passes on this branch flagged the same tension and both concluded it needs explicit human sign-off rather than a unilateral call either way — surfacing it here rather than picking a side.Test plan
./gradlew build) — this sandbox has no local JDK/nix toolchain available (nix devShell requirestemurin-bin-21, not present in this container's read-only, pre-baked Nix store, and no daemon/build support is available to fetch it), so tests could not be executed locally; verified by manual trace ofHunk.makeagainst the newHunkSpeccases instead.