Skip to content

Commit 501a8c5

Browse files
committed
Add a send method to InProcessSourceKitLSPClient in which the client specifies the request ID
1 parent 086f479 commit 501a8c5

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

Sources/InProcessClient/InProcessSourceKitLSPClient.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import BuildSystemIntegration
1414
public import Foundation
1515
public import LanguageServerProtocol
1616
import LanguageServerProtocolExtensions
17+
import SKLogging
1718
import SwiftExtensions
1819
import TSCExtensions
1920

@@ -35,6 +36,8 @@ public final class InProcessSourceKitLSPClient: Sendable {
3536

3637
private let nextRequestID = AtomicUInt32(initialValue: 0)
3738

39+
private let requestIDPrefix = "inproc-"
40+
3841
public convenience init(
3942
toolchainPath: URL?,
4043
capabilities: ClientCapabilities = ClientCapabilities(),
@@ -124,11 +127,33 @@ public final class InProcessSourceKitLSPClient: Sendable {
124127
_ request: R,
125128
reply: @Sendable @escaping (LSPResult<R.Response>) -> Void
126129
) -> RequestID {
127-
let requestID = RequestID.number(Int(nextRequestID.fetchAndIncrement()))
130+
let requestID = RequestID.string("\(requestIDPrefix)\(Int(nextRequestID.fetchAndIncrement()))")
128131
server.handle(request, id: requestID, reply: reply)
129132
return requestID
130133
}
131134

135+
/// Send the request to `server` and return the request result via a completion handler.
136+
///
137+
/// The request ID must not start with `inproc-` to avoid conflicting with the request IDs that are created by
138+
/// `send(:reply:)`.
139+
public func send<R: RequestType>(
140+
_ request: R,
141+
requestID: RequestID,
142+
reply: @Sendable @escaping (LSPResult<R.Response>) -> Void
143+
) {
144+
if case .string(let string) = requestID {
145+
if string.starts(with: requestIDPrefix) {
146+
logger.fault(
147+
"""
148+
Manually specified request ID to InProcessSoruceKitLSPClient has reserved prefix \
149+
'\(self.requestIDPrefix, privacy: .public)'
150+
"""
151+
)
152+
}
153+
}
154+
server.handle(request, id: requestID, reply: reply)
155+
}
156+
132157
/// Send the notification to `server`.
133158
public func send(_ notification: some NotificationType) {
134159
server.handle(notification)

0 commit comments

Comments
 (0)