Skip to content

Commit

Permalink
Use ProcessServiceContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
mattmassicotte committed Oct 17, 2022
1 parent daa0235 commit 9499061
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.DS_Store
/.build
/Packages
/*.xcodeproj
xcuserdata/
DerivedData/
.swiftpm/config/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
6 changes: 3 additions & 3 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"location" : "https://github.com/ChimeHQ/ChimeKit",
"state" : {
"branch" : "main",
"revision" : "3dc4c8017f3e0e4ff31f10ba64250fb7229178aa"
"revision" : "ee6f28bb7a9a5787a76741d7fd5da8646af302ad"
}
},
{
Expand Down Expand Up @@ -104,8 +104,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/ChimeHQ/ProcessService",
"state" : {
"revision" : "8481508879714dafeb1c460c93aee180430ada35",
"version" : "0.1.2"
"revision" : "954097a09372303fc13ec1243cfc716efd14df17",
"version" : "0.2.0"
}
}
],
Expand Down
34 changes: 24 additions & 10 deletions Sources/ChimeRust/RustExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,48 @@ import ChimeKit
import ProcessServiceClient

public final class RustExtension {
private static let processHostService: String = "com.chimehq.ChimeKit.ProcessService"

let host: any HostProtocol
private let lspService: LSPService
private let logger: Logger

public init(host: any HostProtocol) {
public init(host: any HostProtocol, processHostServiceName: String?) {
self.host = host
self.logger = Logger(subsystem: "com.chimehq.ChimeRust", category: "RustExtension")
let logger = Logger(subsystem: "com.chimehq.ChimeRust", category: "RustExtension")
self.logger = logger

let filter = LSPService.contextFilter(for: [.rustSource], projectFiles: ["Cargo.toml"])
let paramProvider = { try await RustExtension.provideParams(logger: logger, processHostService: processHostServiceName) }

self.lspService = LSPService(host: host,
contextFilter: filter,
executionParamsProvider: RustExtension.provideParams,
processHostServiceName: nil)
executionParamsProvider: paramProvider,
processHostServiceName: processHostServiceName)
}
}

extension RustExtension {
private static func provideParams() async throws -> Process.ExecutionParameters {
let userEnv = try await HostedProcess.userEnvironment(with: RustExtension.processHostService)
private static func provideParams(logger: Logger, processHostService: String?) async throws -> Process.ExecutionParameters {
let userEnv: [String: String]

if let processHostService = processHostService {
userEnv = try await HostedProcess.userEnvironment(with: processHostService)
} else {
userEnv = ProcessInfo.processInfo.userEnvironment
}

let whichParams = Process.ExecutionParameters(path: "/usr/bin/which", arguments: ["rust-analyzer"], environment: userEnv)

let whichProcess = HostedProcess(named: RustExtension.processHostService, parameters: whichParams)
let data: Data

let data = try await whichProcess.runAndReadStdout()
if let processHostService = processHostService {
let whichProcess = HostedProcess(named: processHostService, parameters: whichParams)

data = try await whichProcess.runAndReadStdout()
} else {
let whichProcess = Process(parameters: whichParams)

data = try whichProcess.runAndReadStdout() ?? Data()
}

guard let output = String(data: data, encoding: .utf8) else {
throw LSPServiceError.serverNotFound
Expand Down

0 comments on commit 9499061

Please sign in to comment.