Skip to content

Commit 1c1ad6a

Browse files
committed
Docs
1 parent e015c4b commit 1c1ad6a

File tree

4 files changed

+31
-11
lines changed

4 files changed

+31
-11
lines changed

CodeEdit/Features/Editor/Views/CodeFileView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ struct CodeFileView: View {
163163
bracketPairHighlight = getBracketPairHighlight()
164164
}
165165
.onReceive(codeFile.$languageServerObjects) { languageServerObjects in
166+
// This will not be called in single-file views (for now) but is safe to listen to either way
166167
updateHighlightProviders(lspHighlightProvider: languageServerObjects.highlightProvider)
167168
}
168169
}

CodeEdit/Features/LSP/Features/SemanticTokens/SemanticTokenStorage/GenericSemanticTokenStorage.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import Foundation
99
import LanguageServerProtocol
1010
import CodeEditSourceEditor
1111

12-
/// Defines a protocol for an object to provide a storage mechanism for semantic tokens.
12+
/// Defines a protocol for an object to provide storage for semantic tokens.
1313
///
14-
/// There is only one concrete type that conforms to this in CE, but this protocol is useful in testing.
15-
/// See ``SemanticTokenStorage`` for use.
14+
/// There is only one concrete type that conforms to this in CE, but this protocol is useful in testing.
15+
/// See ``SemanticTokenStorage``.
1616
protocol GenericSemanticTokenStorage: AnyObject {
1717
var lastResultId: String? { get }
1818
var hasTokens: Bool { get }

CodeEdit/Features/LSP/Features/SemanticTokens/SemanticTokenStorage/SemanticTokenRange.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Khan Winter on 12/26/24.
66
//
77

8+
/// Represents the range of a semantic token.
89
struct SemanticTokenRange {
910
let line: UInt32
1011
let char: UInt32

CodeEdit/Features/LSP/Features/SemanticTokens/SemanticTokenStorage/SemanticTokenStorage.swift

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import Foundation
99
import LanguageServerProtocol
1010
import 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

Comments
 (0)