Skip to content

Commit 78cb70a

Browse files
committed
Document the async calculator
1 parent fb70a58 commit 78cb70a

File tree

3 files changed

+20
-29
lines changed

3 files changed

+20
-29
lines changed

Sources/CodeEditSourceEditor/LineFolding/Model/LineFoldCalculator.swift

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ import CodeEditTextView
1111
/// A utility that calculates foldable line ranges in a text document based on indentation depth.
1212
///
1313
/// `LineFoldCalculator` observes text edits and rebuilds fold regions asynchronously.
14-
/// Fold information is emitted via `rangesPublisher`.
15-
/// Notify the calculator it should re-calculate
1614
actor LineFoldCalculator {
1715
weak var foldProvider: LineFoldProvider?
1816
weak var controller: TextViewController?
@@ -21,7 +19,12 @@ actor LineFoldCalculator {
2119

2220
private var valueStreamContinuation: AsyncStream<LineFoldStorage>.Continuation
2321
private var textChangedTask: Task<Void, Never>?
24-
22+
23+
/// Create a new calculator object that listens to a given stream for text changes.
24+
/// - Parameters:
25+
/// - foldProvider: The object to use to calculate fold regions.
26+
/// - controller: The text controller to use for text and attachment fetching.
27+
/// - textChangedStream: A stream of text changes, received as the document is edited.
2528
init(
2629
foldProvider: LineFoldProvider?,
2730
controller: TextViewController,
@@ -36,7 +39,9 @@ actor LineFoldCalculator {
3639
deinit {
3740
textChangedTask?.cancel()
3841
}
39-
42+
43+
/// Sets up an attached task to listen to values on a stream of text changes.
44+
/// - Parameter textChangedStream: A stream of text changes.
4045
private func listenToTextChanges(textChangedStream: AsyncStream<(NSRange, Int)>) {
4146
textChangedTask = Task {
4247
for await edit in textChangedStream {
@@ -100,7 +105,12 @@ actor LineFoldCalculator {
100105

101106
await yieldNewStorage(newFolds: foldCache, controller: controller, documentRange: documentRange)
102107
}
103-
108+
109+
/// Yield a new storage value on the value stream using a new set of folds.
110+
/// - Parameters:
111+
/// - newFolds: The new folds to yield with the storage value.
112+
/// - controller: The text controller used for range and attachment fetching.
113+
/// - documentRange: The total range of the current document.
104114
private func yieldNewStorage(
105115
newFolds: [LineFoldStorage.RawFold],
106116
controller: TextViewController,
@@ -125,6 +135,10 @@ actor LineFoldCalculator {
125135
valueStreamContinuation.yield(storage)
126136
}
127137

138+
/// Asynchronously gets more line information from the fold provider.
139+
/// Runs on the main thread so all text-related calculations are safe with the main text storage.
140+
///
141+
/// Has to be an `AsyncSequence` so it can be main actor isolated.
128142
@MainActor
129143
struct ChunkedLineIterator: AsyncSequence, AsyncIteratorProtocol {
130144
var controller: TextViewController
@@ -146,7 +160,7 @@ actor LineFoldCalculator {
146160
self
147161
}
148162

149-
mutating func next() async -> [LineFoldProviderLineInfo]? {
163+
mutating func next() -> [LineFoldProviderLineInfo]? {
150164
var results: [LineFoldProviderLineInfo] = []
151165
var count = 0
152166
var previousDepth: Int = previousDepth

Sources/CodeEditSourceEditor/LineFolding/Model/LineFoldStorage.swift

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,4 @@ struct LineFoldStorage: Sendable {
120120

121121
return result.sorted { $0.range.lowerBound < $1.range.lowerBound }
122122
}
123-
124-
/// Given a depth and a location, return the full original fold region
125-
func fullFoldRegion(at location: Int, depth: Int) -> FoldRange? {
126-
guard let elem = store.findValue(at: location), elem.depth == depth else {
127-
return nil
128-
}
129-
return foldRanges[elem.id]
130-
}
131123
}

Tests/CodeEditSourceEditorTests/LineFoldingTests/LineFoldStorageTests.swift

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,19 +50,4 @@ struct LineFoldStorageTests {
5050
storage.updateFolds(from: raw, collapsedRanges: collapsedSet((1, 0)))
5151
#expect(storage.folds(in: 0..<15).first?.isCollapsed == true)
5252
}
53-
54-
@Test
55-
func stableIDsBetweenUpdates() {
56-
var storage = LineFoldStorage(documentLength: 30)
57-
let raw = [LineFoldStorage.RawFold(depth: 2, range: 10..<20)]
58-
59-
storage.updateFolds(from: raw, collapsedRanges: [])
60-
let initial = storage.fullFoldRegion(at: 10, depth: 2)!.id
61-
62-
// Perform update again with identical raw folds
63-
storage.updateFolds(from: raw, collapsedRanges: [])
64-
let subsequent = storage.fullFoldRegion(at: 10, depth: 2)!.id
65-
66-
#expect(initial == subsequent)
67-
}
6853
}

0 commit comments

Comments
 (0)