Skip to content

Commit 3007d9f

Browse files
committed
Naming improvements, added comments and typo fixes in connection related code
This should make the code easier to understand. No functionality change.
1 parent 7c46df3 commit 3007d9f

File tree

6 files changed

+70
-59
lines changed

6 files changed

+70
-59
lines changed

Sources/LSPTestSupport/TestJSONRPCConnection.swift

Lines changed: 48 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -20,55 +20,68 @@ import class Foundation.Pipe
2020
public final class TestJSONRPCConnection {
2121
public let clientToServer: Pipe = Pipe()
2222
public let serverToClient: Pipe = Pipe()
23-
public let client: TestMessageHandler
24-
public let clientConnection: JSONRPCConnection
23+
24+
/// Mocks a client (aka. editor) that can send requests to the LSP server.
25+
public let client: TestClient
26+
27+
/// The connection with which the client can send requests and notifications to the LSP server and using which it
28+
/// receives replies to the requests.
29+
public let clientToServerConnection: JSONRPCConnection
30+
31+
/// Mocks an LSP server that handles requests from the client.
2532
public let server: TestServer
26-
public let serverConnection: JSONRPCConnection
33+
34+
/// The connection with which the server can send requests and notifications to the client and using which it
35+
/// receives replies to the requests.
36+
public let serverToClientConnection: JSONRPCConnection
2737

2838
public init(allowUnexpectedNotification: Bool = true) {
29-
clientConnection = JSONRPCConnection(
39+
clientToServerConnection = JSONRPCConnection(
3040
name: "client",
3141
protocol: testMessageRegistry,
3242
inFD: serverToClient.fileHandleForReading,
3343
outFD: clientToServer.fileHandleForWriting
3444
)
3545

36-
serverConnection = JSONRPCConnection(
46+
serverToClientConnection = JSONRPCConnection(
3747
name: "server",
3848
protocol: testMessageRegistry,
3949
inFD: clientToServer.fileHandleForReading,
4050
outFD: serverToClient.fileHandleForWriting
4151
)
4252

43-
client = TestMessageHandler(server: clientConnection, allowUnexpectedNotification: allowUnexpectedNotification)
44-
server = TestServer(client: serverConnection)
53+
client = TestClient(
54+
connectionToServer: clientToServerConnection,
55+
allowUnexpectedNotification: allowUnexpectedNotification
56+
)
57+
server = TestServer(client: serverToClientConnection)
4558

46-
clientConnection.start(receiveHandler: client) {
59+
clientToServerConnection.start(receiveHandler: client) {
4760
// FIXME: keep the pipes alive until we close the connection. This
4861
// should be fixed systemically.
4962
withExtendedLifetime(self) {}
5063
}
51-
serverConnection.start(receiveHandler: server) {
64+
serverToClientConnection.start(receiveHandler: server) {
5265
// FIXME: keep the pipes alive until we close the connection. This
5366
// should be fixed systemically.
5467
withExtendedLifetime(self) {}
5568
}
5669
}
5770

5871
public func close() {
59-
clientConnection.close()
60-
serverConnection.close()
72+
clientToServerConnection.close()
73+
serverToClientConnection.close()
6174
}
6275
}
6376

6477
public struct TestLocalConnection {
65-
public let client: TestMessageHandler
66-
public let clientConnection: LocalConnection = .init()
78+
public let client: TestClient
79+
public let clientConnection: LocalConnection = LocalConnection()
6780
public let server: TestServer
68-
public let serverConnection: LocalConnection = .init()
81+
public let serverConnection: LocalConnection = LocalConnection()
6982

7083
public init(allowUnexpectedNotification: Bool = true) {
71-
client = TestMessageHandler(server: serverConnection, allowUnexpectedNotification: allowUnexpectedNotification)
84+
client = TestClient(connectionToServer: serverConnection, allowUnexpectedNotification: allowUnexpectedNotification)
7285
server = TestServer(client: clientConnection)
7386

7487
clientConnection.start(handler: client)
@@ -81,18 +94,18 @@ public struct TestLocalConnection {
8194
}
8295
}
8396

84-
public actor TestMessageHandler: MessageHandler {
85-
/// The connection to the language client.
86-
public let server: Connection
97+
public actor TestClient: MessageHandler {
98+
/// The connection to the LSP server.
99+
public let connectionToServer: Connection
87100

88101
private let messageHandlingQueue = AsyncQueue<Serial>()
89102

90103
private var oneShotNotificationHandlers: [((Any) -> Void)] = []
91104

92105
private let allowUnexpectedNotification: Bool
93106

94-
public init(server: Connection, allowUnexpectedNotification: Bool = true) {
95-
self.server = server
107+
public init(connectionToServer: Connection, allowUnexpectedNotification: Bool = true) {
108+
self.connectionToServer = connectionToServer
96109
self.allowUnexpectedNotification = allowUnexpectedNotification
97110
}
98111

@@ -129,20 +142,18 @@ public actor TestMessageHandler: MessageHandler {
129142
) {
130143
reply(.failure(.methodNotFound(Request.method)))
131144
}
132-
}
133145

134-
extension TestMessageHandler: Connection {
135146
/// Send a notification to the LSP server.
136147
public nonisolated func send(_ notification: some NotificationType) {
137-
server.send(notification)
148+
connectionToServer.send(notification)
138149
}
139150

140151
/// Send a request to the LSP server and (asynchronously) receive a reply.
141152
public nonisolated func send<Request: RequestType>(
142153
_ request: Request,
143154
reply: @escaping (LSPResult<Request.Response>) -> Void
144155
) -> RequestID {
145-
return server.send(request, reply: reply)
156+
return connectionToServer.send(request, reply: reply)
146157
}
147158
}
148159

@@ -153,34 +164,36 @@ public final class TestServer: MessageHandler {
153164
self.client = client
154165
}
155166

156-
public func handle(_ params: some NotificationType) {
157-
if params is EchoNotification {
158-
self.client.send(params)
167+
/// The client sent a notification to the server. Handle it.
168+
public func handle(_ notification: some NotificationType) {
169+
if notification is EchoNotification {
170+
self.client.send(notification)
159171
} else {
160172
fatalError("Unhandled notification")
161173
}
162174
}
163175

164-
public func handle<R: RequestType>(
165-
_ params: R,
176+
/// The client sent a request to the server. Handle it.
177+
public func handle<Request: RequestType>(
178+
_ request: Request,
166179
id: RequestID,
167-
reply: @escaping (LSPResult<R.Response>) -> Void
180+
reply: @escaping (LSPResult<Request.Response>) -> Void
168181
) {
169-
if let params = params as? EchoRequest {
170-
reply(.success(params.string as! R.Response))
171-
} else if let params = params as? EchoError {
182+
if let params = request as? EchoRequest {
183+
reply(.success(params.string as! Request.Response))
184+
} else if let params = request as? EchoError {
172185
if let code = params.code {
173186
reply(.failure(ResponseError(code: code, message: params.message!)))
174187
} else {
175-
reply(.success(VoidResponse() as! R.Response))
188+
reply(.success(VoidResponse() as! Request.Response))
176189
}
177190
} else {
178191
fatalError("Unhandled request")
179192
}
180193
}
181194
}
182195

183-
// MARK: Test requests.
196+
// MARK: Test requests
184197

185198
private let testMessageRegistry = MessageRegistry(
186199
requests: [EchoRequest.self, EchoError.self],

Sources/LanguageServerProtocol/Connection.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public protocol Connection: AnyObject, Sendable {
2020

2121
/// Send a request and (asynchronously) receive a reply.
2222
func send<Request: RequestType>(
23-
_: Request,
23+
_ request: Request,
2424
reply: @escaping (LSPResult<Request.Response>) -> Void
2525
) -> RequestID
2626
}
@@ -33,7 +33,7 @@ public protocol MessageHandler: AnyObject {
3333
/// The method should return as soon as the notification has been sufficiently
3434
/// handled to avoid out-of-order requests, e.g. once the notification has
3535
/// been forwarded to clangd.
36-
func handle(_ params: some NotificationType)
36+
func handle(_ notification: some NotificationType)
3737

3838
/// Handle a request and (asynchronously) receive a reply.
3939
///
@@ -62,7 +62,7 @@ public protocol MessageHandler: AnyObject {
6262
/// ```
6363
///
6464
/// - Note: Unchecked sendable conformance because shared state is guarded by `queue`.
65-
public final class LocalConnection: @unchecked Sendable {
65+
public final class LocalConnection: Connection, @unchecked Sendable {
6666

6767
enum State {
6868
case ready, started, closed
@@ -103,9 +103,7 @@ public final class LocalConnection: @unchecked Sendable {
103103
return .number(_nextRequestID)
104104
}
105105
}
106-
}
107106

108-
extension LocalConnection: Connection {
109107
public func send<Notification>(_ notification: Notification) where Notification: NotificationType {
110108
self.handler?.handle(notification)
111109
}

Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import struct CDispatch.dispatch_fd_t
2121

2222
/// A connection between a message handler (e.g. language server) in the same process as the connection object and a remote message handler (e.g. language client) that may run in another process using JSON RPC messages sent over a pair of in/out file descriptors.
2323
///
24-
/// For example, inside a language server, the `JSONRPCConnection` takes the language service implemenation as its `receiveHandler` and itself provides the client connection for sending notifications and callbacks.
24+
/// For example, inside a language server, the `JSONRPCConnection` takes the language service implementation as its `receiveHandler` and itself provides the client connection for sending notifications and callbacks.
2525
public final class JSONRPCConnection {
2626

2727
/// A name of the endpoint for this connection, used for logging, e.g. `clangd`.
@@ -64,7 +64,7 @@ public final class JSONRPCConnection {
6464
/// The set of currently outstanding outgoing requests along with information about how to decode and handle their responses.
6565
var outstandingRequests: [RequestID: OutstandingRequest] = [:]
6666

67-
/// A handler that will be called asyncronously when the connection is being
67+
/// A handler that will be called asynchronously when the connection is being
6868
/// closed.
6969
var closeHandler: (() async -> Void)? = nil
7070

Sources/SKTestSupport/TestSourceKitLSPClient.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,14 +112,14 @@ public final class TestSourceKitLSPClient: MessageHandler {
112112
}
113113
self.notificationYielder = notificationYielder
114114

115-
let clientConnection = LocalConnection()
116-
self.serverToClientConnection = clientConnection
115+
let serverToClientConnection = LocalConnection()
116+
self.serverToClientConnection = serverToClientConnection
117117
server = SourceKitServer(
118-
client: clientConnection,
118+
client: serverToClientConnection,
119119
toolchainRegistry: ToolchainRegistry.forTesting,
120120
options: serverOptions,
121121
onExit: {
122-
clientConnection.close()
122+
serverToClientConnection.close()
123123
}
124124
)
125125

Sources/SourceKitLSP/Clang/ClangLanguageServer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ fileprivate class ClangdStderrLogForwarder {
5555
/// A thin wrapper over a connection to a clangd server providing build setting handling.
5656
///
5757
/// In addition, it also intercepts notifications and replies from clangd in order to do things
58-
/// like witholding diagnostics when fallback build settings are being used.
58+
/// like withholding diagnostics when fallback build settings are being used.
5959
///
60-
/// ``ClangLangaugeServerShim`` conforms to ``MessageHandler`` to receive
60+
/// ``ClangLanguageServerShim`` conforms to ``MessageHandler`` to receive
6161
/// requests and notifications **from** clangd, not from the editor, and it will
6262
/// forward these requests and notifications to the editor.
6363
actor ClangLanguageServerShim: ToolchainLanguageServer, MessageHandler {

Tests/LanguageServerProtocolJSONRPCTests/ConnectionTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class ConnectionTests: XCTestCase {
5353

5454
func testMessageBuffer() async throws {
5555
let client = connection.client
56-
let clientConnection = connection.clientConnection
56+
let clientConnection = connection.clientToServerConnection
5757
let expectation = self.expectation(description: "note received")
5858

5959
await client.appendOneShotNotificationHandler { (note: EchoNotification) in
@@ -91,8 +91,8 @@ class ConnectionTests: XCTestCase {
9191
try await fulfillmentOfOrThrow([expectation2])
9292

9393
// Close the connection before accessing _requestBuffer, which ensures we don't race.
94-
connection.serverConnection.close()
95-
XCTAssertEqual(connection.serverConnection._requestBuffer, [])
94+
connection.serverToClientConnection.close()
95+
XCTAssertEqual(connection.serverToClientConnection._requestBuffer, [])
9696
}
9797

9898
func testEchoError() {
@@ -175,7 +175,7 @@ class ConnectionTests: XCTestCase {
175175
let expectation = self.expectation(description: "response received")
176176

177177
// response to unknown request
178-
connection.clientConnection.sendReply(.success(VoidResponse()), id: .string("unknown"))
178+
connection.clientToServerConnection.sendReply(.success(VoidResponse()), id: .string("unknown"))
179179

180180
// Nothing bad should happen; check that the next request works.
181181

@@ -193,18 +193,18 @@ class ConnectionTests: XCTestCase {
193193
let client = connection.client
194194
let expectation = self.expectation(description: "note received")
195195

196-
connection.clientConnection.close()
196+
connection.clientToServerConnection.close()
197197

198198
client.send(EchoNotification(string: "hi"))
199199
_ = client.send(EchoRequest(string: "yo")) { result in
200200
XCTAssertEqual(result, .failure(ResponseError.serverCancelled))
201201
expectation.fulfill()
202202
}
203203

204-
connection.clientConnection.sendReply(.success(VoidResponse()), id: .number(1))
204+
connection.clientToServerConnection.sendReply(.success(VoidResponse()), id: .number(1))
205205

206-
connection.clientConnection.close()
207-
connection.clientConnection.close()
206+
connection.clientToServerConnection.close()
207+
connection.clientToServerConnection.close()
208208

209209
waitForExpectations(timeout: defaultTimeout)
210210
}
@@ -219,7 +219,7 @@ class ConnectionTests: XCTestCase {
219219
}
220220

221221
server.client.send(EchoNotification(string: "about to close!"))
222-
connection.serverConnection.close()
222+
connection.serverToClientConnection.close()
223223

224224
try await fulfillmentOfOrThrow([expectation])
225225
}
@@ -232,8 +232,8 @@ class ConnectionTests: XCTestCase {
232232
expectation.fulfill()
233233
}
234234
let notification = EchoNotification(string: "about to close!")
235-
connection.serverConnection._send(.notification(notification), async: false)
236-
connection.serverConnection.close()
235+
connection.serverToClientConnection._send(.notification(notification), async: false)
236+
connection.serverToClientConnection.close()
237237

238238
try await fulfillmentOfOrThrow([expectation])
239239
}

0 commit comments

Comments
 (0)