| 1 | import Foundation |
| 2 | |
| 3 | /// Which characters of the text storage belong to which top-level block, |
| 4 | /// read from the storage's own `.docsTopBlock` runs (spec 2026-11-22 §7 |
| 5 | /// Design 4). Not from publications: text typed before any publication |
| 6 | /// inherits its block's id, so the storage is right before the replica |
| 7 | /// has answered. |
| 8 | /// |
| 9 | /// The runs tile the storage, in order. A run with a nil id is text that |
| 10 | /// carries no block (typed into an empty document). A *provisional* block |
| 11 | /// is one id spanning more than one paragraph: an optimistic Return, whose |
| 12 | /// new paragraph keeps its block's id until a publication names it. A |
| 13 | /// Backspace at a block's start gives the reverse, one paragraph holding |
| 14 | /// two runs. Both are legal until the publication that resolves them. |
| 15 | struct DocumentBlockIndex: Equatable { |
| 16 | struct Run: Equatable { |
| 17 | var id: String? |
| 18 | var location: Int |
| 19 | var length: Int |
| 20 | var end: Int { location + length } |
| 21 | var range: NSRange { NSRange(location: location, length: length) } |
| 22 | } |
| 23 | private(set) var runs: [Run] = [] |
| 24 | |
| 25 | /// The runs as the storage holds them now, read afresh. |
| 26 | static func runs(in storage: NSAttributedString, range: NSRange? = nil) -> [Run] { |
| 27 | let range = range ?? NSRange(location: 0, length: storage.length) |
| 28 | var runs: [Run] = [] |
| 29 | guard range.length > 0 else { return runs } |
| 30 | storage.enumerateAttribute(.docsTopBlock, in: range) { value, found, _ in |
| 31 | let id = value as? String |
| 32 | if let last = runs.last, last.id == id, last.end == found.location { runs[runs.count - 1].length += found.length } |
| 33 | else { runs.append(Run(id: id, location: found.location, length: found.length)) } |
| 34 | } |
| 35 | return runs |
| 36 | } |
| 37 | |
| 38 | mutating func rebuild(from storage: NSAttributedString) { runs = Self.runs(in: storage) } |
| 39 | |
| 40 | /// After the storage processed an edit: `edited` is the range in the |
| 41 | /// new text, `delta` the change in length. Runs before it stay, runs |
| 42 | /// after it shift by `delta`, and the runs it touched (with their |
| 43 | /// neighbours, so that runs of one id merge) are read again from the |
| 44 | /// attribute. O(blocks) integer work, and a re-read of only the touched |
| 45 | /// runs. |
| 46 | mutating func processEditing(_ storage: NSAttributedString, edited: NSRange, delta: Int) { |
| 47 | guard !runs.isEmpty, edited.location != NSNotFound else { rebuild(from: storage); return } |
| 48 | let oldStart = edited.location, oldEnd = NSMaxRange(edited) - delta |
| 49 | // The first run ending at or after the edit, and the last starting |
| 50 | // at or before its end (in the old text): the touched ones, with |
| 51 | // whichever runs merely touch it. |
| 52 | var first = firstRun(endingAtOrAfter: oldStart) |
| 53 | if first > 0 { first -= 1 } |
| 54 | var last = first |
| 55 | while last + 1 < runs.count, runs[last + 1].location <= oldEnd { last += 1 } |
| 56 | if last + 1 < runs.count { last += 1 } |
| 57 | let start = runs[first].location |
| 58 | let end = runs[last].end + delta |
| 59 | guard start >= 0, end <= storage.length, start <= end else { rebuild(from: storage); return } |
| 60 | let fresh = Self.runs(in: storage, range: NSRange(location: start, length: end - start)) |
| 61 | for index in (last + 1)..<runs.count { runs[index].location += delta } |
| 62 | runs.replaceSubrange(first...last, with: fresh) |
| 63 | // The re-read starts and ends on old run boundaries, so a seam can |
| 64 | // only join equal ids that were apart before. |
| 65 | let seams = [first + fresh.count, first] |
| 66 | for seam in seams where seam > 0 && seam < runs.count { |
| 67 | if runs[seam - 1].id == runs[seam].id, runs[seam - 1].end == runs[seam].location { |
| 68 | runs[seam - 1].length += runs[seam].length |
| 69 | runs.remove(at: seam) |
| 70 | } |
| 71 | } |
| 72 | if runs.isEmpty, storage.length > 0 { rebuild(from: storage) } |
| 73 | } |
| 74 | |
| 75 | private func firstRun(endingAtOrAfter location: Int) -> Int { |
| 76 | var low = 0, high = runs.count - 1 |
| 77 | while low < high { |
| 78 | let middle = (low + high) / 2 |
| 79 | if runs[middle].end < location { low = middle + 1 } else { high = middle } |
| 80 | } |
| 81 | return low |
| 82 | } |
| 83 | |
| 84 | /// The runs holding any character from `first` to `last`, inclusive. |
| 85 | func runs(holding first: Int, through last: Int) -> ArraySlice<Run> { |
| 86 | guard !runs.isEmpty, last >= first else { return [] } |
| 87 | var from = firstRun(endingAtOrAfter: first) |
| 88 | if runs[from].end == first, from + 1 < runs.count { from += 1 } |
| 89 | var to = from |
| 90 | while to + 1 < runs.count, runs[to + 1].location <= last { to += 1 } |
| 91 | return runs[from...to] |
| 92 | } |
| 93 | /// Run positions by block id, in storage order. |
| 94 | func positions() -> [String: [Int]] { |
| 95 | var found: [String: [Int]] = [:] |
| 96 | for (index, run) in runs.enumerated() { if let id = run.id { found[id, default: []].append(index) } } |
| 97 | return found |
| 98 | } |
| 99 | } |
| 100 | |