diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b29812 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +.DS_Store +/.build +/Packages +/*.xcodeproj +xcuserdata/ +DerivedData/ +.swiftpm/config/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Package.resolved b/Package.resolved index ff7875a..9ab9738 100644 --- a/Package.resolved +++ b/Package.resolved @@ -15,7 +15,7 @@ "location" : "https://github.com/ChimeHQ/ChimeKit", "state" : { "branch" : "main", - "revision" : "3dc4c8017f3e0e4ff31f10ba64250fb7229178aa" + "revision" : "ee6f28bb7a9a5787a76741d7fd5da8646af302ad" } }, { @@ -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" } } ], diff --git a/Sources/ChimeRust/RustExtension.swift b/Sources/ChimeRust/RustExtension.swift index ba5224d..3dea50e 100644 --- a/Sources/ChimeRust/RustExtension.swift +++ b/Sources/ChimeRust/RustExtension.swift @@ -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