Sources/OrgEditorAppKit/LineIndex.swift

58f3b73e8ff337e5a15111fbb32c0d57cf2346da
orgstar/Sources/OrgEditorAppKit/LineIndex.swift history · blame · raw

74 lines · 2752 bytes

6 symbols in this file
 1#if os(macOS)
 2import Foundation
 3
 4/// Where each line of the text starts (UTF-16), kept up to date edit by edit, so line numbers
 5/// come from a search instead of counting newlines from the top.
 6final class LineIndex {
 7    /// Sorted; the first line starts at 0.
 8    private(set) var starts: [Int] = [0]
 9
10    init(_ text: NSString = "") { reset(text) }
11
12    func reset(_ text: NSString) {
13        starts = [0]
14        appendStarts(in: text, from: 0, to: text.length)
15    }
16
17    /// Reads the text in blocks: a search per line costs more than reading every character.
18    private func appendStarts(in text: NSString, from: Int, to end: Int) {
19        var buffer = [unichar](repeating: 0, count: 4096)
20        var location = from
21        while location < end {
22            let count = min(buffer.count, end - location)
23            text.getCharacters(&buffer, range: NSRange(location: location, length: count))
24            for i in 0..<count where buffer[i] == 10 { starts.append(location + i + 1) }
25            location += count
26        }
27    }
28
29    var lineCount: Int { starts.count }
30
31    /// The line (from 1) holding `offset`.
32    func line(of offset: Int) -> Int {
33        var low = 0
34        var high = starts.count
35        while low < high {
36            let mid = (low + high) / 2
37            if starts[mid] <= offset { low = mid + 1 } else { high = mid }
38        }
39        return max(1, low)
40    }
41
42    /// `old` (in the text before) became `newLength` characters of `text` (the text after).
43    func replace(_ old: Range<Int>, newLength: Int, in text: NSString) {
44        let delta = newLength - old.count
45        // Starts after a newline inside the old range go; starts after it move.
46        let first = firstIndex(after: old.lowerBound)
47        let last = firstIndex(after: old.upperBound)
48        var inserted: [Int] = []
49        var location = old.lowerBound
50        let end = old.lowerBound + newLength
51        while location < end {
52            let found = text.range(of: "\n", options: .literal, range: NSRange(location: location, length: end - location))
53            guard found.location != NSNotFound else { break }
54            inserted.append(found.location + 1)
55            location = found.location + 1
56        }
57        if delta != 0 {
58            for i in last..<starts.count { starts[i] += delta }
59        }
60        starts.replaceSubrange(first..<last, with: inserted)
61    }
62
63    /// The index of the first start greater than `offset`.
64    private func firstIndex(after offset: Int) -> Int {
65        var low = 0
66        var high = starts.count
67        while low < high {
68            let mid = (low + high) / 2
69            if starts[mid] <= offset { low = mid + 1 } else { high = mid }
70        }
71        return low
72    }
73}
74#endif