Editor: style only the first chunk at open, and in one ascending pass !457
3 files changed, +103 −11
Layout: unified · split
Sources/OrgEditorAppKit/OrgEditor+Styling.swift +22 −3
| @@ -26,14 +26,32 @@ extension OrgEditor { | |||
| 26 | } | 26 | } |
| 27 | return | 27 | return |
| 28 | } | 28 | } |
| 29 | if indented { | 29 | // Indents and roles go on together in ascending order, so each change lands at the end |
| 30 | for line in Presentation.lineIndents(document.tree, in: range) { | 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 { | ||
| 31 | storage.addAttribute(.paragraphStyle, value: paragraphStyle(line.firstLine, line.wrapped), range: NSRange(line.range)) | 34 | storage.addAttribute(.paragraphStyle, value: paragraphStyle(line.firstLine, line.wrapped), range: NSRange(line.range)) |
| 35 | indents.removeFirst() | ||
| 32 | } | 36 | } |
| 33 | } | 37 | } |
| 34 | let revealed = caretLine | 38 | let revealed = caretLine |
| 35 | var scriptExclusions: [Range<Int>] = [] | 39 | 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) | ||
| 37 | guard let clipped = clip(run.range, to: range) else { continue } | 55 | guard let clipped = clip(run.range, to: range) else { continue } |
| 38 | if Presentation.skipsLeadingStars(run.role, indented: indented, hideStars: hideStars) { continue } | 56 | if Presentation.skipsLeadingStars(run.role, indented: indented, hideStars: hideStars) { continue } |
| 39 | if !showMarkup, Presentation.hidesWithMarkup(run.role, options: options), NSIntersectionRange(clipped, revealed).length == 0 { | 57 | if !showMarkup, Presentation.hidesWithMarkup(run.role, options: options), NSIntersectionRange(clipped, revealed).length == 0 { |
| @@ -43,6 +61,7 @@ extension OrgEditor { | |||
| 43 | } | 61 | } |
| 44 | if Presentation.excludesScripts(run.role) { scriptExclusions.append(run.range) } | 62 | if Presentation.excludesScripts(run.role) { scriptExclusions.append(run.range) } |
| 45 | } | 63 | } |
| 64 | indent(before: range.upperBound) | ||
| 46 | if theme.mixedPitch { | 65 | if theme.mixedPitch { |
| 47 | for prefix in Presentation.fixedPitchPrefixes(storage.string as NSString, in: range) { | 66 | for prefix in Presentation.fixedPitchPrefixes(storage.string as NSString, in: range) { |
| 48 | guard let clipped = clip(prefix, to: range) else { continue } | 67 | guard let clipped = clip(prefix, to: range) else { continue } |
Sources/OrgEditorAppKit/OrgEditor.swift +12 −8
| @@ -110,19 +110,22 @@ public final class OrgEditor: NSObject { | |||
| 110 | textContentStorage.delegate = self | 110 | textContentStorage.delegate = self |
| 111 | 111 | ||
| 112 | if isOrg { shaded.set(Presentation.shadedBlocks(document.tree)) } | 112 | if isOrg { shaded.set(Presentation.shadedBlocks(document.tree)) } |
| 113 | let startup = isOrg ? Presentation.startupDisplay(document.tree, options: options) : nil | ||
| 114 | if let startup { | ||
| 115 | // What styling reads, set before the text loads: the first chunk and the | ||
| 116 | // background passes style with it. | ||
| 117 | indented = startup.indented | ||
| 118 | hideStars = startup.hideStars | ||
| 119 | shrunk = ShrunkColumns.startup(document.tree) | ||
| 120 | showsImages = startup.inlineImages | ||
| 121 | showsLatex = startup.latexPreview | ||
| 122 | } | ||
| 113 | load(document.text) | 123 | load(document.text) |
| 114 | // Files open at their start, as in Emacs; replacing the storage leaves it at the end. | 124 | // Files open at their start, as in Emacs; replacing the storage leaves it at the end. |
| 115 | textView.setSelectedRange(NSRange(location: 0, length: 0)) | 125 | textView.setSelectedRange(NSRange(location: 0, length: 0)) |
| 116 | if isOrg { | 126 | if let startup { |
| 117 | let startup = Presentation.startupDisplay(document.tree, options: options) | ||
| 118 | indented = startup.indented | ||
| 119 | hideStars = startup.hideStars | ||
| 120 | if startup.folds != FoldState() { setFolds(startup.folds, wrappers: startup.wrappers) } | 127 | if startup.folds != FoldState() { setFolds(startup.folds, wrappers: startup.wrappers) } |
| 121 | shrunk = ShrunkColumns.startup(document.tree) | ||
| 122 | if startup.inlineImages { showsImages = true } | ||
| 123 | if startup.latexPreview { showsLatex = true } | ||
| 124 | if startup.alignTables { _ = perform(AlignAllTables()) } | 128 | if startup.alignTables { _ = perform(AlignAllTables()) } |
| 125 | if !indented || !hideStars { restyleOutsideEditing(0..<utf16Length) } | ||
| 126 | } | 129 | } |
| 127 | spellChecking = options.spellCheck | 130 | spellChecking = options.spellCheck |
| 128 | truncatesLines = options.startupTruncated | 131 | truncatesLines = options.startupTruncated |
| @@ -263,6 +266,7 @@ public final class OrgEditor: NSObject { | |||
| 263 | view.updateGutter() | 266 | view.updateGutter() |
| 264 | } | 267 | } |
| 265 | styledUpTo = 0 | 268 | styledUpTo = 0 |
| 269 | revealedLine = caretLine | ||
| 266 | styleNextChunk(Self.firstStyleChunk) | 270 | styleNextChunk(Self.firstStyleChunk) |
| 267 | scheduleStyling() | 271 | scheduleStyling() |
| 268 | } | 272 | } |
Tests/OrgEditorAppKitTests/EditorTests.swift +69
| @@ -474,6 +474,27 @@ struct RestyleFuzzTests { | |||
| 474 | } | 474 | } |
| 475 | } | 475 | } |
| 476 | 476 | ||
| 477 | @MainActor | ||
| 478 | struct 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 | |||
| 477 | @MainActor | 498 | @MainActor |
| 478 | struct InitialStylingTests { | 499 | struct InitialStylingTests { |
| 479 | @Test func stylesTheFirstScreenThenTheRest() throws { | 500 | @Test func stylesTheFirstScreenThenTheRest() throws { |
| @@ -488,6 +509,54 @@ struct InitialStylingTests { | |||
| 488 | #expect(h.textView.textStorage!.isEqual(to: Harness(h.string).textView.textStorage!)) | 509 | #expect(h.textView.textStorage!.isEqual(to: Harness(h.string).textView.textStorage!)) |
| 489 | } | 510 | } |
| 490 | 511 | ||
| 512 | @Test func openingLeavesTextPastTheFirstChunkUnstyled() throws { | ||
| 513 | let text = String(repeating: "** heading *bold*\nbody text\n", count: 4_000) | ||
| 514 | let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false) | ||
| 515 | let storage = try #require(h.textView.textStorage) | ||
| 516 | var unstyled = NSRange() | ||
| 517 | _ = storage.attributes(at: h.editor.styledUpTo, longestEffectiveRange: &unstyled, in: NSRange(location: 0, length: storage.length)) | ||
| 518 | #expect(unstyled == NSRange(location: h.editor.styledUpTo, length: storage.length - h.editor.styledUpTo)) | ||
| 519 | } | ||
| 520 | |||
| 521 | /// `#+STARTUP` display options hold in the first chunk before any background pass, and in | ||
| 522 | /// the rest after them. | ||
| 523 | @Test(arguments: [ | ||
| 524 | "#+STARTUP: noindent\n", "#+STARTUP: noindent hidestars\n", "#+STARTUP: indent\n", "#+STARTUP: latexpreview\nInline $x^2$ here.\n", | ||
| 525 | "#+STARTUP: shrink\n| <3> | b |\n| long cell | c |\n", "", | ||
| 526 | ]) func startupDisplayAppliesToEveryChunk(head: String) throws { | ||
| 527 | let text = head + String(repeating: "** heading *bold*\nbody text\n", count: 4_000) + "Last $y^2$ line\n" | ||
| 528 | let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false) | ||
| 529 | let storage = try #require(h.textView.textStorage) | ||
| 530 | let length = storage.length | ||
| 531 | // Without the preview boxes, which are made anew by each styling. | ||
| 532 | func styled() -> NSAttributedString { | ||
| 533 | let copy = NSMutableAttributedString(attributedString: storage.attributedSubstring(from: NSRange(location: 0, length: h.editor.styledUpTo))) | ||
| 534 | copy.removeAttribute(.orgLatex, range: NSRange(location: 0, length: copy.length)) | ||
| 535 | return copy | ||
| 536 | } | ||
| 537 | func check() { | ||
| 538 | let before = styled() | ||
| 539 | h.editor.restyleOutsideEditing(0..<h.editor.styledUpTo) | ||
| 540 | #expect(styled().isEqual(to: before)) | ||
| 541 | } | ||
| 542 | #expect(h.editor.styledUpTo < length) | ||
| 543 | check() | ||
| 544 | h.drainStyling() | ||
| 545 | #expect(h.editor.styledUpTo == length) | ||
| 546 | check() | ||
| 547 | |||
| 548 | let ns = text as NSString | ||
| 549 | for needle in ["$x^2$", "$y^2$"] where head.contains("latexpreview") { | ||
| 550 | #expect(storage.attribute(.orgLatex, at: ns.range(of: needle).location, effectiveRange: nil) != nil) | ||
| 551 | } | ||
| 552 | for needle in [ns.range(of: "** heading"), ns.range(of: "** heading", options: .backwards)] { | ||
| 553 | let stars = storage.attribute(.foregroundColor, at: needle.location, effectiveRange: nil) as? NSColor | ||
| 554 | #expect((stars == .clear) == !head.contains("noindent\n")) | ||
| 555 | let body = storage.attribute(.paragraphStyle, at: NSMaxRange(ns.lineRange(for: needle)), effectiveRange: nil) as? NSParagraphStyle | ||
| 556 | #expect((body?.firstLineHeadIndent == 0) == head.contains("noindent")) | ||
| 557 | } | ||
| 558 | } | ||
| 559 | |||
| 491 | @Test func editsBeforeTheWatermarkShiftIt() { | 560 | @Test func editsBeforeTheWatermarkShiftIt() { |
| 492 | let text = String(repeating: "line of text\n", count: 5_000) | 561 | let text = String(repeating: "line of text\n", count: 5_000) |
| 493 | let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false) | 562 | let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false) |