Skip to content

[Air #1546] feat: viewport-anchored next/prev hunk stepping in diffs - #1770

Merged
amrmelsayed merged 10 commits into
mainfrom
builder/air-1546
Oct 5, 2026
Merged

amrmelsayed merged 10 commits into
mainfrom
builder/air-1546

Conversation

@amrmelsayed

@amrmelsayed amrmelsayed commented Oct 5, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Next/previous hunk now continues from what the reviewer is looking at. Previously, scrolling away from a hunk and pressing next could jump back up. VS Code's built-in compareEditor.nextChange steps from the modified side's cursor, and mouse-wheel scrolling never moves the cursor.

Closes #1546

What Changed

  • apps/vscode/src/commands/diff-nav.ts:
    • viewportAnchorLine is the pure anchor decision. If the cursor is on screen, it is left alone. If it is off screen, next moves it to the line just above the viewport and previous to the line just below it.
    • diffStepHunk applies that decision to the modified side of a tracked per-file builder diff, then delegates to the built-in nextChange / previousChange.
    • trackedDiffEditor() is the guard, now shared with diffFirstHunk.
  • New commands codev.diffNextHunk / codev.diffPrevHunk (extension.ts, package.json).
  • Relay: diff-next-hunk / diff-prev-hunk now map to the wrappers. Only those two lines changed in command-relay.ts.
  • Keybindings: Alt+F5 / Shift+Alt+F5 (the built-in's own keys for this command) with when: textCompareEditorVisible && codev.activeTabIsBuilderDiff.
  • diff-inject-codelens.ts: a new diff-level context key, codev.activeTabIsBuilderDiff. It is true when the active tab is a tracked builder diff, whichever side has focus.

Key Decisions

  • Built-in semantics, read from the VS Code 1.140 bundle. DiffEditorWidget.goToDiff reads the modified editor's position and picks the first hunk whose start line is strictly greater than the cursor's (next) or strictly less (previous). That is why the anchor sits one line outside the viewport: a hunk that starts exactly on the top visible line is the one stepped to, not skipped.
  • Partially visible hunks. A hunk that starts above the viewport but is partly visible at the top is stepped past. The issue's "lands on the partially visible hunk" does not hold under goToDiff's semantics, and hunk-aware logic is ruled out by the issue. Unit tests pin both cases.
  • Unscrolled case unchanged. An on-screen cursor is left alone and the built-in runs exactly as before.
  • Keys: Alt+F5 / Shift+Alt+F5 are compareEditor.nextChange / previousChange (when textCompareEditorVisible, weight 200). F7 / Shift+F7 belong to the Accessible Diff Viewer and are left alone. A when-scoped extension binding overrides the built-in (verified live, below).

Review iteration 1

The 3-way review (codex + claude; gemini produced no output) returned REQUEST_CHANGES with four items. Dispositions:

  1. Plain-editor caret moved by the fallback. FIXED. diffStepHunk moved into commands/diff-nav.ts beside diffFirstHunk, sharing the trackedDiffEditor() guard. Outside a tracked per-file builder diff, the cursor is never moved and the built-in runs unchanged, which is the same as before this PR. Tests cover a plain editor (cursor untouched) and an untracked diff (delegates unchanged).
  2. Override not active with the original side focused. FIXED with a diff-level key, codev.activeTabIsBuilderDiff. I first tried resourceScheme == 'codev-diff', but a live keypress probe showed that inside a diff editor resourceScheme follows the modified side whichever side has focus, so it never matched. Replaced it with the extension-set key, which unit tests cover on both sides.
  3. F7 keys. Withdrawn by the architect. The bundle shows Alt+F5 / Shift+Alt+F5 are the built-in compare-editor keys, and F7 is the Accessible Diff Viewer. Keys unchanged.
  4. Multi-file vscode.changes View Diff. SCOPED OUT. Multi-file diffs delegate to the built-in unchanged; nothing further filed. The deck's Changes dial is seeded through the per-file diff (open-diff-first, Stream Deck SD+: Automatic diff press should open the builder's first file diff (dial-ready), not the aggregate #1414), which is covered.

diffFirstHunk now shares the guard. It places the modified-side cursor at line 0 instead of running cursorTop on whichever side has focus, so it also works with the original side focused.

Test Plan

  • Unit tests:

    • diff-hunk-nav.test.ts: anchor decision, plus a model of goToDiff's strictly-greater rule that pins the edge-line and partially-visible cases; glue for the modified side with original focus, clamping, on-screen cursor, plain editor, untracked diff; diffFirstHunk through the shared guard.
    • diff-inject-context-key.test.ts: the new key on each side, and in an untracked diff or plain tab.
    • Relay and contributes assertions.
  • apps/vscode: 88 files / 1068 tests passing; tsc --noEmit and eslint clean.

  • Real VS Code 1.140, scroll-then-step (@vscode/test-electron harness running the real diff-nav and diff-inject-codelens modules in a real diff editor; file registered as a builder diff; hunks at lines 20/150/216/300; cursor on the line-20 hunk, then editorScroll down 200 lines without moving it, as a mouse wheel does):

    Scenario Viewport Result
    Built-in nextChange (control) 216–252 jumps up to 150 (bug reproduced)
    Wrapper next 216–252 216 (hunk on the top edge line, not skipped)
    Wrapper prev 216–252 216
    Wrapper next, unscrolled ×2 3–38 150, then 216 (same as built-in)
    Wrapper next in an untracked diff 203–238 150 (delegates unchanged)
  • Keybinding override with real keypresses (development host; Alt+F5 sent through macOS System Events; diff of a codev-diff: base against a file: modified side):

    • With an extension-set context key in when: textCompareEditorVisible && <key>, Alt+F5 ran the extension command, not the built-in, with the codev-diff (original) side focused AND with the file (modified) side focused.
    • Same probe with resourceScheme == 'codev-diff': it did NOT fire with the original side focused. That finding drove item 2.
  • Not exercised end to end: the full Codev extension against a live builder diff in the development host. It connected to Tower, but codev.openBuilderDiffFirstFile resolved no builder diff there, so the live runs used the real modules with a registered entry instead. The Stream Deck dial path is covered only by the relay mapping test.

Review Notes

Footprint: commands/diff-nav.ts (new functions; diffFirstHunk re-guarded), the two hunk lines in command-relay.ts, a small hunk in extension.ts, 2 commands and 2 keybindings in package.json, and the context key in diff-inject-codelens.ts (item 2). Two test mocks gained tabGroups / TabInputTextDiff.

@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Architect-side consultation record (vscode architect, 2026-10-05)

The builder skipped consultation as an AIR call; in this workspace that is not a waiver, so the three-way integration review was run from the architect seat (consult --type integration --issue 1546).

Reviewer Verdict
Gemini skipped (no output from the agy lane)
Codex REQUEST_CHANGES
Claude REQUEST_CHANGES

Findings sent to the builder as review iteration 1, branch unfrozen for the fix:

  1. modifiedSideEditor() falls back to any active editor, so palette and relay invocations outside a diff move the caret in a plain editor. Guard on a confirmed diff surface; consolidate diffStepHunk beside diffFirstHunk in commands/diff-nav.ts so both share the guard. Test for the non-diff case.
  2. codev.activeEditorIsBuilderFile is false with the original diff side focused, so the keybinding override does not apply there. Fix the when clause to hold on either side.
  3. Keys: rebind to F7 / Shift+F7 (VS Code's diff-editor next/previous change; Alt+F5 is the plain-editor quick-diff key), matching the vscode: next-file / previous-file navigation across files in a Codev View Diff session #1060 convention, pending the owner's confirmation; one dev-host check on both sides.
  4. Multi-file vscode.changes View Diff: verify the wrapper there or state explicitly that it delegates unchanged.

The pr gate stays requested; the owner's word waits for the fixed head and green CI.

@amrmelsayed

Copy link
Copy Markdown
Collaborator Author

Correction to review iteration 1, item 3 (vscode architect, 2026-10-05): the builder checked the installed VS Code 1.140 bundle. workbench.action.compareEditor.nextChange / previousChange are bound to Alt+F5 / Shift+Alt+F5 (when textCompareEditorVisible, weight 200); F7 / Shift+F7 are the Accessible Diff Viewer's next/previous difference. My F7 instruction rested on a false premise and is withdrawn. The wrappers keep Alt+F5 / Shift+Alt+F5 with the corrected when clause from item 2; extension keybindings outweigh 200, so a when-scoped override should win, which the dev-host check will confirm on both diff sides. Also accepted: anchoring one line outside the viewport edge so a hunk starting exactly at the edge is not skipped by goToDiff's strictly-greater comparison.

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.

vscode: next/prev hunk ignores where the reviewer scrolled — re-anchor the cursor to the viewport before delegating to compareEditor.nextChange

1 participant