Skip to content

Commit 357a3d2

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

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

Sources/InProcessClient/InProcessSourceKitLSPClient.swift

Lines changed: 28 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,35 @@ 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 DEBUG
145+
if case .string(let string) = requestID {
146+
if string.starts(with: requestIDPrefix) {
147+
logger.fault(
148+
"""
149+
Manually specified request ID to InProcessSoruceKitLSPClient has reserved prefix \
150+
'\(self.requestIDPrefix, privacy: .public)'
151+
"""
152+
)
153+
}
154+
}
155+
#endif
156+
server.handle(request, id: requestID, reply: reply)
157+
}
158+
132159
/// Send the notification to `server`.
133160
public func send(_ notification: some NotificationType) {
134161
server.handle(notification)

0 commit comments

Comments
 (0)