Files
museion/MuseionTests/ProseListTests.swift
T
AlexKucera cc317b65b9
CI / ci (push) Canceled after 0s
perf(editor): scope fold re-key to the edit (#142)
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
2026-11-03 10:40:14 +01:00

307 lines
12 KiB
Swift

//
// ProseListTests.swift
// Museion
//
// #115 (S5): the prose-list model pins — item scan (nesting, lazy
// continuation, blanks, fences), Tab/⇧Tab deltas (the content-column
// rule), sibling moves (separators preserved, boundaries no-op), fold
// scopes — plus the view's fold rendering: the buffer never changes,
// hidden scopes carry the folded attributes, folds re-key after edits.
//
import AppKit
@testable import Museion
import Testing
struct ProseListTests {
// MARK: Scan
@Test func `item basics`() throws {
let item = try #require(ProseList.item(at: 0, in: "- a\n- b\n- c\n"))
#expect(item.indent == 0)
#expect(item.block == NSRange(location: 0, length: 4))
#expect(item.foldScope == nil)
#expect(item.markerEnd == 3)
}
@Test func `nested children and scope`() throws {
let text = "- a\n - a1\n - a2\n- b\n"
let parentItem = try #require(ProseList.item(at: 0, in: text))
#expect(parentItem.block == NSRange(location: 0, length: 18)) // "- a\n - a1\n - a2\n"
#expect(parentItem.foldScope == NSRange(location: 4, length: 14))
#expect(parentItem.hiddenLineCount == 2)
let secondItem = try #require(ProseList.item(at: 19, in: text))
#expect(secondItem.block == NSRange(location: 18, length: 4))
#expect(secondItem.foldScope == nil)
}
@Test func `lazy continuation belongs to item`() throws {
let parentItem = try #require(ProseList.item(at: 0, in: "- a\ncont\n- b\n"))
#expect(parentItem.block.length == 9) // "- a\n" + "cont\n"
#expect(parentItem.foldScope == nil) // indent-0 prose is not a fold child
}
@Test func `blank separated child folds blank too`() throws {
let text = "- a\n\n - a1\n- b\n"
let parentItem = try #require(ProseList.item(at: 0, in: text))
#expect(parentItem.block.length == 12) // "- a\n" + "\n" + " - a1\n"
#expect(parentItem.foldScope == NSRange(location: 4, length: 8)) // starts at the blank
#expect(parentItem.hiddenLineCount == 2)
}
@Test func `fence blocks item detection`() {
#expect(ProseList.item(at: 5, in: "```\n- x\n```\n- y\n") == nil)
#expect(ProseList.item(at: 13, in: "```\n- x\n```\n- y\n") != nil)
}
@Test func `caret in leading whitespace still finds item`() {
#expect(ProseList.item(at: 1, in: " - a\n") != nil)
}
@Test func `parents scan`() {
let parentsList = ProseList.parents(in: "- a\n - a1\n- b\n - b1\n")
#expect(parentsList.map(\.markerLine.location) == [0, 11])
#expect(parentsList.allSatisfy { $0.hiddenLineCount == 1 })
}
// MARK: Indent / outdent
@Test func `indent uses dash content column`() throws {
let text = "- a\n- b\n"
let item = try #require(ProseList.item(at: 5, in: text))
let edit = try #require(ProseList.indent(item, caret: 5, in: text))
#expect(edit.range == NSRange(location: 4, length: 4))
#expect(edit.replacement == " - b\n")
#expect(edit.caret == 7)
}
@Test func `indent uses ordered content column`() throws {
let text = "1. one\n1. two\n"
let item = try #require(ProseList.item(at: 8, in: text))
let edit = try #require(ProseList.indent(item, caret: 8, in: text))
#expect(edit.replacement == " 1. two\n") // "1. " content column = 3
}
@Test func `indent shifts subtree`() throws {
let text = "- a\n- b\n - b1\n"
let item = try #require(ProseList.item(at: 5, in: text))
let edit = try #require(ProseList.indent(item, caret: 5, in: text))
#expect(edit.replacement == " - b\n - b1\n")
}
@Test func `indent nests under previous sibling past its children`() throws {
let text = "- a\n - a1\n- b\n"
// walking back from b: a1 (deeper — skip), a (same indent) = sibling
let item = try #require(ProseList.item(at: 11, in: text))
let edit = try #require(ProseList.indent(item, caret: 11, in: text))
#expect(edit.replacement == " - b\n")
}
@Test func `indent boundary after paragraph noop`() throws {
let text = "intro\n\n- a\n- b\n"
let item = try #require(ProseList.item(at: 8, in: text))
#expect(ProseList.indent(item, caret: 8, in: text) == nil)
}
@Test func `indent first child noop`() throws {
let text = "- a\n - a1\n"
let item = try #require(ProseList.item(at: 5, in: text))
#expect(ProseList.indent(item, caret: 5, in: text) == nil)
}
@Test func `indent crosses lazy prose to sibling`() throws {
let text = "- a\ncont\n- b\n"
let item = try #require(ProseList.item(at: 9, in: text))
let edit = try #require(ProseList.indent(item, caret: 9, in: text))
#expect(edit.replacement == " - b\n")
}
@Test func `indent stops at standalone paragraph`() throws {
let text = "- a\n\npara\n\n- b\n"
let item = try #require(ProseList.item(at: 12, in: text))
#expect(ProseList.indent(item, caret: 12, in: text) == nil)
}
@Test func `outdent removes two and shifts caret`() throws {
let text = "- a\n - b1\n"
let item = try #require(ProseList.item(at: 5, in: text))
let edit = try #require(ProseList.outdent(item, caret: 7, in: text))
#expect(edit.range == NSRange(location: 4, length: 7))
#expect(edit.replacement == "- b1\n")
#expect(edit.caret == 5)
}
@Test func `outdent parks caret in stripped prefix`() throws {
let text = "- a\n - b1\n"
let item = try #require(ProseList.item(at: 5, in: text))
let edit = try #require(ProseList.outdent(item, caret: 5, in: text))
#expect(edit.replacement == "- b1\n")
#expect(edit.caret == 4) // caret was inside the stripped spaces
}
@Test func `outdent rides subtree`() throws {
let text = " - b\n - b1\n"
let item = try #require(ProseList.item(at: 2, in: text))
let edit = try #require(ProseList.outdent(item, caret: 4, in: text))
#expect(edit.replacement == "- b\n - b1\n")
}
@Test func `outdent at root noop`() throws {
let item = try #require(ProseList.item(at: 0, in: "- a\n"))
#expect(ProseList.outdent(item, caret: 0, in: "- a\n") == nil)
}
// MARK: Moves
@Test func `move down swaps sibling`() throws {
let text = "- a\n- b\n- c\n"
let item = try #require(ProseList.item(at: 0, in: text))
let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text))
#expect(edit.range == NSRange(location: 0, length: 8))
#expect(edit.replacement == "- b\n- a\n")
#expect(edit.caret == 5)
}
@Test func `move down rides subtree`() throws {
let text = "- a\n - a1\n- b\n"
let item = try #require(ProseList.item(at: 0, in: text))
let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text))
#expect(edit.replacement == "- b\n- a\n - a1\n")
#expect(edit.caret == 5)
}
@Test func `move down keeps sibling subtree with sibling`() throws {
// #115 audit pin: the SIBLING's children must ride the sibling, not
// get stolen by the moved item (marker-line swap bug).
let text = "- a\n- b\n - b1\n- c\n"
let item = try #require(ProseList.item(at: 0, in: text))
let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text))
#expect(edit.replacement == "- b\n - b1\n- a\n")
}
@Test func `move up keeps sibling subtree with sibling`() throws {
let text = "- a\n - a1\n- b\n"
let item = try #require(ProseList.item(at: 11, in: text))
let edit = try #require(ProseList.move(item, caret: 12, direction: -1, in: text))
#expect(edit.replacement == "- b\n- a\n - a1\n")
#expect(edit.caret == 1)
}
@Test func `move up keeps sibling lazy line with sibling`() throws {
let text = "- a\ncont\n- b\n"
let item = try #require(ProseList.item(at: 9, in: text))
let edit = try #require(ProseList.move(item, caret: 9, direction: -1, in: text))
#expect(edit.replacement == "- b\n- a\ncont\n")
}
@Test func `indent leaves blank lines empty`() throws {
// #115 audit pin: blank lines inside the block must not gain
// injected leading spaces.
let text = "- p\n- a\n\n - a1\n"
let item = try #require(ProseList.item(at: 5, in: text))
let edit = try #require(ProseList.indent(item, caret: 6, in: text))
#expect(edit.replacement == " - a\n\n - a1\n")
}
@Test func `move up swaps with previous`() throws {
let text = "- a\n- b\n- c\n"
let item = try #require(ProseList.item(at: 5, in: text))
let edit = try #require(ProseList.move(item, caret: 5, direction: -1, in: text))
#expect(edit.range == NSRange(location: 0, length: 8))
#expect(edit.replacement == "- b\n- a\n")
#expect(edit.caret == 1)
}
@Test func `move boundaries noop`() throws {
let text = "- a\n- b\n"
let firstItem = try #require(ProseList.item(at: 0, in: text))
let lastItem = try #require(ProseList.item(at: 5, in: text))
#expect(ProseList.move(firstItem, caret: 0, direction: -1, in: text) == nil)
#expect(ProseList.move(lastItem, caret: 5, direction: 1, in: text) == nil)
}
@Test func `move at deeper boundary noop`() throws {
// a1's next list line is "- b" at a shallower indent — not a sibling
let boundaryText = "- a\n - a1\n- b\n"
let deepItem = try #require(ProseList.item(at: 5, in: boundaryText))
#expect(ProseList.move(deepItem, caret: 5, direction: 1, in: boundaryText) == nil)
}
@Test func `move preserves blank separators`() throws {
let text = "- a\n\n- b\n\n- c\n"
let item = try #require(ProseList.item(at: 0, in: text))
let edit = try #require(ProseList.move(item, caret: 1, direction: 1, in: text))
#expect(edit.replacement == "- b\n\n- a\n")
}
@Test func `move stops at paragraph boundary`() throws {
let paraText = "- a\n\npara\n\n- b\n"
let paraItem = try #require(ProseList.item(at: 0, in: paraText))
#expect(ProseList.move(paraItem, caret: 1, direction: 1, in: paraText) == nil)
}
}
@MainActor
struct ProseFoldRenderingTests {
private func makeView(_ text: String) -> ProseListTextView {
let textView = ProseListTextView()
textView.isRichText = false
textView.textStorage?.setAttributedString(ProsePresenter.attribute(text))
return textView
}
@Test func `fold hides scope without touching buffer`() throws {
let text = "- parent\n - child one\n - child two\n- next\n"
let textView = makeView(text)
let parent = try #require(ProseList.parents(in: text).first)
textView.toggleFold(parent)
#expect(textView.string == text) // folding never edits the buffer
#expect(textView.folds.count == 1)
// hidden scope carries the folded attributes
let hiddenFont = try #require(
textView.textStorage?.attribute(.font, at: parent.foldScope.location, effectiveRange: nil) as? NSFont,
)
#expect(hiddenFont.pointSize < 1)
// the parent line itself stays body-size
let bodyFont = try #require(textView.textStorage?.attribute(.font, at: 0, effectiveRange: nil) as? NSFont)
#expect(bodyFont.pointSize > 10)
// unfold restores body presentation
let parentAgain = try #require(ProseList.parents(in: textView.string).first)
textView.toggleFold(parentAgain)
let restored = try #require(
textView.textStorage?.attribute(.font, at: parent.foldScope.location, effectiveRange: nil) as? NSFont,
)
#expect(restored.pointSize > 10)
#expect(textView.folds.isEmpty)
}
@Test func `folds rekey after edit above`() throws {
let text = "- parent\n - child\n"
let textView = makeView(text)
let parent = try #require(ProseList.parents(in: text).first)
textView.toggleFold(parent)
// 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: 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
}
@Test func `fold dies when marker text vanishes`() throws {
let text = "- parent\n - child\n"
let textView = makeView(text)
let parent = try #require(ProseList.parents(in: text).first)
textView.toggleFold(parent)
textView.textStorage?.replaceCharacters(in: NSRange(location: 0, length: 8), with: "# head\n")
textView.refreshFoldRendering()
#expect(textView.folds.isEmpty)
}
}