|
| 1 | +// |
| 2 | +// LineFoldCalculator.swift |
| 3 | +// CodeEditSourceEditor |
| 4 | +// |
| 5 | +// Created by Khan Winter on 5/9/25. |
| 6 | +// |
| 7 | + |
| 8 | +import AppKit |
| 9 | +import CodeEditTextView |
| 10 | +import Combine |
| 11 | + |
| 12 | +/// A utility that calculates foldable line ranges in a text document based on indentation depth. |
| 13 | +/// |
| 14 | +/// `LineFoldCalculator` observes text edits and rebuilds fold regions asynchronously. |
| 15 | +/// Fold information is emitted via `rangesPublisher`. |
| 16 | +/// Notify the calculator it should re-calculate |
| 17 | +class LineFoldCalculator { |
| 18 | + weak var foldProvider: LineFoldProvider? |
| 19 | + weak var textView: TextView? |
| 20 | + |
| 21 | + var rangesPublisher = CurrentValueSubject<[FoldRange], Never>([]) |
| 22 | + |
| 23 | + private let workQueue = DispatchQueue.global(qos: .default) |
| 24 | + |
| 25 | + var textChangedReceiver = PassthroughSubject<(NSRange, Int), Never>() |
| 26 | + private var textChangedCancellable: AnyCancellable? |
| 27 | + |
| 28 | + init(foldProvider: LineFoldProvider?, textView: TextView) { |
| 29 | + self.foldProvider = foldProvider |
| 30 | + self.textView = textView |
| 31 | + |
| 32 | + textChangedCancellable = textChangedReceiver |
| 33 | + .throttle(for: 0.1, scheduler: RunLoop.main, latest: true) |
| 34 | + .sink { edit in |
| 35 | + self.buildFoldsForDocument(afterEditIn: edit.0, delta: edit.1) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + /// Build out the folds for the entire document. |
| 40 | + /// |
| 41 | + /// For each line in the document, find the indentation level using the ``levelProvider``. At each line, if the |
| 42 | + /// indent increases from the previous line, we start a new fold. If it decreases we end the fold we were in. |
| 43 | + private func buildFoldsForDocument(afterEditIn: NSRange, delta: Int) { |
| 44 | + workQueue.async { |
| 45 | + guard let textView = self.textView, let foldProvider = self.foldProvider else { return } |
| 46 | + var foldCache: [FoldRange] = [] |
| 47 | + var currentFold: FoldRange? |
| 48 | + var currentDepth: Int = 0 |
| 49 | + var iterator = textView.layoutManager.linesInRange(textView.documentRange) |
| 50 | + |
| 51 | + var lines = self.getMoreLines(textView: textView, iterator: &iterator, foldProvider: foldProvider) |
| 52 | + while let lineChunk = lines { |
| 53 | + for (lineNumber, foldDepth) in lineChunk { |
| 54 | + // Start a new fold, going deeper to a new depth. |
| 55 | + if foldDepth > currentDepth { |
| 56 | + let newFold = FoldRange( |
| 57 | + lineRange: (lineNumber - 1)...(lineNumber - 1), |
| 58 | + range: .zero, |
| 59 | + depth: foldDepth, |
| 60 | + parent: currentFold, |
| 61 | + subFolds: [] |
| 62 | + ) |
| 63 | + |
| 64 | + if currentFold == nil { |
| 65 | + foldCache.append(newFold) |
| 66 | + } else { |
| 67 | + currentFold?.subFolds.append(newFold) |
| 68 | + } |
| 69 | + currentFold = newFold |
| 70 | + } else if foldDepth < currentDepth { |
| 71 | + // End this fold, go shallower "popping" folds deeper than the new depth |
| 72 | + while let fold = currentFold, fold.depth > foldDepth { |
| 73 | + // close this fold at the current line |
| 74 | + fold.lineRange = fold.lineRange.lowerBound...lineNumber |
| 75 | + // move up |
| 76 | + currentFold = fold.parent |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + currentDepth = foldDepth |
| 81 | + } |
| 82 | + lines = self.getMoreLines(textView: textView, iterator: &iterator, foldProvider: foldProvider) |
| 83 | + } |
| 84 | + |
| 85 | + // Clean up any hanging folds. |
| 86 | + while let fold = currentFold { |
| 87 | + fold.lineRange = fold.lineRange.lowerBound...textView.layoutManager.lineCount |
| 88 | + currentFold = fold.parent |
| 89 | + } |
| 90 | + |
| 91 | + self.rangesPublisher.send(foldCache) |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private func getMoreLines( |
| 96 | + textView: TextView, |
| 97 | + iterator: inout TextLayoutManager.RangeIterator, |
| 98 | + foldProvider: LineFoldProvider |
| 99 | + ) -> [(index: Int, foldDepth: Int)]? { |
| 100 | + DispatchQueue.main.asyncAndWait { |
| 101 | + var results: [(index: Int, foldDepth: Int)] = [] |
| 102 | + var count = 0 |
| 103 | + while count < 50, let linePosition = iterator.next() { |
| 104 | + guard let substring = textView.textStorage.substring(from: linePosition.range) as NSString?, |
| 105 | + let foldDepth = foldProvider.foldLevelAtLine( |
| 106 | + linePosition.index, |
| 107 | + substring: substring |
| 108 | + ) else { |
| 109 | + count += 1 |
| 110 | + continue |
| 111 | + } |
| 112 | + |
| 113 | + results.append((linePosition.index, foldDepth)) |
| 114 | + count += 1 |
| 115 | + } |
| 116 | + if results.isEmpty && count == 0 { |
| 117 | + return nil |
| 118 | + } |
| 119 | + return results |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments