Sources/OrgEditorAppKit/HiddenRanges.swift
43 lines · 1143 bytes
1#if os(macOS)
2import Foundation
3
4/// Hidden ranges, read by TextKit's enumeration callbacks, which may run off the main thread.
5final class HiddenRanges: @unchecked Sendable {
6 private let lock = NSLock()
7 private var ranges: [Range<Int>] = []
8
9 func set(_ new: [Range<Int>]) {
10 lock.withLock { ranges = new }
11 }
12
13 var all: [Range<Int>] {
14 lock.withLock { ranges }
15 }
16
17 func covers(_ start: Int, _ end: Int) -> Bool {
18 lock.withLock { ranges.contains { $0.lowerBound <= start && end <= $0.upperBound } }
19 }
20
21 func containing(_ offset: Int) -> Range<Int>? {
22 lock.withLock { ranges.first { $0.lowerBound <= offset && offset < $0.upperBound } }
23 }
24}
25
26/// Folded heading starts, read by the layout-fragment callback.
27final class FoldedHeadings: @unchecked Sendable {
28 private let lock = NSLock()
29 private var starts: Set<Int> = []
30
31 func set(_ new: Set<Int>) {
32 lock.withLock { starts = new }
33 }
34
35 func contains(_ offset: Int) -> Bool {
36 lock.withLock { starts.contains(offset) }
37 }
38
39 var all: Set<Int> {
40 lock.withLock { starts }
41 }
42}
43#endif