import Foundation /// Which characters of the text storage belong to which top-level block, /// read from the storage's own `.docsTopBlock` runs (spec 2026-11-22 ยง7 /// Design 4). Not from publications: text typed before any publication /// inherits its block's id, so the storage is right before the replica /// has answered. /// /// The runs tile the storage, in order. A run with a nil id is text that /// carries no block (typed into an empty document). A *provisional* block /// is one id spanning more than one paragraph: an optimistic Return, whose /// new paragraph keeps its block's id until a publication names it. A /// Backspace at a block's start gives the reverse, one paragraph holding /// two runs. Both are legal until the publication that resolves them. struct DocumentBlockIndex: Equatable { struct Run: Equatable { var id: String? var location: Int var length: Int var end: Int { location + length } var range: NSRange { NSRange(location: location, length: length) } } private(set) var runs: [Run] = [] /// The runs as the storage holds them now, read afresh. static func runs(in storage: NSAttributedString, range: NSRange? = nil) -> [Run] { let range = range ?? NSRange(location: 0, length: storage.length) var runs: [Run] = [] guard range.length > 0 else { return runs } storage.enumerateAttribute(.docsTopBlock, in: range) { value, found, _ in let id = value as? String if let last = runs.last, last.id == id, last.end == found.location { runs[runs.count - 1].length += found.length } else { runs.append(Run(id: id, location: found.location, length: found.length)) } } return runs } mutating func rebuild(from storage: NSAttributedString) { runs = Self.runs(in: storage) } /// After the storage processed an edit: `edited` is the range in the /// new text, `delta` the change in length. Runs before it stay, runs /// after it shift by `delta`, and the runs it touched (with their /// neighbours, so that runs of one id merge) are read again from the /// attribute. O(blocks) integer work, and a re-read of only the touched /// runs. mutating func processEditing(_ storage: NSAttributedString, edited: NSRange, delta: Int) { guard !runs.isEmpty, edited.location != NSNotFound else { rebuild(from: storage); return } let oldStart = edited.location, oldEnd = NSMaxRange(edited) - delta // The first run ending at or after the edit, and the last starting // at or before its end (in the old text): the touched ones, with // whichever runs merely touch it. var first = firstRun(endingAtOrAfter: oldStart) if first > 0 { first -= 1 } var last = first while last + 1 < runs.count, runs[last + 1].location <= oldEnd { last += 1 } if last + 1 < runs.count { last += 1 } let start = runs[first].location let end = runs[last].end + delta guard start >= 0, end <= storage.length, start <= end else { rebuild(from: storage); return } let fresh = Self.runs(in: storage, range: NSRange(location: start, length: end - start)) for index in (last + 1).. 0 && seam < runs.count { if runs[seam - 1].id == runs[seam].id, runs[seam - 1].end == runs[seam].location { runs[seam - 1].length += runs[seam].length runs.remove(at: seam) } } if runs.isEmpty, storage.length > 0 { rebuild(from: storage) } } private func firstRun(endingAtOrAfter location: Int) -> Int { var low = 0, high = runs.count - 1 while low < high { let middle = (low + high) / 2 if runs[middle].end < location { low = middle + 1 } else { high = middle } } return low } /// The runs holding any character from `first` to `last`, inclusive. func runs(holding first: Int, through last: Int) -> ArraySlice { guard !runs.isEmpty, last >= first else { return [] } var from = firstRun(endingAtOrAfter: first) if runs[from].end == first, from + 1 < runs.count { from += 1 } var to = from while to + 1 < runs.count, runs[to + 1].location <= last { to += 1 } return runs[from...to] } /// Run positions by block id, in storage order. func positions() -> [String: [Int]] { var found: [String: [Int]] = [:] for (index, run) in runs.enumerated() { if let id = run.id { found[id, default: []].append(index) } } return found } }