Skip to content

fix(diff): mark a missing final newline wherever the last line appears - #108

Merged
letstri merged 1 commit into
letstri:mainfrom
TonyWu2333:fix/diff-no-newline-marker
Sep 25, 2026
Merged

letstri merged 1 commit into
letstri:mainfrom
TonyWu2333:fix/diff-no-newline-marker

Conversation

@TonyWu2333

@TonyWu2333 TonyWu2333 commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Fixes the open-debt row in IMPROVEMENTS.md, "Diffs lose 'no newline at end of file'". Before this, only a change that was nothing but the final newline got git's \ No newline at end of file (via newlineOnlyDiff). A file with other edits too lost the marker, and if its last line gained or lost a newline next to those edits, the diff showed that line as unchanged.
  • unifiedDiff now compares the last line of a side without a final newline as unequal to a terminated line. It only changes the comparison keys; the lines it shows are untouched. That makes the Myers/rewrite paths treat the last line the way git does, and the marker now follows that line whether it's context, a deletion or an addition, rewrites included. newlineOnlyDiff isn't needed any more, so it's removed. The marker still doesn't count toward lines.
  • paneLines (DiffView.tsx) no longer gives the marker a slot. OpenTUI 0.5.11's <diff> renderable draws no row for a \ line: buildUnifiedView has no branch for it, and buildSplitView does i++. So the extra slot pushed every ref after it down by one, and the misplaced syntax colour cut the next row short (+ const three = instead of + const three = 3). The new UI test catches that.
  • Removes the fixed row from IMPROVEMENTS.md.

For patch bodies from the first @@ on, I checked the output against git diff --no-index on a dozen EOF cases: gained/lost newline, only-context, added last line, emptied file, new file without a newline, far-apart hunks. Every hunk body matched git line for line. The only differences were header details that are the same as before this change (git's -1 vs our -1,1, and git's function-context suffix after @@).

Test plan

  • bun test test/diff.test.ts: 4 new cases (other edits plus a gained newline, as an exact patch; unterminated last line as context; an added unterminated line; the rewrite path). All 4 fail on main and pass here.
  • bun test test/diff-view.test.tsx: new case "the rows after a missing final newline are drawn whole, in either layout". It fails with the old paneLines and passes here.
  • bun run check: types, lint and format are clean. All diff/changes suites pass. In the parallel run, a few git-status UI files (git-footer, git-discard-ui, and on one run unit, git-panel, goto, statusbar) failed on timing, and they fail the same way on unmodified main. Each of them passes when run on its own, on this branch and on main.
  • Commit a file with no final newline, edit an earlier line and add one at the end, then open the Changes page. The last line should show as -/+ with the marker in the patch, and no row after it should be cut.

Summary by CodeRabbit

  • Bug Fixes
    • Diffs now accurately indicate when a file’s final line has no trailing newline, including in context, added, and removed lines.
    • Inline and side-by-side views keep affected lines fully visible; the no-newline indicator no longer takes up a line in either pane.
    • Rewrites and other changes continue to display the final added line correctly, including for large diffs.

Only a change that was nothing but the final newline carried git's
`\ No newline at end of file`; any file with other edits lost it, and a
last line that gained or lost its newline beside them showed as
unchanged. The last line of a side with no newline now compares
unequal to a terminated one, so it diffs the way git's does, and the
marker follows it as context, deletion or addition, rewrites included.

The `<diff>` renderable draws no row for the marker, so `paneLines`
no longer gives it one: the slot put every row after it off by one,
and the misplaced syntax colour cut the next line short.
@vercel

vercel Bot commented Sep 24, 2026

Copy link
Copy Markdown

@TonyWu2333 is attempting to deploy a commit to the letstri Team on Vercel.

A member of the Team first needs to authorize it.

@coderabbitai

coderabbitai Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Navigate logical layers of code changes, visualize relationships, and explore their blast radius.

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Advanced

Run ID: b2c3fa6e-450f-4fc6-94bf-b20a229a64e6

📥 Commits

Reviewing files that changed from the base of the PR and between d396c93 and 8281fb6.

📒 Files selected for processing (5)
  • IMPROVEMENTS.md
  • src/core/diff.ts
  • src/ui/DiffView.tsx
  • test/diff-view.test.tsx
  • test/diff.test.ts
💤 Files with no reviewable changes (1)
  • IMPROVEMENTS.md

Included review availability: Your plan provides up to 10 included reviews per hour; 9 remain after this review.


📝 Walkthrough

Walkthrough

Diff generation now marks unterminated final lines in context, deletion, and addition rows. The diff view excludes the marker from pane rows. Tests cover patch content and inline and side-by-side rendering.

Changes

Final newline diff handling

Layer / File(s) Summary
Detect and render unterminated lines
src/core/diff.ts, test/diff.test.ts, IMPROVEMENTS.md
unifiedDiff compares normalized lines and adds the no-newline marker to the corresponding context, deletion, or addition row. Tests cover marker placement and rewrite output. The roadmap item about this behavior was removed.
Exclude marker rows from diff panes
src/ui/DiffView.tsx, test/diff-view.test.tsx
The marker does not take a pane row. The regression test checks that changed lines remain fully visible in inline and side-by-side views.

Priority: ⬇️ Low

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: letstri

Merge Risk: ⚪ Minimal · up to 8281f

No actionable merge-blocking issue is established; the change is ready for normal merge checks.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 57.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 7 functions across 4 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: marking a missing final newline wherever the affected line appears in the diff.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches 💡 1
🛠️ Fix failing CI checks 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR

Comment @coderabbitai help to get the list of available commands.

@letstri
letstri merged commit e9bfd0e into letstri:main Sep 25, 2026
1 of 2 checks passed
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.

2 participants