Skip to content

Revert asyncificaiton changes #846

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 1 commit into from
Sep 30, 2023
Merged
Show file tree
Hide file tree
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
16 changes: 0 additions & 16 deletions Sources/LSPLogging/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,22 +62,6 @@ public func orLog<R>(
}
}

/// Like `try?`, but logs the error on failure.
public func orLog<R>(
_ prefix: String = "",
level: LogLevel = .default,
logger: Logger = Logger.shared,
_ block: () async throws -> R?) async -> R?
{
do {
return try await block()
} catch {
logger.log("\(prefix)\(prefix.isEmpty ? "" : " ")\(error)", level: level)
return nil
}
}


/// Logs the time that the given block takes to execute in milliseconds.
public func logExecutionTime<R>(
_ prefix: String = #function,
Expand Down
18 changes: 18 additions & 0 deletions Sources/LSPTestSupport/AssertNoThrow.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import XCTest

/// Same as `XCTAssertThrows` but executes the trailing closure.
public func assertNoThrow<T>(_ expression: () throws -> T, _ message: @autoclosure () -> String = "", file: StaticString = #filePath, line: UInt = #line) {
XCTAssertNoThrow(try expression(), message(), file: file, line: line)
}
105 changes: 0 additions & 105 deletions Sources/LSPTestSupport/Assertions.swift

This file was deleted.

87 changes: 0 additions & 87 deletions Sources/LanguageServerProtocol/AsyncQueue.swift

This file was deleted.

1 change: 0 additions & 1 deletion Sources/LanguageServerProtocol/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
add_library(LanguageServerProtocol STATIC
AsyncQueue.swift
Cancellation.swift
Connection.swift
CustomCodable.swift
Expand Down
57 changes: 12 additions & 45 deletions Sources/LanguageServerProtocol/Connection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,10 @@ extension Connection {
public protocol MessageHandler: AnyObject {

/// Handle a notification without a reply.
///
/// The method should return as soon as the notification has been sufficiently
/// handled to avoid out-of-order requests, e.g. once the notification has
/// been forwarded to clangd.
func handle(_ params: some NotificationType, from clientID: ObjectIdentifier) async
func handle(_ params: some NotificationType, from clientID: ObjectIdentifier)

/// Handle a request and (asynchronously) receive a reply.
///
/// The method should return as soon as the request has been sufficiently
/// handled to avoid out-of-order requests, e.g. once the corresponding
/// request has been sent to sourcekitd. The actual semantic computation
/// should occur after the method returns and report the result via `reply`.
func handle<Request: RequestType>(_ params: Request, id: RequestID, from clientID: ObjectIdentifier, reply: @escaping (LSPResult<Request.Response>) -> Void) async
func handle<Request: RequestType>(_ params: Request, id: RequestID, from clientID: ObjectIdentifier, reply: @escaping (LSPResult<Request.Response>) -> Void)
}

/// A connection between two message handlers in the same process.
Expand All @@ -75,20 +66,8 @@ public final class LocalConnection {
case ready, started, closed
}

/// The queue guarding `_nextRequestID`.
let queue: DispatchQueue = DispatchQueue(label: "local-connection-queue")

/// The queue on which all messages (notifications, requests, responses) are
/// handled.
///
/// The queue is blocked until the message has been sufficiently handled to
/// avoid out-of-order handling of messages. For sourcekitd, this means that
/// a request has been sent to sourcekitd and for clangd, this means that we
/// have forwarded the request to clangd.
///
/// The actual semantic handling of the message happens off this queue.
let messageHandlingQueue: AsyncQueue = AsyncQueue()

var _nextRequestID: Int = 0

var state: State = .ready
Expand Down Expand Up @@ -125,34 +104,22 @@ public final class LocalConnection {

extension LocalConnection: Connection {
public func send<Notification>(_ notification: Notification) where Notification: NotificationType {
messageHandlingQueue.async {
await self.handler?.handle(notification, from: ObjectIdentifier(self))
}
handler?.handle(notification, from: ObjectIdentifier(self))
}

public func send<Request: RequestType>(
_ request: Request,
queue: DispatchQueue,
reply: @escaping (LSPResult<Request.Response>) -> Void
) -> RequestID {
public func send<Request>(_ request: Request, queue: DispatchQueue, reply: @escaping (LSPResult<Request.Response>) -> Void) -> RequestID where Request: RequestType {
let id = nextRequestID()
guard let handler = handler else {
queue.async { reply(.failure(.serverCancelled)) }
return id
}

messageHandlingQueue.async {
guard let handler = self.handler else {
queue.async {
reply(.failure(.serverCancelled))
}
return
}

precondition(self.state == .started)
await handler.handle(request, id: id, from: ObjectIdentifier(self)) { result in
queue.async {
reply(result)
}
precondition(state == .started)
handler.handle(request, id: id, from: ObjectIdentifier(self)) { result in
queue.async {
reply(result)
}
}

return id
}
}
10 changes: 5 additions & 5 deletions Sources/LanguageServerProtocol/Message.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public protocol _RequestType: MessageType {
id: RequestID,
connection: Connection,
reply: @escaping (LSPResult<ResponseType>, RequestID) -> Void
) async
)
}

/// A request, which must have a unique `method` name as well as an associated response type.
Expand All @@ -54,16 +54,16 @@ extension RequestType {
id: RequestID,
connection: Connection,
reply: @escaping (LSPResult<ResponseType>, RequestID) -> Void
) async {
await handler.handle(self, id: id, from: ObjectIdentifier(connection)) { response in
) {
handler.handle(self, id: id, from: ObjectIdentifier(connection)) { response in
reply(response.map({ $0 as ResponseType }), id)
}
}
}

extension NotificationType {
public func _handle(_ handler: MessageHandler, connection: Connection) async {
await handler.handle(self, from: ObjectIdentifier(connection))
public func _handle(_ handler: MessageHandler, connection: Connection) {
handler.handle(self, from: ObjectIdentifier(connection))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//===----------------------------------------------------------------------===//

public typealias CodeActionProviderCompletion = (LSPResult<[CodeAction]>) -> Void
public typealias CodeActionProvider = (CodeActionRequest, @escaping CodeActionProviderCompletion) async -> Void
public typealias CodeActionProvider = (CodeActionRequest, @escaping CodeActionProviderCompletion) -> Void

/// Request for returning all possible code actions for a given text document and range.
///
Expand Down
Loading