@@ -177,26 +177,39 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {
177
177
indexTaskScheduler: TaskScheduler < AnyIndexTaskDescription >
178
178
) async {
179
179
struct ConnectionToClient : BuildSystemManagerConnectionToClient {
180
- let sourceKitLSPServer : SourceKitLSPServer
180
+ weak var sourceKitLSPServer : SourceKitLSPServer ?
181
181
func send( _ notification: some NotificationType ) async {
182
+ guard let sourceKitLSPServer else {
183
+ // `SourceKitLSPServer` has been destructed. We are tearing down the
184
+ // language server. Nothing left to do.
185
+ logger. error (
186
+ " Ignoring notificaiton \( type ( of: notification) . method) because connection to editor has been closed "
187
+ )
188
+ return
189
+ }
182
190
await sourceKitLSPServer. waitUntilInitialized ( )
183
191
sourceKitLSPServer. sendNotificationToClient ( notification)
184
192
}
185
193
186
194
func send< R: RequestType > ( _ request: R ) async throws -> R . Response {
195
+ guard let sourceKitLSPServer else {
196
+ // `SourceKitLSPServer` has been destructed. We are tearing down the
197
+ // language server. Nothing left to do.
198
+ throw ResponseError . unknown ( " Connection to the editor closed " )
199
+ }
187
200
await sourceKitLSPServer. waitUntilInitialized ( )
188
201
return try await sourceKitLSPServer. sendRequestToClient ( request)
189
202
}
190
203
191
204
/// Whether the client can handle `WorkDoneProgress` requests.
192
205
var clientSupportsWorkDoneProgress : Bool {
193
206
get async {
194
- await sourceKitLSPServer. capabilityRegistry? . clientCapabilities. window? . workDoneProgress ?? false
207
+ await sourceKitLSPServer? . capabilityRegistry? . clientCapabilities. window? . workDoneProgress ?? false
195
208
}
196
209
}
197
210
198
211
func watchFiles( _ fileWatchers: [ FileSystemWatcher ] ) async {
199
- await sourceKitLSPServer. watchFiles ( fileWatchers)
212
+ await sourceKitLSPServer? . watchFiles ( fileWatchers)
200
213
}
201
214
}
202
215
0 commit comments