Skip to content

Commit cd07e2f

Browse files
authored
Merge pull request #1137 from ahoppen/ahoppen/no-work-done-progess-if-client-doesnt-support
Don’t send `WorkDoneProgressRequest` to the client if it doesn’t support work done progress
2 parents 3e505ed + 252cccc commit cd07e2f

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,15 @@ final actor WorkDoneProgressState {
132132
/// Start a new task, creating a new `WorkDoneProgress` if none is running right now.
133133
///
134134
/// - Parameter server: The server that is used to create the `WorkDoneProgress` on the client
135-
func startProgress(server: SourceKitServer) {
135+
func startProgress(server: SourceKitServer) async {
136136
activeTasks += 1
137+
guard await server.capabilityRegistry?.clientCapabilities.window?.workDoneProgress ?? false else {
138+
// If the client doesn't support workDoneProgress, keep track of the active task count but don't update the state.
139+
// That way, if we call `startProgress` before initialization finishes, we won't send the
140+
// `CreateWorkDoneProgressRequest` but if we call `startProgress` again after initialization finished (and thus
141+
// the capability is set), we will create the work done progress.
142+
return
143+
}
137144
if state == .noProgress {
138145
state = .creating
139146
// Discard the handle. We don't support cancellation of the creation of a work done progress.
@@ -170,9 +177,12 @@ final actor WorkDoneProgressState {
170177
/// If this drops the active task count to 0, the work done progress is ended on the client.
171178
///
172179
/// - Parameter server: The server that is used to send and update of the `WorkDoneProgress` to the client
173-
func endProgress(server: SourceKitServer) {
180+
func endProgress(server: SourceKitServer) async {
174181
assert(activeTasks > 0, "Unbalanced startProgress/endProgress calls")
175182
activeTasks -= 1
183+
guard await server.capabilityRegistry?.clientCapabilities.window?.workDoneProgress ?? false else {
184+
return
185+
}
176186
if state == .created && activeTasks == 0 {
177187
server.client.send(WorkDoneProgress(token: token, value: .end(WorkDoneProgressEnd())))
178188
}

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ public actor SwiftLanguageServer: ToolchainLanguageServer {
147147
if sourcekitdCrashedWorkDoneProgress == nil {
148148
sourcekitdCrashedWorkDoneProgress = WorkDoneProgressManager(
149149
server: sourceKitServer,
150+
capabilityRegistry: capabilityRegistry,
150151
title: "SourceKit-LSP: Restoring functionality",
151152
message: "Please run 'sourcekit-lsp diagnose' to file an issue"
152153
)

Sources/SourceKitLSP/WorkDoneProgressManager.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ final class WorkDoneProgressManager {
2323
private let queue = AsyncQueue<Serial>()
2424
private let server: SourceKitServer
2525

26-
init(server: SourceKitServer, title: String, message: String? = nil) {
26+
init?(server: SourceKitServer, capabilityRegistry: CapabilityRegistry, title: String, message: String? = nil) {
27+
guard capabilityRegistry.clientCapabilities.window?.workDoneProgress ?? false else {
28+
return nil
29+
}
2730
self.token = .string("WorkDoneProgress-\(UUID())")
2831
self.server = server
2932
queue.async { [server, token] in

Tests/SourceKitDTests/CrashRecoveryTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ final class CrashRecoveryTests: XCTestCase {
4848
try SkipUnless.platformIsDarwin("Linux and Windows use in-process sourcekitd")
4949
try SkipUnless.longTestsEnabled()
5050

51-
let testClient = try await TestSourceKitLSPClient(usePullDiagnostics: false)
51+
let testClient = try await TestSourceKitLSPClient(
52+
capabilities: ClientCapabilities(window: WindowClientCapabilities(workDoneProgress: true)),
53+
usePullDiagnostics: false
54+
)
5255
let uri = DocumentURI.for(.swift)
5356

5457
let positions = testClient.openDocument(

0 commit comments

Comments
 (0)