@@ -9,10 +9,10 @@ import Foundation
99import LanguageServerProtocol
1010import CodeEditSourceEditor
1111
12- /// This class provides an efficient storage mechanism for semantic token data.
12+ /// This class provides storage for semantic token data.
1313///
14- /// The LSP spec requires that clients keep the original compressed data to apply delta edits to. The delta updates may
15- /// come as a delta to a single number in the compressed array. This class maintains a current state of compressed
14+ /// The LSP spec requires that clients keep the original compressed data to apply delta edits. Delta updates may
15+ /// appear as a delta to a single number in the compressed array. This class maintains the current state of compressed
1616/// tokens and their decoded counterparts. It supports applying delta updates from the language server.
1717///
1818/// See ``SemanticTokenHighlightProvider`` for it's connection to the editor view.
@@ -34,18 +34,23 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
3434
3535 var state : CurrentState ?
3636
37+ /// Create an empty storage object.
3738 init ( ) {
3839 state = nil
3940 }
4041
4142 // MARK: - Storage Conformance
42-
43+
44+ /// Finds all tokens in the given range.
45+ /// - Parameter range: The range to query.
46+ /// - Returns: All tokens found in the range.
4347 func getTokensFor( range: LSPRange ) -> [ SemanticToken ] {
4448 guard let state = state, !state. tokens. isEmpty else {
4549 return [ ]
4650 }
4751 var tokens : [ SemanticToken ] = [ ]
4852
53+ // Perform a binary search
4954 guard var idx = findLowerBound ( in: range, data: state. tokens [ ... ] ) else {
5055 return [ ]
5156 }
@@ -57,7 +62,9 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
5762
5863 return tokens
5964 }
60-
65+
66+ /// Clear the current state and set a new one.
67+ /// - Parameter data: The semantic tokens to set as the current state.
6168 func setData( _ data: borrowing SemanticTokens ) {
6269 state = CurrentState ( resultId: data. resultId, tokenData: data. data, tokens: data. decode ( ) )
6370 }
@@ -67,10 +74,11 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
6774 /// To calculate invalidated ranges:
6875 /// - Grabs all semantic tokens that *will* be updated and invalidates their ranges
6976 /// - Loops over all inserted tokens and invalidates their ranges
70- /// This may result in duplicated ranges. It's up to the caller to de-duplicate if necessary.
77+ /// This may result in duplicated ranges. It's up to the caller to de-duplicate if necessary. See
78+ /// ``SemanticTokenStorage/invalidatedRanges(startIdx:length:data:)``.
7179 ///
7280 /// - Parameter deltas: The deltas to apply.
73- /// - Returns: All ranges invalidated by the applied deltas.
81+ /// - Returns: Ranges invalidated by the applied deltas.
7482 func applyDelta( _ deltas: SemanticTokensDelta ) -> [ SemanticTokenRange ] {
7583 assert ( state != nil , " State should be set before applying any deltas. " )
7684 guard var tokenData = state? . tokenData else { return [ ] }
@@ -117,7 +125,17 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
117125 }
118126
119127 // MARK: - Invalidated Indices
120-
128+
129+ /// Calculate what document ranges are invalidated due to changes in the compressed token data.
130+ ///
131+ /// This overestimates invalidated ranges by assuming all tokens touched by a change are invalid. All this does is
132+ /// find what tokens are being updated by a delta and return them.
133+ ///
134+ /// - Parameters:
135+ /// - startIdx: The start index of the compressed token data an edits start at.
136+ /// - length: The length of any edits.
137+ /// - data: A reference to the compressed token data.
138+ /// - Returns: All token ranges included in the range of the edit.
121139 func invalidatedRanges( startIdx: UInt , length: UInt , data: ArraySlice < UInt32 > ) -> [ SemanticTokenRange ] {
122140 var ranges : [ SemanticTokenRange ] = [ ]
123141 var idx = startIdx - ( startIdx % 5 )
0 commit comments