Skip to content

fix(editor): extract diff first-change auto-scroll to a hook to unblock main - #21738

Merged
nwparker merged 1 commit into
mainfrom
fix/diffviewer-max-lines
Sep 20, 2026
Merged

nwparker merged 1 commit into
mainfrom
fix/diffviewer-max-lines

Conversation

@nwparker

@nwparker nwparker commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 0 0 0 0
Prod 2 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​93 $\color{#cf222e}{\Huge{\mathbf{−}}}$​62 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​31

ELI5

One file in the code editor grew two lines past the size limit the project enforces. That makes the automated code check fail — not only on main, but on every open pull request, because each one is checked against a merged copy of main. This moves one self-contained piece of that file into its own file. Nothing about the app changes; the size check goes green again.

What Changed

src/renderer/src/components/editor/DiffViewer.tsx counted 402 lines against a 400-line cap (blank lines and comments already excluded), so oxlint reported:

src/renderer/src/components/editor/DiffViewer.tsx:445:2: File has too many lines (402). help: Maximum allowed is 400.

The behavior that piece implements: when you open a file diff, the view scrolls so the first changed line sits in the middle of the pane instead of leaving you at the top of a long unchanged file. It has to run as its own effect rather than inside the editor's mount callback, because the inline-comment decorator inserts view zones that would otherwise push the content down after the scroll position was measured.

That whole effect — the run-once-per-file guards, the "a comment already owns this scroll" bail-out, the deferred animation frame, and the onDidUpdateDiff subscription — now lives in useDiffViewerFirstChangeAutoScroll.ts and is called as a hook. The body is carried over verbatim. DiffViewer.tsx keeps what is left: editor mount wiring, comment popover state, view-state persistence, and render.

Before and after as a user experiences it: identical. Same scroll target, same timing, same render output. The only visible difference is that CI passes.

Why

Static analysis is red on main, which makes every open PR look broken — at least #20949, #21021 and #20981 fail on exactly this error without touching the file. That hides real failures, so it is worth fixing on its own branch rather than bundling into feature work.

Alternatives considered:

  • Suppress the rule or raise the per-file limit. Explicitly forbidden by AGENTS.md, and it would let the file keep growing.
  • Cut lines wherever the file happened to be longest. Only two counted lines had to go, so a minimal trim would have passed — but it leaves the file one line from failing again, and an arbitrary cut makes the code harder to follow rather than easier.
  • Extract the collapsed-unchanged-regions logic added by feat(editor): add opt-in collapsed unchanged regions for file diffs #11955, the commit that pushed the file over. It turns out that feature already lives in its own module (diff-editor-hide-unchanged-options.ts); its footprint here is a single import and a single spread. There was no seam left to take.
  • Extract the comment popover (state, positioning effect, submit handler, JSX). A larger and equally coherent seam, but it would move rendered markup and add a component boundary. For a change whose only purpose is to unblock CI, a pure-logic extraction that cannot alter the render tree is the safer trade.

The auto-scroll effect was the right seam: one coherent responsibility, no JSX, and a direct sibling precedent in useDiffViewerLargeDiffLifecycle.ts.

Linked Issue

No tracking issue — main is red and this restores it.

Visual Proof

N/A — pure refactor. No markup, props, render order, or styling changed; the extracted effect runs at the same point in the component with the same inputs.

Testing

Run from a worktree on this branch:

  • npx oxlint src/renderer/src/components/editor/ — clean; the max-lines error is gone and nothing new appeared.
  • pnpm run check:code-quality:changed — passes, 0 new findings across both changed files.
  • pnpm test src/renderer/src/components/editor/ — 1546 passed, 2 skipped, across 226 files. No test was modified.
  • npx tsc -p config/tsconfig.web.json --noEmit — no errors in components/editor/. The remaining TS6307 cross-project-import errors in unrelated test files are pre-existing on main.

Final counts: DiffViewer.tsx 390 raw lines (under the 400 cap for .tsx), useDiffViewerFirstChangeAutoScroll.ts 86 raw lines (under the 300 cap for .ts).

Tested on macOS. Platform-independent — renderer-only, no paths, shortcuts, or process spawning.

  • I manually tested these changes locally
  • Automated tests added/updated, or explained why not below

No new tests: this adds no behavior to test. The existing editor suite covers the surface and passes unchanged, which is the evidence that matters for a pure extraction.

Review

Agent skill upstream boundary

  • Not applicable, or this change follows docs/reference/agent-skill-sharing-upstream-boundary.md and copies or mechanically translates no upstream skill-installer source, tests, fixtures, registry entries, path tables, comments, or documentation.

Notes

No impact on security, cross-platform support, SSH/remote, mobile, backwards compatibility, or performance. Renderer-only code motion within one component; the extracted hook has the same call site, inputs, and effect lifetime it had inline.

One deliberate detail for review: diffEditorRef is now a hook argument, so it is listed in the effect's dependency array. It is a useRef object created by DiffViewer and its identity never changes for the component's lifetime, so the effect re-runs on exactly the same triggers as before. This matches useDiffViewerLargeDiffLifecycle, which takes the same ref the same way.

Checklist

  • This PR is small and focused
  • I explained what changed and why (ELI5, the user-facing before/after, the mechanism, and why over the alternatives)
  • Before/after screenshots or videos attached for UI changes, or N/A with reason
  • Self-reviewed for correctness, security, and performance
  • Cross-platform, SSH/remote, and path/shortcut impact considered (or N/A)
  • pnpm lint, pnpm typecheck, pnpm test, and pnpm build pass (or CI will cover; local preferred)

DiffViewer.tsx crossed the 400-line oxlint cap (402 counted lines), so
static analysis fails on main and reddens every open PR through the merge
ref. Moves the self-contained "center the viewport on the first change"
effect into useDiffViewerFirstChangeAutoScroll, leaving DiffViewer to own
mount wiring, comment popover state, and render.

No behavior change: the effect body, guards, rAF sequencing, and
dependency set are carried over verbatim; diffEditorRef joins the deps as
a stable ref object.
@coderabbitai

coderabbitai Bot commented Sep 19, 2026

Copy link
Copy Markdown
Contributor

Review Change StackReview Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Advanced

Run ID: eff0b1e8-c3f2-48e0-b1eb-9fe41f1c36f5

📥 Commits

Reviewing files that changed from the base of the PR and between 3e7da29 and 851e010.

📒 Files selected for processing (2)
  • src/renderer/src/components/editor/DiffViewer.tsx
  • src/renderer/src/components/editor/useDiffViewerFirstChangeAutoScroll.ts

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


📝 Walkthrough

Walkthrough

The change extracts DiffViewer’s inline first-change auto-scroll effect into useDiffViewerFirstChangeAutoScroll. The hook preserves model-key guards, cached-position and pending-comment handling, deferred centering, diff-update subscription, and cleanup. DiffViewer now supplies the required editor and scroll state to the hook.

Priority: ⬇️ Low

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 2 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly identifies the main change: extracting the diff first-change auto-scroll logic into a hook to resolve the main-branch line-limit failure.
Description check ✅ Passed The description is detailed and follows the template. It explains the user impact, implementation, motivation, alternatives, visual proof, testing, and compatibility considerations. It does not includ…
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
📝 Generate docstrings 💡
  • Commit to this branch
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ No new issues found.

Reviewed changes — a pure code-motion refactor that extracts the diff first-change auto-scroll effect out of DiffViewer.tsx so it drops back under oxlint's 400-line cap and unblocks main.

  • New useDiffViewerFirstChangeAutoScroll hook — the inline useEffect and its two refs were moved verbatim into useDiffViewerFirstChangeAutoScroll.ts; the only textual deltas are the pendingScrollForThisViewer → pendingScrollCommentId rename and diffEditorRef joining the dependency array (a stable useRef identity, so effect re-run triggers are unchanged).
  • DiffViewer.tsx call site — the hook is invoked at the same position as the removed effect, after the comment decorator and popover effect, preserving hook order and effect sequencing relative to the decorator's view zones. Now 390 raw lines.
  • No collateral cleanup needed — useRef, useEffect, and diffViewStateCache are all still used elsewhere in DiffViewer.tsx, so no imports were left dangling.

I compared the removed block against the new hook body line by line and found no logic difference, confirmed no remaining references to the old inline symbols, and ran oxlint on both files (0 warnings, 0 errors — the max-lines failure is gone).

Pullfrog  | View workflow run | Using DeepSeek Flash (free via Pullfrog for OSS) | 𝕏

@nwparker
nwparker merged commit b766f51 into main Sep 20, 2026
29 of 31 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.

1 participant