Skip to content

Commit 1b6015f

Browse files
committed
Make the folding range request return the request result as an async return value
1 parent 1f02b95 commit 1b6015f

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

Sources/SourceKitLSP/Clang/ClangLanguageServer.swift

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,6 @@ actor ClangLanguageServerShim: ToolchainLanguageServer, MessageHandler {
4747

4848
/// The queue on which all messages that originate from clangd are handled.
4949
///
50-
/// This includes requests and notifications sent *from* clangd and does not
51-
/// include replies from clangd.
52-
///
5350
/// These are requests and notifications sent *from* clangd, not replies from
5451
/// clangd.
5552
///
@@ -320,6 +317,27 @@ actor ClangLanguageServerShim: ToolchainLanguageServer, MessageHandler {
320317
}
321318
}
322319

320+
/// Forward the given request to `clangd`.
321+
///
322+
/// This method calls `readyToHandleNextRequest` once the request has been
323+
/// transmitted to `clangd` and another request can be safely transmitted to
324+
/// `clangd` while guaranteeing ordering.
325+
///
326+
/// The response of the request is returned asynchronously as the return value.
327+
func forwardRequestToClangd<R: RequestType>(_ request: R) async throws -> R.Response {
328+
try await withCheckedThrowingContinuation { continuation in
329+
_ = clangd.send(request, queue: clangdCommunicationQueue) { result in
330+
switch result {
331+
case .success(let response):
332+
continuation.resume(returning: response)
333+
case .failure(let error):
334+
continuation.resume(throwing: error)
335+
}
336+
}
337+
}
338+
// FIXME: (async) Cancellation
339+
}
340+
323341
func _crash() {
324342
// Since `clangd` doesn't have a method to crash it, kill it.
325343
#if os(Windows)
@@ -553,12 +571,11 @@ extension ClangLanguageServerShim {
553571
forwardRequestToClangd(req)
554572
}
555573

556-
func foldingRange(_ req: Request<FoldingRangeRequest>) {
557-
if self.capabilities?.foldingRangeProvider?.isSupported == true {
558-
forwardRequestToClangd(req)
559-
} else {
560-
req.reply(.success(nil))
574+
func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
575+
guard self.capabilities?.foldingRangeProvider?.isSupported ?? false else {
576+
return nil
561577
}
578+
return try await forwardRequestToClangd(req)
562579
}
563580

564581
func openInterface(_ request: Request<OpenInterfaceRequest>) {

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,32 @@ public actor SourceKitServer {
269269
await requestHandler(request, workspace, languageService)
270270
}
271271

272+
private func withLanguageServiceAndWorkspace<RequestType: TextDocumentRequest>(
273+
for request: Request<RequestType>,
274+
requestHandler: @escaping (RequestType, Workspace, ToolchainLanguageServer) async throws -> RequestType.Response,
275+
fallback: RequestType.Response
276+
) async {
277+
let doc = request.params.textDocument.uri
278+
guard let workspace = await self.workspaceForDocument(uri: doc) else {
279+
return request.reply(.failure(.workspaceNotOpen(doc)))
280+
}
281+
282+
guard let languageService = workspace.documentService[doc] else {
283+
return request.reply(fallback)
284+
}
285+
286+
do {
287+
let result = try await requestHandler(request.params, workspace, languageService)
288+
request.reply(.success(result))
289+
} catch let error as ResponseError {
290+
request.reply(.failure(error))
291+
} catch is CancellationError {
292+
request.reply(.failure(.cancelled))
293+
} catch {
294+
request.reply(.failure(.unknown("Unknown error: \(error)")))
295+
}
296+
}
297+
272298

273299
/// Send the given notification to the editor.
274300
public func sendNotificationToClient(_ notification: some NotificationType) {
@@ -1253,10 +1279,11 @@ extension SourceKitServer {
12531279
}
12541280

12551281
func foldingRange(
1256-
_ req: Request<FoldingRangeRequest>,
1282+
_ req: FoldingRangeRequest,
12571283
workspace: Workspace,
1258-
languageService: ToolchainLanguageServer) async {
1259-
await languageService.foldingRange(req)
1284+
languageService: ToolchainLanguageServer
1285+
) async throws -> [FoldingRange]? {
1286+
return try await languageService.foldingRange(req)
12601287
}
12611288

12621289
func documentSymbol(
@@ -1322,13 +1349,6 @@ extension SourceKitServer {
13221349
return
13231350
}
13241351

1325-
await self.fowardExecuteCommand(req, languageService: languageService)
1326-
}
1327-
1328-
func fowardExecuteCommand(
1329-
_ req: Request<ExecuteCommandRequest>,
1330-
languageService: ToolchainLanguageServer
1331-
) async {
13321352
let params = req.params
13331353
let executeCommand = ExecuteCommandRequest(command: params.command,
13341354
arguments: params.argumentsWithoutSourceKitMetadata)

Sources/SourceKitLSP/Swift/SwiftLanguageServer.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -984,13 +984,12 @@ extension SwiftLanguageServer {
984984
_ = handle
985985
}
986986

987-
public func foldingRange(_ req: Request<FoldingRangeRequest>) async {
987+
public func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
988988
let foldingRangeCapabilities = capabilityRegistry.clientCapabilities.textDocument?.foldingRange
989-
let uri = req.params.textDocument.uri
989+
let uri = req.textDocument.uri
990990
guard var snapshot = self.documentManager.latestSnapshot(uri) else {
991-
log("failed to find snapshot for url \(req.params.textDocument.uri)")
992-
req.reply(nil)
993-
return
991+
log("failed to find snapshot for url \(req.textDocument.uri)")
992+
return nil
994993
}
995994

996995
// FIXME: (async) We might not have computed the syntax tree yet. Wait until we have a syntax tree.
@@ -1000,16 +999,14 @@ extension SwiftLanguageServer {
1000999
if let newSnapshot = documentManager.latestSnapshot(uri) {
10011000
snapshot = newSnapshot
10021001
} else {
1003-
log("failed to find snapshot for url \(req.params.textDocument.uri)")
1004-
req.reply(nil)
1005-
return
1002+
log("failed to find snapshot for url \(req.textDocument.uri)")
1003+
return nil
10061004
}
10071005
}
10081006

10091007
guard let sourceFile = snapshot.tokens.syntaxTree else {
1010-
log("no lexical structure available for url \(req.params.textDocument.uri)")
1011-
req.reply(nil)
1012-
return
1008+
log("no lexical structure available for url \(req.textDocument.uri)")
1009+
return nil
10131010
}
10141011

10151012
final class FoldingRangeFinder: SyntaxVisitor {
@@ -1205,10 +1202,11 @@ extension SwiftLanguageServer {
12051202
}
12061203
}
12071204

1205+
try Task.checkCancellation()
1206+
12081207
// If the limit is less than one, do nothing.
12091208
if let limit = foldingRangeCapabilities?.rangeLimit, limit <= 0 {
1210-
req.reply([])
1211-
return
1209+
return []
12121210
}
12131211

12141212
let rangeFinder = FoldingRangeFinder(
@@ -1218,7 +1216,7 @@ extension SwiftLanguageServer {
12181216
rangeFinder.walk(sourceFile)
12191217
let ranges = rangeFinder.finalize()
12201218

1221-
req.reply(ranges.sorted())
1219+
return ranges.sorted()
12221220
}
12231221

12241222
public func codeAction(_ req: Request<CodeActionRequest>) async {

Sources/SourceKitLSP/ToolchainLanguageServer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public protocol ToolchainLanguageServer: AnyObject {
8888
func declaration(_ request: Request<DeclarationRequest>) async -> Bool
8989

9090
func documentSymbolHighlight(_ req: Request<DocumentHighlightRequest>) async
91-
func foldingRange(_ req: Request<FoldingRangeRequest>) async
91+
func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]?
9292
func documentSymbol(_ req: Request<DocumentSymbolRequest>) async
9393
func documentColor(_ req: Request<DocumentColorRequest>) async
9494
func documentSemanticTokens(_ req: Request<DocumentSemanticTokensRequest>) async

0 commit comments

Comments
 (0)