Skip to content

Commit

Permalink
Remove synchronous properties from Server protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Jan 11, 2023
1 parent 57f38c3 commit 7bea600
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ let package = Package(
targets: ["LanguageServerProtocol"]),
],
dependencies: [
.package(url: "https://github.com/ChimeHQ/JSONRPC", from: "0.6.1"),
.package(url: "https://github.com/ChimeHQ/JSONRPC", from: "0.7.0"),
],
targets: [
.target(
Expand Down
8 changes: 7 additions & 1 deletion Sources/LanguageServerProtocol/JSONRPCLanguageServer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public class JSONRPCLanguageServer: Server {
get { return protocolTransport.logMessages }
set { protocolTransport.logMessages = newValue }
}

public func setHandlers(_ handlers: ServerHandlers, completionHandler: @escaping (ServerError?) -> Void) {
self.notificationHandler = handlers.notificationHandler
self.requestHandler = handlers.requestHandler

completionHandler(nil)
}
}

extension JSONRPCLanguageServer {
Expand Down Expand Up @@ -417,4 +424,3 @@ extension JSONRPCLanguageServer {
}
}
}

33 changes: 30 additions & 3 deletions Sources/LanguageServerProtocol/Server.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,45 @@ public enum ServerError: LocalizedError {

public typealias ServerResult<T: Codable> = Result<T, ServerError>

public struct ServerHandlers {
public var requestHandler: Server.RequestHandler?
public var notificationHandler: Server.NotificationHandler?

public init(requestHandler: Server.RequestHandler? = nil, notificationHandler: Server.NotificationHandler? = nil) {
self.requestHandler = requestHandler
self.notificationHandler = notificationHandler
}
}

public protocol Server {
typealias RequestHandler = (ServerRequest, @escaping (ServerResult<LSPAny>) -> Void) -> Void
typealias NotificationHandler = (ServerNotification, @escaping (ServerError?) -> Void) -> Void
typealias ResponseHandler<T: Codable> = (ServerResult<JSONRPCResponse<T>>) -> Void

var requestHandler: RequestHandler? { get set }
var notificationHandler: NotificationHandler? { get set }

func setHandlers(_ handlers: ServerHandlers, completionHandler: @escaping (ServerError?) -> Void)
func sendNotification(_ notif: ClientNotification, completionHandler: @escaping (ServerError?) -> Void)
func sendRequest<Response: Codable>(_ request: ClientRequest, completionHandler: @escaping (ServerResult<Response>) -> Void)
}

public extension Server {
func setHandlers(_ handlers: ServerHandlers) {
setHandlers(handlers, completionHandler: { _ in })
}

@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *)
func setHandlers(_ handlers: ServerHandlers) async throws {
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
setHandlers(handlers) { error in
if let error = error {
continuation.resume(throwing: error)
} else {
continuation.resume()
}
}
}
}
}

extension Server {
func sendRequestWithErrorOnlyResult(_ request: ClientRequest, completionHandler: @escaping (ServerError?) -> Void) {
sendRequest(request) { (result: ServerResult<UnusedResult>) in
Expand Down

0 comments on commit 7bea600

Please sign in to comment.