Skip to content

Minor improvements to SwiftLanguageServer.swift #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 26, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ extension SwiftLanguageServer {
/// If the client doesn't support pull diagnostics, compute diagnostics for the latest version of the given document
/// and send a `PublishDiagnosticsNotification` to the client for it.
private func publishDiagnosticsIfNeeded(for document: DocumentURI) {
withLoggingScope("publish-diagnostics") {
publishDiagnosticsIfNeededImpl(for: document)
}
}

private func publishDiagnosticsIfNeededImpl(for document: DocumentURI) {
guard enablePublishDiagnostics else {
return
}
Expand All @@ -383,24 +389,31 @@ extension SwiftLanguageServer {
// Sleep for a little bit until triggering the diagnostic generation. This effectively de-bounces diagnostic
// generation since any later edit will cancel the previous in-flight task, which will thus never go on to send
// the `DocumentDiagnosticsRequest`.
try await Task.sleep(nanoseconds: UInt64(sourceKitServer.options.swiftPublishDiagnosticsDebounceDuration * 1_000_000_000))
try await Task.sleep(
nanoseconds: UInt64(sourceKitServer.options.swiftPublishDiagnosticsDebounceDuration * 1_000_000_000)
)
} catch {
return
}
do {
let diagnosticReport = try await self.fullDocumentDiagnosticReport(DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(document)))
let diagnosticReport = try await self.fullDocumentDiagnosticReport(
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(document))
)

await sourceKitServer.sendNotificationToClient(
PublishDiagnosticsNotification(
uri: document,
diagnostics: diagnosticReport.items
)
)
} catch is CancellationError {
} catch {
logger.fault("""
logger.fault(
"""
Failed to get diagnostics
\(error.forLogging)
""")
"""
)
}
}
}
Expand Down Expand Up @@ -1171,7 +1184,9 @@ extension SwiftLanguageServer {
return try await .full(fullDocumentDiagnosticReport(req))
}

private func fullDocumentDiagnosticReport(_ req: DocumentDiagnosticsRequest) async throws -> RelatedFullDocumentDiagnosticReport {
private func fullDocumentDiagnosticReport(
_ req: DocumentDiagnosticsRequest
) async throws -> RelatedFullDocumentDiagnosticReport {
guard let snapshot = documentManager.latestSnapshot(req.textDocument.uri) else {
throw ResponseError.unknown("failed to find snapshot for url \(req.textDocument.uri.forLogging)")
}
Expand Down