@@ -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
@@ -106,7 +119,7 @@ public actor TestMessageHandler: MessageHandler {
106
119
}
107
120
108
121
/// The LSP server sent a notification to the client. Handle it.
109
- public nonisolated func handle( _ notification: some NotificationType , from clientID : ObjectIdentifier ) {
122
+ public nonisolated func handle( _ notification: some NotificationType ) {
110
123
messageHandlingQueue. async {
111
124
await self . handleNotificationImpl ( notification)
112
125
}
@@ -125,25 +138,22 @@ public actor TestMessageHandler: MessageHandler {
125
138
public nonisolated func handle< Request: RequestType > (
126
139
_ request: Request ,
127
140
id: RequestID ,
128
- from clientID: ObjectIdentifier ,
129
141
reply: @escaping ( LSPResult < Request . Response > ) -> Void
130
142
) {
131
143
reply ( . failure( . methodNotFound( Request . method) ) )
132
144
}
133
- }
134
145
135
- extension TestMessageHandler : Connection {
136
146
/// Send a notification to the LSP server.
137
147
public nonisolated func send( _ notification: some NotificationType ) {
138
- server . send ( notification)
148
+ connectionToServer . send ( notification)
139
149
}
140
150
141
151
/// Send a request to the LSP server and (asynchronously) receive a reply.
142
152
public nonisolated func send< Request: RequestType > (
143
153
_ request: Request ,
144
154
reply: @escaping ( LSPResult < Request . Response > ) -> Void
145
155
) -> RequestID {
146
- return server . send ( request, reply: reply)
156
+ return connectionToServer . send ( request, reply: reply)
147
157
}
148
158
}
149
159
@@ -154,35 +164,36 @@ public final class TestServer: MessageHandler {
154
164
self . client = client
155
165
}
156
166
157
- public func handle( _ params: some NotificationType , from clientID: ObjectIdentifier ) {
158
- if params is EchoNotification {
159
- 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)
160
171
} else {
161
172
fatalError ( " Unhandled notification " )
162
173
}
163
174
}
164
175
165
- public func handle< R: RequestType > (
166
- _ params: R ,
176
+ /// The client sent a request to the server. Handle it.
177
+ public func handle< Request: RequestType > (
178
+ _ request: Request ,
167
179
id: RequestID ,
168
- from clientID: ObjectIdentifier ,
169
- reply: @escaping ( LSPResult < R . Response > ) -> Void
180
+ reply: @escaping ( LSPResult < Request . Response > ) -> Void
170
181
) {
171
- if let params = params as? EchoRequest {
172
- reply ( . success( params. string as! R . Response ) )
173
- } 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 {
174
185
if let code = params. code {
175
186
reply ( . failure( ResponseError ( code: code, message: params. message!) ) )
176
187
} else {
177
- reply ( . success( VoidResponse ( ) as! R . Response ) )
188
+ reply ( . success( VoidResponse ( ) as! Request . Response ) )
178
189
}
179
190
} else {
180
191
fatalError ( " Unhandled request " )
181
192
}
182
193
}
183
194
}
184
195
185
- // MARK: Test requests.
196
+ // MARK: Test requests
186
197
187
198
private let testMessageRegistry = MessageRegistry (
188
199
requests: [ EchoRequest . self, EchoError . self] ,
0 commit comments