Skip to content

fix(editor): keep the caret in view after Cmd+Up, Cmd+Down and paste - #3242

Merged
datlechin merged 2 commits into
mainfrom
fix/editor-cmd-arrow-scroll
Oct 2, 2026
Merged

datlechin merged 2 commits into
mainfrom
fix/editor-cmd-arrow-scroll

Conversation

@datlechin

Copy link
Copy Markdown
Member

Why

In a long query, Cmd+Up, Cmd+Down and long pastes moved the caret but not the view. The editor did scroll, then its SwiftUI wrapper scrolled it back: it compared the binding with the editor's live scroll position, which the editor writes back a run-loop turn late, so a layout pass in between read the old value as a request to return to it.

Change

  • Cmd+Up, Cmd+Down, Cmd+Shift+Up/Down and long pastes keep the caret in view. The wrapper applies a state field only when the binding's owner changed it, as the text binding already does.
  • Use Selection for Find no longer reverts to the previous term once the caret moves (same cause).
  • Clear Query, favorites, history and AI fixes always reach the editor. A caret key that moved nothing left the text-sync latch set, so the next text change was skipped.
  • Run, Explain and AI actions use the caret the editor shows after find navigation or a jump to a result's statement. The focus guard that dropped it was a workaround for the stale push above.

Verified

SourceEditorStateSyncTests (6 of 7 fail on main) and SQLEditorCursorPropagationTests (fails on main). Each flow checked on a Debug build.

Screenshots

Before: Cmd+Down, caret on line 4000 After
Before After

Not in this PR

  • No UI test: the bug needs the display cycle to run before a deferred block, which XCUITest cannot order; the package test forces it.
  • Two find-panel bugs now decide what Run acts on, since Run follows the visible selection: Replace shifts later matches the wrong way (FindPanelViewModel+Replace.swift:85), and matches go stale after the document is replaced. Separate causes, left for a follow-up.

Fixes #3239

SourceEditor compared its state binding with the editor's live values. The
editor writes scroll and find state back a run-loop turn late, so a layout
pass in between pushed the old scroll position back. State fields are now
diffed against the value the coordinator last synced.

State writes no longer set the text-sync latch, which an unchanged cursor
write left set, dropping the host's next text change. SQLEditorView drops
the focus guard that worked around the stale push, so Run, Explain and AI
use the caret after find navigation.

Fixes #3239
Autocomplete UI tests on CI committed the wrong keyword after the last
typed character, which points at a state push in the update pass right
after a caret write. The latch skipped that pass before this branch.
Changed state writes set it again, and only an unchanged write, which
runs no update pass to clear it, leaves it alone.
@datlechin
datlechin merged commit aa2419d into main Oct 2, 2026
15 checks passed
@datlechin
datlechin deleted the fix/editor-cmd-arrow-scroll branch October 2, 2026 13:11
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.

Scrolling to the very beginning/end doesn't scroll the editor

1 participant