perf(editor): scope fold re-key to the edit (#142)
CI / ci (push) Canceled after 0s

With a fold open, every keystroke re-scanned the buffer, rebuilt the
full attributed string through setAttributedString (invalidating
TextKit layout for the ENTIRE document — O(n²) typing on folded
documents), and rasterized a chrome image the size of the document
(~35M pixels on a 700×50,000pt doc) to paint a handful of chevrons.

Single-line edits now re-key folds in place: a shouldChangeText
override records the edit (range + UTF-16 delta) and a pure rekey
function shifts surviving folds, kills folds whose marker line was
edited (un-hiding just that scope), and re-hides a line edited inside
a hidden scope — no parents rescan, no storage swap. Structural
edits, multi-line pastes, and wholesale loads keep the full pass; a
font probe at each survivor's scope head falls back to it whenever
hidden styling was wiped (the fence-flip restyle self-heals this way).

Chrome renders as one tiny cached layer per list parent (same glyph
rasters at ~15×18pt, geometry-flipped into the existing document-space
overlay) instead of the document-sized bitmap; an edit at/after every
parent line skips the rebuild outright, so writing at the document end
does zero geometry queries. Full-buffer scans per keystroke drop from
2–3 to one.

Finish-audit hardening: the single-line gate checks Character.isNewline
(a literal newline compare passes CRLF — one grapheme equal to neither
scalar), and apply() clears the recorded edit so structural indent/
outdent always takes the full pass (a childless unterminated block's
shift-tab is a single-line replacement that would otherwise keep a
dead fold's scope hidden — apparent line loss).

Closes #142
This commit is contained in:
AlexKucera committed 2026-11-03 10:40:14 +01:00
1 parent de1154dacd
commit cc317b65b9
9 files changed
+687 -114

No files matched your search

+7 -1
View File
@@ -283,7 +283,13 @@ struct ProseFoldRenderingTests {
// an edit above shifts every line down by 3 ("x\n\n" prefix)
textView.textStorage?.replaceCharacters(in: NSRange(location: 0, length: 0), with: "x\n\n")
textView.refreshFoldRendering()
#expect(textView.folds == [ProseListTextView.Fold(key: 3, lineText: "- parent")])
#expect(textView.folds == [
ProseListTextView.Fold(
key: parent.markerLine.location + 3,
lineText: "- parent",
scope: NSRange(location: parent.foldScope.location + 3, length: parent.foldScope.length),
),
])
let font = try #require(textView.textStorage?.attribute(.font, at: 13, effectiveRange: nil) as? NSFont)
#expect(font.pointSize < 1) // the shifted child stays hidden
}