@@ -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
1614actor 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
0 commit comments