Skip to content

Avoid scrolling whole selections to keep undo responsive#108

Open
brandonbloom wants to merge 1 commit intokrzyzanowskim:mainfrom
brandonbloom:perf/undo-scroll-selection
Open

Avoid scrolling whole selections to keep undo responsive#108
brandonbloom wants to merge 1 commit intokrzyzanowskim:mainfrom
brandonbloom:perf/undo-scroll-selection

Conversation

@brandonbloom
Copy link

Undoing a delete after selecting a very large range can become slow on AppKit. The expensive work happens in STTextView.layout() when the view tries to make the restored selection visible. The existing code always asks TextKit for the frame of the entire current selection, so undoing Cmd+A/Delete effectively requests geometry for the whole document.

Make the scroll-to-selection path target only the geometry that is needed:

  • if the selection already intersects the current viewport, do not scroll
  • if the selection is outside the viewport, scroll only the nearest selection edge instead of the entire selection range
  • share the same gutter-aware rect adjustment logic for range-based and location-based scrolling

Add focused AppKit tests for the selection-to-scroll-target logic in UndoTests so this behavior is covered on the branch without depending on any other test harness changes.

This keeps undo responsive for large selection restores while preserving the existing selection visibility behavior.

Undoing a delete after selecting a very large range can become slow on AppKit.
The expensive work happens in STTextView.layout() when the view tries to make
the restored selection visible. The existing code always asks TextKit for the
frame of the entire current selection, so undoing Cmd+A/Delete effectively
requests geometry for the whole document.

Make the scroll-to-selection path target only the geometry that is needed:
- if the selection already intersects the current viewport, do not scroll
- if the selection is outside the viewport, scroll only the nearest selection
  edge instead of the entire selection range
- share the same gutter-aware rect adjustment logic for range-based and
  location-based scrolling

Add focused AppKit tests for the selection-to-scroll-target logic in UndoTests
so this behavior is covered on the branch without depending on any other test
harness changes.

This keeps undo responsive for large selection restores while preserving the
existing selection visibility behavior.
ruslanhirychau added a commit to ruslanhirychau/STTextView that referenced this pull request Mar 16, 2026
Optimize scroll-to-selection logic in layout() to avoid unnecessary
viewport jumps:
- Skip scrolling if selection already intersects visible viewport
- Scroll to nearest selection edge instead of entire range
- Extract adjustedScrollRect() to deduplicate gutter-aware rect logic

Based on PR krzyzanowskim#108 by brandonbloom.

Co-Authored-By: Claude Opus 4.6 <[email protected]>
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