Skip to content

Commit 2b0a8fb

Browse files
committed
Make the folding range request return the request result as an async return value
1 parent 2366377 commit 2b0a8fb

File tree

4 files changed

+67
-28
lines changed

4 files changed

+67
-28
lines changed

Sources/SourceKitLSP/Clang/ClangLanguageServer.swift

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,27 @@ actor ClangLanguageServerShim: ToolchainLanguageServer, MessageHandler {
320320
}
321321
}
322322

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

556-
func foldingRange(_ req: Request<FoldingRangeRequest>) {
577+
func foldingRange(_ req: FoldingRangeRequest) async throws -> [FoldingRange]? {
557578
if self.capabilities?.foldingRangeProvider?.isSupported == true {
558-
forwardRequestToClangd(req)
579+
return try await forwardRequestToClangd(req)
559580
} else {
560-
req.reply(.success(nil))
581+
return nil
561582
}
562583
}
563584

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)