Commit 13a0d02bad
Verified · cmc
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 } |
Tests/OrgEditorAppKitTests/EditorTests.swift +21
| @@ -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 { |