Skip to content

Commit 3f1568c

Browse files
authored
Merge pull request #2144 from ahoppen/refresh-semantic-tokens
Send `workspace/semanticTokens/refresh` to client when build settings have changed
2 parents 863c0e2 + 1bc7d85 commit 3f1568c

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

Sources/SourceKitLSP/Swift/SwiftLanguageService.swift

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,14 @@ package actor SwiftLanguageService: LanguageService, Sendable {
183183
/// `buildSettings(for:)` returns `nil` are not included.
184184
private var buildSettingsForOpenFiles: [DocumentURI: SwiftCompileCommand] = [:]
185185

186-
/// Calling `scheduleCall` on `refreshDiagnosticsDebouncer` schedules a `DiagnosticsRefreshRequest` to be sent to
187-
/// to the client.
186+
/// Calling `scheduleCall` on `refreshDiagnosticsAndSemanticTokensDebouncer` schedules a `DiagnosticsRefreshRequest`
187+
/// and `WorkspaceSemanticTokensRefreshRequest` to be sent to to the client.
188188
///
189189
/// We debounce these calls because the `DiagnosticsRefreshRequest` is a workspace-wide request. If we discover that
190190
/// the client should update diagnostics for file A and then discover that it should also update diagnostics for file
191191
/// B, we don't want to send two `DiagnosticsRefreshRequest`s. Instead, the two should be unified into a single
192192
/// request.
193-
private let refreshDiagnosticsDebouncer: Debouncer<Void>
193+
private let refreshDiagnosticsAndSemanticTokensDebouncer: Debouncer<Void>
194194

195195
/// Creates a language server for the given client using the sourcekitd dylib specified in `toolchain`.
196196
/// `reopenDocuments` is a closure that will be called if sourcekitd crashes and the `SwiftLanguageService` asks its
@@ -229,19 +229,29 @@ package actor SwiftLanguageService: LanguageService, Sendable {
229229
self.options = options
230230

231231
// The debounce duration of 500ms was chosen arbitrarily without scientific research.
232-
self.refreshDiagnosticsDebouncer = Debouncer(debounceDuration: .milliseconds(500)) { [weak sourceKitLSPServer] in
232+
self.refreshDiagnosticsAndSemanticTokensDebouncer = Debouncer(debounceDuration: .milliseconds(500)) {
233+
[weak sourceKitLSPServer] in
233234
guard let sourceKitLSPServer else {
234-
logger.fault("Not sending DiagnosticRefreshRequest to client because sourceKitLSPServer has been deallocated")
235+
logger.fault(
236+
"Not sending diagnostic and semantic token refresh request to client because sourceKitLSPServer has been deallocated"
237+
)
235238
return
236239
}
237-
guard
238-
await sourceKitLSPServer.capabilityRegistry?.clientCapabilities.workspace?.diagnostics?.refreshSupport ?? false
239-
else {
240+
let clientCapabilities = await sourceKitLSPServer.capabilityRegistry?.clientCapabilities
241+
if clientCapabilities?.workspace?.diagnostics?.refreshSupport ?? false {
242+
_ = await orLog("Sending DiagnosticRefreshRequest to client after document dependencies updated") {
243+
try await sourceKitLSPServer.sendRequestToClient(DiagnosticsRefreshRequest())
244+
}
245+
} else {
240246
logger.debug("Not sending DiagnosticRefreshRequest because the client doesn't support it")
241-
return
242247
}
243-
_ = await orLog("Sending DiagnosticRefreshRequest to client after document dependencies updated") {
244-
try await sourceKitLSPServer.sendRequestToClient(DiagnosticsRefreshRequest())
248+
249+
if clientCapabilities?.workspace?.semanticTokens?.refreshSupport ?? false {
250+
_ = await orLog("Sending WorkspaceSemanticTokensRefreshRequest to client after document dependencies updated") {
251+
try await sourceKitLSPServer.sendRequestToClient(WorkspaceSemanticTokensRefreshRequest())
252+
}
253+
} else {
254+
logger.debug("Not sending WorkspaceSemanticTokensRefreshRequest because the client doesn't support it")
245255
}
246256
}
247257

@@ -337,7 +347,7 @@ package actor SwiftLanguageService: LanguageService, Sendable {
337347
await sourceKitLSPServer.sourcekitdCrashedWorkDoneProgress.end()
338348
// We can provide diagnostics again now. Send a diagnostic refresh request to prompt the editor to reload
339349
// diagnostics.
340-
await refreshDiagnosticsDebouncer.scheduleCall()
350+
await refreshDiagnosticsAndSemanticTokensDebouncer.scheduleCall()
341351
case (.connected, .connected),
342352
(.connectionInterrupted, .connectionInterrupted),
343353
(.connectionInterrupted, .semanticFunctionalityDisabled),
@@ -469,7 +479,7 @@ extension SwiftLanguageService {
469479
}
470480

471481
if await capabilityRegistry.clientSupportsPullDiagnostics(for: .swift) {
472-
await self.refreshDiagnosticsDebouncer.scheduleCall()
482+
await self.refreshDiagnosticsAndSemanticTokensDebouncer.scheduleCall()
473483
} else {
474484
await publishDiagnosticsIfNeeded(for: snapshot.uri)
475485
}

0 commit comments

Comments
 (0)