Skip to content

Commit 8f859c5

Browse files
committed
Simplify forwarding for requests to clangd
1 parent ebcdbb6 commit 8f859c5

File tree

1 file changed

+22
-35
lines changed

1 file changed

+22
-35
lines changed

Sources/SourceKitLSP/Clang/ClangLanguageServer.swift

Lines changed: 22 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -293,30 +293,17 @@ actor ClangLanguageServerShim: ToolchainLanguageServer, MessageHandler {
293293
}
294294
}
295295

296-
/// Forwards a request to the given connection, taking care of replying to the original request
297-
/// and cancellation, while providing a callback with the response for additional processing.
298-
///
299-
/// Immediately after `handler` returns, this passes the result to the original reply handler by
300-
/// calling `request.reply(result)`.
296+
/// Forwards a request to `clangd`, taking care of replying to the original request
297+
/// and cancellation.
301298
///
302299
/// The cancellation token from the original request is automatically linked to the forwarded
303300
/// request such that cancelling the original request will cancel the forwarded request.
304-
///
305-
/// - Parameters:
306-
/// - request: The request to forward.
307-
/// - to: Where to forward the request (e.g. self.clangd).
308-
/// - handler: An optional closure that will be called with the result of the request.
309-
func forwardRequest<R>(
310-
_ request: Request<R>,
311-
to: Connection,
312-
_ handler: ((LSPResult<R.Response>) -> Void)? = nil)
313-
{
314-
let id = to.send(request.params, queue: clangdCommunicationQueue) { result in
315-
handler?(result)
301+
func forwardRequestToClangd<R>(_ request: Request<R>) {
302+
let id = clangd.send(request.params, queue: clangdCommunicationQueue) { result in
316303
request.reply(result)
317304
}
318305
request.cancellationToken.addCancellationHandler {
319-
to.send(CancelRequestNotification(id: id))
306+
self.clangd.send(CancelRequestNotification(id: id))
320307
}
321308
}
322309

@@ -482,80 +469,80 @@ extension ClangLanguageServerShim {
482469
/// Returns true if the `ToolchainLanguageServer` will take ownership of the request.
483470
public func definition(_ req: Request<DefinitionRequest>) -> Bool {
484471
// We handle it to provide jump-to-header support for #import/#include.
485-
self.forwardRequest(req, to: self.clangd)
472+
self.forwardRequestToClangd(req)
486473
return true
487474
}
488475

489476
/// Returns true if the `ToolchainLanguageServer` will take ownership of the request.
490477
public func declaration(_ req: Request<DeclarationRequest>) -> Bool {
491478
// We handle it to provide jump-to-header support for #import/#include.
492-
forwardRequest(req, to: clangd)
479+
forwardRequestToClangd(req)
493480
return true
494481
}
495482

496483
func completion(_ req: Request<CompletionRequest>) {
497-
forwardRequest(req, to: clangd)
484+
forwardRequestToClangd(req)
498485
}
499486

500487
func hover(_ req: Request<HoverRequest>) {
501-
forwardRequest(req, to: clangd)
488+
forwardRequestToClangd(req)
502489
}
503490

504491
func symbolInfo(_ req: Request<SymbolInfoRequest>) {
505-
forwardRequest(req, to: clangd)
492+
forwardRequestToClangd(req)
506493
}
507494

508495
func documentSymbolHighlight(_ req: Request<DocumentHighlightRequest>) {
509-
forwardRequest(req, to: clangd)
496+
forwardRequestToClangd(req)
510497
}
511498

512499
func documentSymbol(_ req: Request<DocumentSymbolRequest>) {
513-
forwardRequest(req, to: clangd)
500+
forwardRequestToClangd(req)
514501
}
515502

516503
func documentColor(_ req: Request<DocumentColorRequest>) {
517504
if self.capabilities?.colorProvider?.isSupported == true {
518-
forwardRequest(req, to: clangd)
505+
forwardRequestToClangd(req)
519506
} else {
520507
req.reply(.success([]))
521508
}
522509
}
523510

524511
func documentSemanticTokens(_ req: Request<DocumentSemanticTokensRequest>) {
525-
forwardRequest(req, to: clangd)
512+
forwardRequestToClangd(req)
526513
}
527514

528515
func documentSemanticTokensDelta(_ req: Request<DocumentSemanticTokensDeltaRequest>) {
529-
forwardRequest(req, to: clangd)
516+
forwardRequestToClangd(req)
530517
}
531518

532519
func documentSemanticTokensRange(_ req: Request<DocumentSemanticTokensRangeRequest>) {
533-
forwardRequest(req, to: clangd)
520+
forwardRequestToClangd(req)
534521
}
535522

536523
func colorPresentation(_ req: Request<ColorPresentationRequest>) {
537524
if self.capabilities?.colorProvider?.isSupported == true {
538-
forwardRequest(req, to: clangd)
525+
forwardRequestToClangd(req)
539526
} else {
540527
req.reply(.success([]))
541528
}
542529
}
543530

544531
func codeAction(_ req: Request<CodeActionRequest>) {
545-
forwardRequest(req, to: clangd)
532+
forwardRequestToClangd(req)
546533
}
547534

548535
func inlayHint(_ req: Request<InlayHintRequest>) {
549-
forwardRequest(req, to: clangd)
536+
forwardRequestToClangd(req)
550537
}
551538

552539
func documentDiagnostic(_ req: Request<DocumentDiagnosticsRequest>) {
553-
forwardRequest(req, to: clangd)
540+
forwardRequestToClangd(req)
554541
}
555542

556543
func foldingRange(_ req: Request<FoldingRangeRequest>) {
557544
if self.capabilities?.foldingRangeProvider?.isSupported == true {
558-
forwardRequest(req, to: clangd)
545+
forwardRequestToClangd(req)
559546
} else {
560547
req.reply(.success(nil))
561548
}
@@ -568,7 +555,7 @@ extension ClangLanguageServerShim {
568555
// MARK: - Other
569556

570557
func executeCommand(_ req: Request<ExecuteCommandRequest>) {
571-
forwardRequest(req, to: clangd)
558+
forwardRequestToClangd(req)
572559
}
573560
}
574561

0 commit comments

Comments
 (0)