Commit 26d74b83c5
Verified · cmc
Layout: unified · split
Sources/OrgEditorAppKit/OrgEditor.swift +12 −8
| @@ -110,19 +110,22 @@ public final class OrgEditor: NSObject { | ||
| 110 | 110 | textContentStorage.delegate = self |
| 111 | 111 | |
| 112 | 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 | 123 | load(document.text) |
| 114 | 124 | // Files open at their start, as in Emacs; replacing the storage leaves it at the end. |
| 115 | 125 | textView.setSelectedRange(NSRange(location: 0, length: 0)) |
| 116 | if isOrg { | |
| 117 | let startup = Presentation.startupDisplay(document.tree, options: options) | |
| 118 | indented = startup.indented | |
| 119 | hideStars = startup.hideStars | |
| 126 | if let startup { | |
| 120 | 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 | 128 | if startup.alignTables { _ = perform(AlignAllTables()) } |
| 125 | if !indented || !hideStars { restyleOutsideEditing(0..<utf16Length) } | |
| 126 | 129 | } |
| 127 | 130 | spellChecking = options.spellCheck |
| 128 | 131 | truncatesLines = options.startupTruncated |
| @@ -263,6 +266,7 @@ public final class OrgEditor: NSObject { | ||
| 263 | 266 | view.updateGutter() |
| 264 | 267 | } |
| 265 | 268 | styledUpTo = 0 |
| 269 | revealedLine = caretLine | |
| 266 | 270 | styleNextChunk(Self.firstStyleChunk) |
| 267 | 271 | scheduleStyling() |
| 268 | 272 | } |
Tests/OrgEditorAppKitTests/EditorTests.swift +48
| @@ -488,6 +488,54 @@ struct InitialStylingTests { | ||
| 488 | 488 | #expect(h.textView.textStorage!.isEqual(to: Harness(h.string).textView.textStorage!)) |
| 489 | 489 | } |
| 490 | 490 | |
| 491 | @Test func openingLeavesTextPastTheFirstChunkUnstyled() throws { | |
| 492 | let text = String(repeating: "** heading *bold*\nbody text\n", count: 4_000) | |
| 493 | let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false) | |
| 494 | let storage = try #require(h.textView.textStorage) | |
| 495 | var unstyled = NSRange() | |
| 496 | _ = storage.attributes(at: h.editor.styledUpTo, longestEffectiveRange: &unstyled, in: NSRange(location: 0, length: storage.length)) | |
| 497 | #expect(unstyled == NSRange(location: h.editor.styledUpTo, length: storage.length - h.editor.styledUpTo)) | |
| 498 | } | |
| 499 | ||
| 500 | /// `#+STARTUP` display options hold in the first chunk before any background pass, and in | |
| 501 | /// the rest after them. | |
| 502 | @Test(arguments: [ | |
| 503 | "#+STARTUP: noindent\n", "#+STARTUP: noindent hidestars\n", "#+STARTUP: indent\n", "#+STARTUP: latexpreview\nInline $x^2$ here.\n", | |
| 504 | "#+STARTUP: shrink\n| <3> | b |\n| long cell | c |\n", "", | |
| 505 | ]) func startupDisplayAppliesToEveryChunk(head: String) throws { | |
| 506 | let text = head + String(repeating: "** heading *bold*\nbody text\n", count: 4_000) + "Last $y^2$ line\n" | |
| 507 | let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false) | |
| 508 | let storage = try #require(h.textView.textStorage) | |
| 509 | let length = storage.length | |
| 510 | // Without the preview boxes, which are made anew by each styling. | |
| 511 | func styled() -> NSAttributedString { | |
| 512 | let copy = NSMutableAttributedString(attributedString: storage.attributedSubstring(from: NSRange(location: 0, length: h.editor.styledUpTo))) | |
| 513 | copy.removeAttribute(.orgLatex, range: NSRange(location: 0, length: copy.length)) | |
| 514 | return copy | |
| 515 | } | |
| 516 | func check() { | |
| 517 | let before = styled() | |
| 518 | h.editor.restyleOutsideEditing(0..<h.editor.styledUpTo) | |
| 519 | #expect(styled().isEqual(to: before)) | |
| 520 | } | |
| 521 | #expect(h.editor.styledUpTo < length) | |
| 522 | check() | |
| 523 | h.drainStyling() | |
| 524 | #expect(h.editor.styledUpTo == length) | |
| 525 | check() | |
| 526 | ||
| 527 | let ns = text as NSString | |
| 528 | for needle in ["$x^2$", "$y^2$"] where head.contains("latexpreview") { | |
| 529 | #expect(storage.attribute(.orgLatex, at: ns.range(of: needle).location, effectiveRange: nil) != nil) | |
| 530 | } | |
| 531 | for needle in [ns.range(of: "** heading"), ns.range(of: "** heading", options: .backwards)] { | |
| 532 | let stars = storage.attribute(.foregroundColor, at: needle.location, effectiveRange: nil) as? NSColor | |
| 533 | #expect((stars == .clear) == !head.contains("noindent\n")) | |
| 534 | let body = storage.attribute(.paragraphStyle, at: NSMaxRange(ns.lineRange(for: needle)), effectiveRange: nil) as? NSParagraphStyle | |
| 535 | #expect((body?.firstLineHeadIndent == 0) == head.contains("noindent")) | |
| 536 | } | |
| 537 | } | |
| 538 | ||
| 491 | 539 | @Test func editsBeforeTheWatermarkShiftIt() { |
| 492 | 540 | let text = String(repeating: "line of text\n", count: 5_000) |
| 493 | 541 | let h = Harness(DocumentState(bytes: Array(text.utf8)), layout: false) |