@@ -288,7 +288,36 @@ public final class SwiftLanguageServer: ToolchainLanguageServer {
288288 }
289289 } )
290290 }
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+
292321 /// Publish diagnostics for the given `snapshot`. We withhold semantic diagnostics if we are using
293322 /// fallback arguments.
294323 ///
@@ -304,31 +333,22 @@ public final class SwiftLanguageServer: ToolchainLanguageServer {
304333 return
305334 }
306335
307- let isFallback = compileCommand? . isFallback ?? true
308-
309336 let stageUID : sourcekitd_uid_t ? = response [ sourcekitd. keys. diagnostic_stage]
310337 let stage = stageUID. flatMap { DiagnosticStage ( $0, sourcekitd: sourcekitd) } ?? . sema
311338
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+ )
332352 }
333353
334354 /// Should be called on self.queue.
@@ -1393,20 +1413,16 @@ extension SwiftLanguageServer {
13931413 skreq [ keys. compilerargs] = compileCommand. compilerArgs
13941414 }
13951415
1396- let supportsCodeDescription = capabilityRegistry. clientHasDiagnosticsCodeDescriptionSupport
1397-
13981416 let handle = self . sourcekitd. send ( skreq, self . queue) { response in
13991417 guard let dict = response. success else {
14001418 return completion ( . failure( ResponseError ( response. failure!) ) )
14011419 }
14021420
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+ )
14101426
14111427 completion ( . success( diagnostics) )
14121428 }
0 commit comments