Commit 13a0d02bad

13a0d02bad01156ea839a83ba1fee10382be2732

parent: 26d74b83c5

Verified · cmc

cmc <hello@cleberg.net> · 2026-11-22 12:55 UTC

Apply indents and roles in one ascending pass

restyle added a paragraph style per line and then each role from the
start of the range, so every change moved the attribute runs after it.
Both now go on in ascending order. Styling 1, 2, 4 and 10 MB after open
takes 0.22, 0.45, 0.91 and 2.3 s (1.4, 5.9 and 24 s before for 1, 2 and
4 MB).

Closes #189

Layout: unified · split

Sources/OrgEditorAppKit/OrgEditor+Styling.swift +22 −3
@@ -26,14 +26,32 @@ extension OrgEditor {
2626 }
2727 return
2828 }
29 if indented {
30 for line in Presentation.lineIndents(document.tree, in: range) {
29 // Indents and roles go on together in ascending order, so each change lands at the end
30 // of the runs made so far instead of moving the ones after it.
31 var indents = (indented ? Presentation.lineIndents(document.tree, in: range) : [])[...]
32 func indent(before offset: Int) {
33 while let line = indents.first, line.range.lowerBound <= offset {
3134 storage.addAttribute(.paragraphStyle, value: paragraphStyle(line.firstLine, line.wrapped), range: NSRange(line.range))
35 indents.removeFirst()
3236 }
3337 }
3438 let revealed = caretLine
3539 var scriptExclusions: [Range<Int>] = []
36 for run in Presentation.styles(document.tree, in: range, highlighter: highlighter) {
40 // Outer before inner still: an outer role never starts after an inner one, and the
41 // index keeps the order of roles that start together. A src block's code runs come in
42 // the highlighter's order, where later wins, so they sort as one.
43 let styles = Presentation.styles(document.tree, in: range, highlighter: highlighter)
44 var starts: [Int] = []
45 for (index, run) in styles.enumerated() {
46 if case .syntax = run.role, index > 0, case .syntax = styles[index - 1].role {
47 starts.append(starts[index - 1])
48 } else {
49 starts.append(run.range.lowerBound)
50 }
51 }
52 for index in styles.indices.sorted(by: { (starts[$0], $0) < (starts[$1], $1) }) {
53 let run = styles[index]
54 indent(before: run.range.lowerBound)
3755 guard let clipped = clip(run.range, to: range) else { continue }
3856 if Presentation.skipsLeadingStars(run.role, indented: indented, hideStars: hideStars) { continue }
3957 if !showMarkup, Presentation.hidesWithMarkup(run.role, options: options), NSIntersectionRange(clipped, revealed).length == 0 {
@@ -43,6 +61,7 @@ extension OrgEditor {
4361 }
4462 if Presentation.excludesScripts(run.role) { scriptExclusions.append(run.range) }
4563 }
64 indent(before: range.upperBound)
4665 if theme.mixedPitch {
4766 for prefix in Presentation.fixedPitchPrefixes(storage.string as NSString, in: range) {
4867 guard let clipped = clip(prefix, to: range) else { continue }
Tests/OrgEditorAppKitTests/EditorTests.swift +21
@@ -474,6 +474,27 @@ struct RestyleFuzzTests {
474474 }
475475}
476476
477@MainActor
478struct SourceHighlightTests {
479 struct Overlapping: CodeHighlighter {
480 func highlights(language: String, code: String) -> [(range: Range<Int>, category: SyntaxCategory)] {
481 [(5..<7, .string), (0..<7, .keyword), (2..<4, .comment)]
482 }
483 }
484
485 /// Later runs win over earlier ones, wherever they start.
486 @Test func laterHighlighterRunsWin() throws {
487 let text = "* a\n#+begin_src sh\necho hi\n#+end_src\n"
488 let editor = OrgEditor(document: DocumentState(bytes: Array(text.utf8)), highlighter: Overlapping())
489 editor.finishStyling()
490 let storage = try #require(editor.textView.textStorage)
491 let code = (text as NSString).range(of: "echo").location
492 func color(_ offset: Int) -> NSColor? { storage.attribute(.foregroundColor, at: code + offset, effectiveRange: nil) as? NSColor }
493 #expect(color(0) == color(5) && color(0) == color(4))
494 #expect(color(2) != color(0) && color(2) == color(3))
495 }
496}
497
477498@MainActor
478499struct InitialStylingTests {
479500 @Test func stylesTheFirstScreenThenTheRest() throws {