@@ -288,7 +288,36 @@ public final class SwiftLanguageServer: ToolchainLanguageServer {
288
288
}
289
289
} )
290
290
}
291
-
291
+
292
+ /// Register the diagnostics returned from sourcekitd in `currentDiagnostics`
293
+ /// and returns the corresponding LSP diagnostics.
294
+ private func registerDiagnostics(
295
+ sourcekitdDiagnostics: SKDResponseArray ? ,
296
+ snapshot: DocumentSnapshot ,
297
+ stage: DiagnosticStage
298
+ ) -> [ Diagnostic ] {
299
+ let supportsCodeDescription = capabilityRegistry. clientHasDiagnosticsCodeDescriptionSupport
300
+
301
+ var newDiags : [ CachedDiagnostic ] = [ ]
302
+ sourcekitdDiagnostics? . forEach { _, diag in
303
+ if let diag = CachedDiagnostic ( diag, in: snapshot, useEducationalNoteAsCode: supportsCodeDescription) {
304
+ newDiags. append ( diag)
305
+ }
306
+ return true
307
+ }
308
+
309
+ let result = mergeDiagnostics (
310
+ old: currentDiagnostics [ snapshot. document. uri] ?? [ ] ,
311
+ new: newDiags,
312
+ stage: stage,
313
+ isFallback: self . commandsByFile [ snapshot. document. uri] ? . isFallback ?? true
314
+ )
315
+ currentDiagnostics [ snapshot. document. uri] = result
316
+
317
+ return result. map ( \. diagnostic)
318
+
319
+ }
320
+
292
321
/// Publish diagnostics for the given `snapshot`. We withhold semantic diagnostics if we are using
293
322
/// fallback arguments.
294
323
///
@@ -304,31 +333,22 @@ public final class SwiftLanguageServer: ToolchainLanguageServer {
304
333
return
305
334
}
306
335
307
- let isFallback = compileCommand? . isFallback ?? true
308
-
309
336
let stageUID : sourcekitd_uid_t ? = response [ sourcekitd. keys. diagnostic_stage]
310
337
let stage = stageUID. flatMap { DiagnosticStage ( $0, sourcekitd: sourcekitd) } ?? . sema
311
338
312
- let supportsCodeDescription = capabilityRegistry. clientHasDiagnosticsCodeDescriptionSupport
313
-
314
- // Note: we make the notification even if there are no diagnostics to clear the current state.
315
- var newDiags : [ CachedDiagnostic ] = [ ]
316
- response [ keys. diagnostics] ? . forEach { _, diag in
317
- if let diag = CachedDiagnostic ( diag,
318
- in: snapshot,
319
- useEducationalNoteAsCode: supportsCodeDescription) {
320
- newDiags. append ( diag)
321
- }
322
- return true
323
- }
324
-
325
- let result = mergeDiagnostics (
326
- old: currentDiagnostics [ documentUri] ?? [ ] ,
327
- new: newDiags, stage: stage, isFallback: isFallback)
328
- currentDiagnostics [ documentUri] = result
329
-
330
- client. send ( PublishDiagnosticsNotification (
331
- uri: documentUri, version: snapshot. version, diagnostics: result. map { $0. diagnostic } ) )
339
+ let diagnostics = registerDiagnostics (
340
+ sourcekitdDiagnostics: response [ keys. diagnostics] ,
341
+ snapshot: snapshot,
342
+ stage: stage
343
+ )
344
+
345
+ client. send (
346
+ PublishDiagnosticsNotification (
347
+ uri: documentUri,
348
+ version: snapshot. version,
349
+ diagnostics: diagnostics
350
+ )
351
+ )
332
352
}
333
353
334
354
/// Should be called on self.queue.
@@ -1393,20 +1413,16 @@ extension SwiftLanguageServer {
1393
1413
skreq [ keys. compilerargs] = compileCommand. compilerArgs
1394
1414
}
1395
1415
1396
- let supportsCodeDescription = capabilityRegistry. clientHasDiagnosticsCodeDescriptionSupport
1397
-
1398
1416
let handle = self . sourcekitd. send ( skreq, self . queue) { response in
1399
1417
guard let dict = response. success else {
1400
1418
return completion ( . failure( ResponseError ( response. failure!) ) )
1401
1419
}
1402
1420
1403
- var diagnostics : [ Diagnostic ] = [ ]
1404
- dict [ keys. diagnostics] ? . forEach { _, diag in
1405
- if let diagnostic = Diagnostic ( diag, in: snapshot, useEducationalNoteAsCode: supportsCodeDescription) {
1406
- diagnostics. append ( diagnostic)
1407
- }
1408
- return true
1409
- }
1421
+ let diagnostics = self . registerDiagnostics (
1422
+ sourcekitdDiagnostics: dict [ keys. diagnostics] ,
1423
+ snapshot: snapshot,
1424
+ stage: . sema
1425
+ )
1410
1426
1411
1427
completion ( . success( diagnostics) )
1412
1428
}
0 commit comments