Skip to content

perf(editor): redraw only the lines a caret move or edit changes - #4570

Open
datlechin wants to merge 2 commits into
mainfrom
perf/editor-partial-redraw
Open

datlechin wants to merge 2 commits into
mainfrom
perf/editor-partial-redraw

Conversation

@datlechin

@datlechin datlechin commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Summary

  • A caret move or selection change redraws only the lines the selections left and the lines they are on now, as NSTextView redraws only the lines an insertion point leaves and enters, instead of setNeedsDisplay(bounds). A draw pass records where it put the selections, so a layout pass that moved their lines is still covered on the next move. An edit invalidates the selections too, which a text view that is not first responder used to skip.
  • The statement band invalidates the lines it leaves and the lines it covers, two line lookups, instead of the whole view, and draws only the part of the statement on the lines in the dirty rect, as the selection fill already does.
  • The gutter draws the caret line's band when its row is in the dirty rect rather than in the visible text range, redraws only the selection's old and new rows on a selection change, and on an edit only the edited line, or from it down when the edit adds or removes a line break or lines wrap. Line numbers are shaped once per number and resolved colour and reused. The selected line numbers after Select All come from the drawn lines only, not from every line of the document.
  • Fixes a bug found on the way: didUpdate = didUpdate || repositionCursorSelection(...) stopped repositioning carets after the first one that moved, so with several carets the later ones stayed where they were.

Measured in the unit tests (900 x 6,000 pt view, 14 pt monospaced):

What Before After
Text view area invalidated per caret move the whole view, 900 x 6,000 two rects of one line height
Lines walked for the gutter's selected numbers, per draw after Select All of 2,000 lines 2,000 the 10 lines drawn
CTLines created per gutter draw one per visible number none once each number is cached

Left for a follow-up, in the issue comment: dropping the gutter redraw on each scroll frame, and the 50 ms synthetic frame-change notification after an edit.

Test plan

  • SelectionBehaviourTests: a caret move from line 1 to line 201 invalidates only rects one line high, covering both lines (fails on main, which invalidates the bounds); several carets moved together each land on their new offset (fails on main).
  • GutterHighlightTests: a ranged selection marks only the lines inside the drawn range.
  • verify.sh package TableProEditor, the whole package: 931 passed (TableProTextEngineTests 477, TableProEditorKitTests 454).

Closes #4536

A caret move invalidated the text view's bounds, the statement band's change
redrew the whole view, and the gutter redrew every row on each selection
change and edit. Now the selection manager invalidates the lines the
selections left and the lines they are on, the band invalidates its old and
new lines and draws only the lines in the dirty rect, and the gutter redraws
the selection's old and new rows and, on an edit, the edited line or every
line below it when lines move.

The gutter's caret band is tested against the dirty rect instead of the
visible text, selected line numbers after Select All come from the drawn
lines only, and shaped line numbers are cached by number and colour.

Fixes `didUpdate || reposition(...)`, which stopped repositioning carets
after the first one that moved.

This branch has not been deployed

No deployments
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.

perf(editor): redraw only the changed lines on a caret move or edit

1 participant