@@ -9,10 +9,10 @@ import Foundation
9
9
import LanguageServerProtocol
10
10
import CodeEditSourceEditor
11
11
12
- /// This class provides an efficient storage mechanism for semantic token data.
12
+ /// This class provides storage for semantic token data.
13
13
///
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
16
16
/// tokens and their decoded counterparts. It supports applying delta updates from the language server.
17
17
///
18
18
/// See ``SemanticTokenHighlightProvider`` for it's connection to the editor view.
@@ -34,18 +34,23 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
34
34
35
35
var state : CurrentState ?
36
36
37
+ /// Create an empty storage object.
37
38
init ( ) {
38
39
state = nil
39
40
}
40
41
41
42
// 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.
43
47
func getTokensFor( range: LSPRange ) -> [ SemanticToken ] {
44
48
guard let state = state, !state. tokens. isEmpty else {
45
49
return [ ]
46
50
}
47
51
var tokens : [ SemanticToken ] = [ ]
48
52
53
+ // Perform a binary search
49
54
guard var idx = findLowerBound ( in: range, data: state. tokens [ ... ] ) else {
50
55
return [ ]
51
56
}
@@ -57,7 +62,9 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
57
62
58
63
return tokens
59
64
}
60
-
65
+
66
+ /// Clear the current state and set a new one.
67
+ /// - Parameter data: The semantic tokens to set as the current state.
61
68
func setData( _ data: borrowing SemanticTokens ) {
62
69
state = CurrentState ( resultId: data. resultId, tokenData: data. data, tokens: data. decode ( ) )
63
70
}
@@ -67,10 +74,11 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
67
74
/// To calculate invalidated ranges:
68
75
/// - Grabs all semantic tokens that *will* be updated and invalidates their ranges
69
76
/// - 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:)``.
71
79
///
72
80
/// - Parameter deltas: The deltas to apply.
73
- /// - Returns: All ranges invalidated by the applied deltas.
81
+ /// - Returns: Ranges invalidated by the applied deltas.
74
82
func applyDelta( _ deltas: SemanticTokensDelta ) -> [ SemanticTokenRange ] {
75
83
assert ( state != nil , " State should be set before applying any deltas. " )
76
84
guard var tokenData = state? . tokenData else { return [ ] }
@@ -117,7 +125,17 @@ final class SemanticTokenStorage: GenericSemanticTokenStorage {
117
125
}
118
126
119
127
// 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.
121
139
func invalidatedRanges( startIdx: UInt , length: UInt , data: ArraySlice < UInt32 > ) -> [ SemanticTokenRange ] {
122
140
var ranges : [ SemanticTokenRange ] = [ ]
123
141
var idx = startIdx - ( startIdx % 5 )
0 commit comments