@@ -162,18 +162,18 @@ public final class SwiftLanguageServer: ToolchainLanguageServer {
162
162
}
163
163
164
164
/// Update lexical and syntactic tokens for the given `snapshot`.
165
- func updateLexicalAndSyntacticTokens(
165
+ /// Should only be called on `queue`.
166
+ private func updateLexicalAndSyntacticTokens(
166
167
response: SKDResponseDictionary ,
167
168
for snapshot: DocumentSnapshot
168
169
) {
170
+ dispatchPrecondition ( condition: . onQueue( queue) )
171
+
169
172
let uri = snapshot. document. uri
170
173
171
174
if let syntaxMap: SKDResponseArray = response [ keys. syntaxmap] {
172
- let tokenParser = SemanticTokenParser (
173
- sourcekitd: sourcekitd,
174
- snapshot: snapshot
175
- )
176
- let tokens = tokenParser. parseTokens ( syntaxMap)
175
+ let tokenParser = SyntaxHighlightingTokenParser ( sourcekitd: sourcekitd)
176
+ let tokens = tokenParser. parseTokens ( syntaxMap, in: snapshot)
177
177
do {
178
178
try documentManager. addLexicalTokens ( uri, tokens: tokens)
179
179
} catch {
@@ -182,50 +182,51 @@ public final class SwiftLanguageServer: ToolchainLanguageServer {
182
182
}
183
183
184
184
if let substructure: SKDResponseArray = response [ keys. substructure] {
185
- let tokenParser = SemanticTokenParser (
186
- sourcekitd: sourcekitd,
187
- snapshot: snapshot,
188
- useName: true
189
- )
190
- let tokens = tokenParser. parseTokens ( substructure)
185
+ let tokenParser = SyntaxHighlightingTokenParser ( sourcekitd: sourcekitd, useName: true )
186
+ let tokens = tokenParser. parseTokens ( substructure, in: snapshot)
191
187
do {
192
188
try documentManager. replaceSyntacticTokens ( uri, tokens: tokens)
193
189
} catch {
194
- log ( " updating lexical tokens for \( uri) failed: \( error) " , level: . warning)
190
+ log ( " updating syntactic tokens for \( uri) failed: \( error) " , level: . warning)
195
191
}
196
192
}
197
193
}
198
194
199
195
/// Update semantic tokens for the given `snapshot`.
200
- func updateSemanticTokens(
196
+ /// Should only be called on `queue`.
197
+ private func updateSemanticTokens(
201
198
response: SKDResponseDictionary ,
202
199
for snapshot: DocumentSnapshot
203
200
) {
201
+ dispatchPrecondition ( condition: . onQueue( queue) )
202
+
204
203
let uri = snapshot. document. uri
205
204
guard let skTokens: SKDResponseArray = response [ keys. annotations] else {
206
205
return
207
206
}
208
207
209
- let tokenParser = SemanticTokenParser (
210
- sourcekitd: sourcekitd,
211
- snapshot: snapshot
212
- )
213
- let tokens = tokenParser. parseTokens ( skTokens)
208
+ let tokenParser = SyntaxHighlightingTokenParser ( sourcekitd: sourcekitd)
209
+ let tokens = tokenParser. parseTokens ( skTokens, in: snapshot)
214
210
215
211
do {
216
212
try documentManager. replaceSemanticTokens ( uri, tokens: tokens)
217
- if clientCapabilities. workspace? . semanticTokens? . refreshSupport ?? false {
218
- _ = client. send ( WorkspaceSemanticTokensRefreshRequest ( ) , queue: queue) { result in
219
- if let error = result. failure {
220
- log ( " refreshing semantic tokens for \( uri) failed: \( error) " , level: . warning)
221
- }
222
- }
223
- }
213
+
224
214
} catch {
225
215
log ( " updating semantic tokens for \( uri) failed: \( error) " , level: . warning)
226
216
}
227
217
}
228
218
219
+ /// Inform the client about changes to the syntax highlighting tokens.
220
+ private func requestTokensRefresh( ) {
221
+ if clientCapabilities. workspace? . semanticTokens? . refreshSupport ?? false {
222
+ _ = client. send ( WorkspaceSemanticTokensRefreshRequest ( ) , queue: queue) { result in
223
+ if let error = result. failure {
224
+ log ( " refreshing tokens failed: \( error) " , level: . warning)
225
+ }
226
+ }
227
+ }
228
+ }
229
+
229
230
/// Publish diagnostics for the given `snapshot`. We withhold semantic diagnostics if we are using
230
231
/// fallback arguments.
231
232
///
@@ -288,6 +289,7 @@ public final class SwiftLanguageServer: ToolchainLanguageServer {
288
289
if let dict = try ? self . sourcekitd. sendSync ( req) {
289
290
publishDiagnostics ( response: dict, for: snapshot, compileCommand: compileCommand)
290
291
updateSemanticTokens ( response: dict, for: snapshot)
292
+ requestTokensRefresh ( )
291
293
}
292
294
}
293
295
}
@@ -323,8 +325,8 @@ extension SwiftLanguageServer {
323
325
commands: builtinSwiftCommands) ,
324
326
semanticTokensProvider: SemanticTokensOptions (
325
327
legend: SemanticTokensLegend (
326
- tokenTypes: SemanticToken . Kind. allCases. map ( \. lspName) ,
327
- tokenModifiers: SemanticToken . Modifiers. allCases. compactMap ( \ . lspName) ) ,
328
+ tokenTypes: SyntaxHighlightingToken . Kind. allCases. map ( \. lspName) ,
329
+ tokenModifiers: SyntaxHighlightingToken . Modifiers. allCases. map { $0 . lspName! } ) ,
328
330
range: . bool( true ) ,
329
331
full: . bool( true ) )
330
332
) )
@@ -788,8 +790,8 @@ extension SwiftLanguageServer {
788
790
return
789
791
}
790
792
791
- let tokens = snapshot. tokens. sorted
792
- let encodedTokens = encodeToIntArray ( semanticTokens : tokens)
793
+ let tokens = snapshot. tokens. mergedAndSorted
794
+ let encodedTokens = tokens. lspEncoded
793
795
794
796
req. reply ( DocumentSemanticTokensResponse ( data: encodedTokens) )
795
797
}
@@ -811,8 +813,8 @@ extension SwiftLanguageServer {
811
813
return
812
814
}
813
815
814
- let tokens = snapshot. tokens. sorted . filter { $0. range. overlaps ( range) }
815
- let encodedTokens = encodeToIntArray ( semanticTokens : tokens)
816
+ let tokens = snapshot. tokens. mergedAndSorted . filter { $0. range. overlaps ( range) }
817
+ let encodedTokens = tokens. lspEncoded
816
818
817
819
req. reply ( DocumentSemanticTokensResponse ( data: encodedTokens) )
818
820
}
0 commit comments