@@ -20,55 +20,68 @@ import class Foundation.Pipe
20
20
public final class TestJSONRPCConnection {
21
21
public let clientToServer : Pipe = Pipe ( )
22
22
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.
25
32
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
27
37
28
38
public init ( allowUnexpectedNotification: Bool = true ) {
29
- clientConnection = JSONRPCConnection (
39
+ clientToServerConnection = JSONRPCConnection (
30
40
name: " client " ,
31
41
protocol: testMessageRegistry,
32
42
inFD: serverToClient. fileHandleForReading,
33
43
outFD: clientToServer. fileHandleForWriting
34
44
)
35
45
36
- serverConnection = JSONRPCConnection (
46
+ serverToClientConnection = JSONRPCConnection (
37
47
name: " server " ,
38
48
protocol: testMessageRegistry,
39
49
inFD: clientToServer. fileHandleForReading,
40
50
outFD: serverToClient. fileHandleForWriting
41
51
)
42
52
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)
45
58
46
- clientConnection . start ( receiveHandler: client) {
59
+ clientToServerConnection . start ( receiveHandler: client) {
47
60
// FIXME: keep the pipes alive until we close the connection. This
48
61
// should be fixed systemically.
49
62
withExtendedLifetime ( self ) { }
50
63
}
51
- serverConnection . start ( receiveHandler: server) {
64
+ serverToClientConnection . start ( receiveHandler: server) {
52
65
// FIXME: keep the pipes alive until we close the connection. This
53
66
// should be fixed systemically.
54
67
withExtendedLifetime ( self ) { }
55
68
}
56
69
}
57
70
58
71
public func close( ) {
59
- clientConnection . close ( )
60
- serverConnection . close ( )
72
+ clientToServerConnection . close ( )
73
+ serverToClientConnection . close ( )
61
74
}
62
75
}
63
76
64
77
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 ( )
67
80
public let server : TestServer
68
- public let serverConnection : LocalConnection = . init ( )
81
+ public let serverConnection : LocalConnection = LocalConnection ( )
69
82
70
83
public init ( allowUnexpectedNotification: Bool = true ) {
71
- client = TestMessageHandler ( server : serverConnection, allowUnexpectedNotification: allowUnexpectedNotification)
84
+ client = TestClient ( connectionToServer : serverConnection, allowUnexpectedNotification: allowUnexpectedNotification)
72
85
server = TestServer ( client: clientConnection)
73
86
74
87
clientConnection. start ( handler: client)
@@ -81,18 +94,18 @@ public struct TestLocalConnection {
81
94
}
82
95
}
83
96
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
87
100
88
101
private let messageHandlingQueue = AsyncQueue < Serial > ( )
89
102
90
103
private var oneShotNotificationHandlers : [ ( ( Any ) -> Void ) ] = [ ]
91
104
92
105
private let allowUnexpectedNotification : Bool
93
106
94
- public init ( server : Connection , allowUnexpectedNotification: Bool = true ) {
95
- self . server = server
107
+ public init ( connectionToServer : Connection , allowUnexpectedNotification: Bool = true ) {
108
+ self . connectionToServer = connectionToServer
96
109
self . allowUnexpectedNotification = allowUnexpectedNotification
97
110
}
98
111
@@ -129,20 +142,18 @@ public actor TestMessageHandler: MessageHandler {
129
142
) {
130
143
reply ( . failure( . methodNotFound( Request . method) ) )
131
144
}
132
- }
133
145
134
- extension TestMessageHandler : Connection {
135
146
/// Send a notification to the LSP server.
136
147
public nonisolated func send( _ notification: some NotificationType ) {
137
- server . send ( notification)
148
+ connectionToServer . send ( notification)
138
149
}
139
150
140
151
/// Send a request to the LSP server and (asynchronously) receive a reply.
141
152
public nonisolated func send< Request: RequestType > (
142
153
_ request: Request ,
143
154
reply: @escaping ( LSPResult < Request . Response > ) -> Void
144
155
) -> RequestID {
145
- return server . send ( request, reply: reply)
156
+ return connectionToServer . send ( request, reply: reply)
146
157
}
147
158
}
148
159
@@ -153,34 +164,36 @@ public final class TestServer: MessageHandler {
153
164
self . client = client
154
165
}
155
166
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)
159
171
} else {
160
172
fatalError ( " Unhandled notification " )
161
173
}
162
174
}
163
175
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 ,
166
179
id: RequestID ,
167
- reply: @escaping ( LSPResult < R . Response > ) -> Void
180
+ reply: @escaping ( LSPResult < Request . Response > ) -> Void
168
181
) {
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 {
172
185
if let code = params. code {
173
186
reply ( . failure( ResponseError ( code: code, message: params. message!) ) )
174
187
} else {
175
- reply ( . success( VoidResponse ( ) as! R . Response ) )
188
+ reply ( . success( VoidResponse ( ) as! Request . Response ) )
176
189
}
177
190
} else {
178
191
fatalError ( " Unhandled request " )
179
192
}
180
193
}
181
194
}
182
195
183
- // MARK: Test requests.
196
+ // MARK: Test requests
184
197
185
198
private let testMessageRegistry = MessageRegistry (
186
199
requests: [ EchoRequest . self, EchoError . self] ,
0 commit comments