Skip to content

Commit

Permalink
Temporarily remove LanguageClient dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
FastestMolasses committed Jan 27, 2024
1 parent 43e2bc6 commit e8ba847
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 194 deletions.
53 changes: 4 additions & 49 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -14,53 +14,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/ConcurrencyPlus",
"state" : {
"revision" : "b5ba8d5ea6bfe9e43ccc44aa63f9b458057fa0f4",
"version" : "0.4.1"
}
},
{
"identity" : "fseventswrapper",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Frizlab/FSEventsWrapper",
"state" : {
"revision" : "e0c59a2ce2775e5f6642da6d19207445f10112d0",
"version" : "1.0.2"
}
},
{
"identity" : "glob",
"kind" : "remoteSourceControl",
"location" : "https://github.com/Bouke/Glob",
"state" : {
"revision" : "deda6e163d2ff2a8d7e138e2c3326dbd71157faf",
"version" : "1.0.5"
}
},
{
"identity" : "jsonrpc",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/JSONRPC",
"state" : {
"revision" : "afc20d00e38674774f84edc325424a32ae3b9e01",
"version" : "0.7.0"
}
},
{
"identity" : "languageclient",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/LanguageClient",
"state" : {
"revision" : "92beeecd0bb783da52227839ba6c55e43fc866ec",
"version" : "0.5.1"
}
},
{
"identity" : "languageserverprotocol",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/LanguageServerProtocol",
"state" : {
"revision" : "192bcfdcf7a013da49c6fa1b95de66254ce7c614",
"version" : "0.9.1"
"revision" : "8dc56499412a373d617d50d059116bccf44b9874",
"version" : "0.4.2"
}
},
{
Expand All @@ -86,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/lukepistrol/SwiftLintPlugin",
"state" : {
"revision" : "d3ec7fb242ebe1d8e23bf17e58a1e27d43125994",
"version" : "0.2.6"
"revision" : "ea6d3ca895b49910f790e98e4b4ca658e0fe490e",
"version" : "0.54.0"
}
}
],
Expand Down
3 changes: 1 addition & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/ChimeHQ/ConcurrencyPlus", from: "0.4.1"),
.package(url: "https://github.com/ChimeHQ/LanguageClient", from: "0.5.0"),
.package(url: "https://github.com/ChimeHQ/ProcessService", from: "0.2.6"),
.package(
url: "https://github.com/lukepistrol/SwiftLintPlugin",
Expand All @@ -29,7 +28,7 @@ let package = Package(
targets: [
.target(
name: "CodeEditKit",
dependencies: ["AnyCodable", "ConcurrencyPlus", "LanguageClient", .product(name: "ProcessServiceClient", package: "ProcessService")],
dependencies: ["AnyCodable", "ConcurrencyPlus", .product(name: "ProcessServiceClient", package: "ProcessService")],
plugins: [.plugin(name: "SwiftLint", package: "SwiftLintPlugin")]
),
.testTarget(
Expand Down
286 changes: 143 additions & 143 deletions Sources/CodeEditKit/NonUIExtensions/LSP/LanguageExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,146 +5,146 @@
// Created by Wouter Hennen on 27/03/2023.
//

import Foundation
import LanguageClient
import Combine
import OSLog
import ProcessServiceClient
import ConcurrencyPlus
import LanguageServerProtocol
import ProcessEnv
import JSONRPC

protocol LanguageExtension {
var client: RemoteLanguageServer { get }

}

final class UnrestrictedProcessTransport {
private var readHandler: ReadHandler = { _ in }
private let process: HostedProcess
private let taskQueue = TaskQueue()
private var subscription: AnyCancellable?
private let logger = Logger(subsystem: "com.chimehq.ChimeKit", category: "UnrestrictedProcessTransport")

init(process: HostedProcess) {
self.process = process
}

func beginMonitoringProcess() async throws {
let task = taskQueue.addOperation {
self.subscription = try await self.process.processEventPublisher
.sink(receiveCompletion: { _ in
}, receiveValue: { [weak self] event in
switch event {
case .stdout(let data):
self?.readHandler(data)
case .stderr(let data):
let output = String(data: data, encoding: .utf8) ?? ""

self?.logger.info("stderr: \(output, privacy: .public)")
default:
break
}
})
}

try await task.value
}
}

extension UnrestrictedProcessTransport: DataTransport {
func write(_ data: Data) {
taskQueue.addOperation {
try await self.process.write(data)
}
}

func setReaderHandler(_ handler: @escaping ReadHandler) {
self.readHandler = handler
}

func close() {
subscription?.cancel()
}
}

/// Provides an interface to a LSP language server hosted by an intermediary process.
public class RemoteLanguageServer {
private let wrappedServer: JSONRPCLanguageServer
private var subscription: AnyCancellable?
private let logger = Logger(subsystem: "com.chimehq.ChimeKit", category: "RemoteLanguageServer")

private let process: HostedProcess
private let taskQueue = TaskQueue()
public var terminationHandler: (() -> Void)?

init(named serviceName: String, parameters: Process.ExecutionParameters) throws {
self.process = HostedProcess(named: serviceName, parameters: parameters)
let transport = UnrestrictedProcessTransport(process: process)
self.wrappedServer = JSONRPCLanguageServer(dataTransport: transport)

taskQueue.addOperation {
self.logger.debug("launching remote server")

do {
try await self.process.launch()

self.subscription = try await self.process.processEventPublisher
.sink(receiveCompletion: { _ in

}, receiveValue: { event in
switch event {
case .terminated:
self.terminationHandler?()
default:
break
}
})

try await transport.beginMonitoringProcess()
} catch {
self.logger.error("failed to launch: \(String(describing: error), privacy: .public)")
}
}
}

private func stopProcess() {
self.taskQueue.addOperation {
do {
try await self.process.terminate()
} catch {
self.logger.error("failed to terminate: \(String(describing: error), privacy: .public)")
}
}
}

public var logMessages: Bool {
get { return wrappedServer.logMessages }
set { wrappedServer.logMessages = newValue }
}
}

extension RemoteLanguageServer: LanguageServerProtocol.Server {
public func setHandlers(_ handlers: ServerHandlers, completionHandler: @escaping (ServerError?) -> Void) {
wrappedServer.setHandlers(handlers, completionHandler: completionHandler)
}

public func sendNotification(_ notif: ClientNotification, completionHandler: @escaping (ServerError?) -> Void) {
taskQueue.addOperation {
self.wrappedServer.sendNotification(notif, completionHandler: completionHandler)
}
}

public func sendRequest<Response: Codable>(_ request: ClientRequest, completionHandler: @escaping (ServerResult<Response>) -> Void) {
taskQueue.addOperation {
self.wrappedServer.sendRequest(request, completionHandler: { (result: ServerResult<Response>) in
if case .success = result, case .shutdown = request {
self.stopProcess()
}

completionHandler(result)
})
}
}
}
//import Foundation
//import LanguageClient
//import Combine
//import OSLog
//import ProcessServiceClient
//import ConcurrencyPlus
//import LanguageServerProtocol
//import ProcessEnv
//import JSONRPC
//
//protocol LanguageExtension {
// var client: RemoteLanguageServer { get }
//
//}
//
//final class UnrestrictedProcessTransport {
// private var readHandler: ReadHandler = { _ in }
// private let process: HostedProcess
// private let taskQueue = TaskQueue()
// private var subscription: AnyCancellable?
// private let logger = Logger(subsystem: "com.chimehq.ChimeKit", category: "UnrestrictedProcessTransport")
//
// init(process: HostedProcess) {
// self.process = process
// }
//
// func beginMonitoringProcess() async throws {
// let task = taskQueue.addOperation {
// self.subscription = try await self.process.processEventPublisher
// .sink(receiveCompletion: { _ in
// }, receiveValue: { [weak self] event in
// switch event {
// case .stdout(let data):
// self?.readHandler(data)
// case .stderr(let data):
// let output = String(data: data, encoding: .utf8) ?? ""
//
// self?.logger.info("stderr: \(output, privacy: .public)")
// default:
// break
// }
// })
// }
//
// try await task.value
// }
//}
//
//extension UnrestrictedProcessTransport: DataTransport {
// func write(_ data: Data) {
// taskQueue.addOperation {
// try await self.process.write(data)
// }
// }
//
// func setReaderHandler(_ handler: @escaping ReadHandler) {
// self.readHandler = handler
// }
//
// func close() {
// subscription?.cancel()
// }
//}
//
///// Provides an interface to a LSP language server hosted by an intermediary process.
//public class RemoteLanguageServer {
// private let wrappedServer: JSONRPCLanguageServer
// private var subscription: AnyCancellable?
// private let logger = Logger(subsystem: "com.chimehq.ChimeKit", category: "RemoteLanguageServer")
//
// private let process: HostedProcess
// private let taskQueue = TaskQueue()
// public var terminationHandler: (() -> Void)?
//
// init(named serviceName: String, parameters: Process.ExecutionParameters) throws {
// self.process = HostedProcess(named: serviceName, parameters: parameters)
// let transport = UnrestrictedProcessTransport(process: process)
// self.wrappedServer = JSONRPCLanguageServer(dataTransport: transport)
//
// taskQueue.addOperation {
// self.logger.debug("launching remote server")
//
// do {
// try await self.process.launch()
//
// self.subscription = try await self.process.processEventPublisher
// .sink(receiveCompletion: { _ in
//
// }, receiveValue: { event in
// switch event {
// case .terminated:
// self.terminationHandler?()
// default:
// break
// }
// })
//
// try await transport.beginMonitoringProcess()
// } catch {
// self.logger.error("failed to launch: \(String(describing: error), privacy: .public)")
// }
// }
// }
//
// private func stopProcess() {
// self.taskQueue.addOperation {
// do {
// try await self.process.terminate()
// } catch {
// self.logger.error("failed to terminate: \(String(describing: error), privacy: .public)")
// }
// }
// }
//
// public var logMessages: Bool {
// get { return wrappedServer.logMessages }
// set { wrappedServer.logMessages = newValue }
// }
//}
//
//extension RemoteLanguageServer: LanguageServerProtocol.Server {
// public func setHandlers(_ handlers: ServerHandlers, completionHandler: @escaping (ServerError?) -> Void) {
// wrappedServer.setHandlers(handlers, completionHandler: completionHandler)
// }
//
// public func sendNotification(_ notif: ClientNotification, completionHandler: @escaping (ServerError?) -> Void) {
// taskQueue.addOperation {
// self.wrappedServer.sendNotification(notif, completionHandler: completionHandler)
// }
// }
//
// public func sendRequest<Response: Codable>(_ request: ClientRequest, completionHandler: @escaping (ServerResult<Response>) -> Void) {
// taskQueue.addOperation {
// self.wrappedServer.sendRequest(request, completionHandler: { (result: ServerResult<Response>) in
// if case .success = result, case .shutdown = request {
// self.stopProcess()
// }
//
// completionHandler(result)
// })
// }
// }
//}

0 comments on commit e8ba847

Please sign in to comment.