12
12
13
13
import LanguageServerProtocol
14
14
import LanguageServerProtocolJSONRPC
15
+ import SKSupport
15
16
import XCTest
16
17
17
18
import class Foundation. Pipe
@@ -24,7 +25,7 @@ public final class TestJSONRPCConnection {
24
25
public let server : TestServer
25
26
public let serverConnection : JSONRPCConnection
26
27
27
- public init ( ) {
28
+ public init ( allowUnexpectedNotification : Bool = true ) {
28
29
clientConnection = JSONRPCConnection (
29
30
name: " client " ,
30
31
protocol: testMessageRegistry,
@@ -39,7 +40,7 @@ public final class TestJSONRPCConnection {
39
40
outFD: serverToClient. fileHandleForWriting
40
41
)
41
42
42
- client = TestMessageHandler ( server: clientConnection)
43
+ client = TestMessageHandler ( server: clientConnection, allowUnexpectedNotification : allowUnexpectedNotification )
43
44
server = TestServer ( client: serverConnection)
44
45
45
46
clientConnection. start ( receiveHandler: client) {
@@ -66,8 +67,8 @@ public struct TestLocalConnection {
66
67
public let server : TestServer
67
68
public let serverConnection : LocalConnection = . init( )
68
69
69
- public init ( ) {
70
- client = TestMessageHandler ( server: serverConnection)
70
+ public init ( allowUnexpectedNotification : Bool = true ) {
71
+ client = TestMessageHandler ( server: serverConnection, allowUnexpectedNotification : allowUnexpectedNotification )
71
72
server = TestServer ( client: clientConnection)
72
73
73
74
clientConnection. start ( handler: client)
@@ -80,17 +81,20 @@ public struct TestLocalConnection {
80
81
}
81
82
}
82
83
83
- public final class TestMessageHandler : MessageHandler {
84
+ public actor TestMessageHandler : MessageHandler {
84
85
/// The connection to the language client.
85
86
public let server : Connection
86
87
87
- public init ( server: Connection ) {
88
- self . server = server
89
- }
88
+ private let messageHandlingQueue = AsyncQueue < Serial > ( )
90
89
91
- var oneShotNotificationHandlers : [ ( ( Any ) -> Void ) ] = [ ]
90
+ private var oneShotNotificationHandlers : [ ( ( Any ) -> Void ) ] = [ ]
92
91
93
- public var allowUnexpectedNotification : Bool = true
92
+ private let allowUnexpectedNotification : Bool
93
+
94
+ public init ( server: Connection , allowUnexpectedNotification: Bool = true ) {
95
+ self . server = server
96
+ self . allowUnexpectedNotification = allowUnexpectedNotification
97
+ }
94
98
95
99
public func appendOneShotNotificationHandler< N: NotificationType > ( _ handler: @escaping ( N ) -> Void ) {
96
100
oneShotNotificationHandlers. append ( { anyNote in
@@ -101,7 +105,14 @@ public final class TestMessageHandler: MessageHandler {
101
105
} )
102
106
}
103
107
104
- public func handle( _ notification: some NotificationType , from clientID: ObjectIdentifier ) {
108
+ /// The LSP server sent a notification to the client. Handle it.
109
+ public nonisolated func handle( _ notification: some NotificationType , from clientID: ObjectIdentifier ) {
110
+ messageHandlingQueue. async {
111
+ await self . handleNotificationImpl ( notification)
112
+ }
113
+ }
114
+
115
+ public func handleNotificationImpl( _ notification: some NotificationType ) {
105
116
guard !oneShotNotificationHandlers. isEmpty else {
106
117
if allowUnexpectedNotification { return }
107
118
fatalError ( " unexpected notification \( notification) " )
@@ -110,25 +121,25 @@ public final class TestMessageHandler: MessageHandler {
110
121
handler ( notification)
111
122
}
112
123
113
- public func handle< R: RequestType > (
114
- _ params: R ,
124
+ /// The LSP server sent a request to the client. Handle it.
125
+ public nonisolated func handle< Request: RequestType > (
126
+ _ request: Request ,
115
127
id: RequestID ,
116
128
from clientID: ObjectIdentifier ,
117
- reply: @escaping ( LSPResult < R . Response > ) -> Void
129
+ reply: @escaping ( LSPResult < Request . Response > ) -> Void
118
130
) {
119
- reply ( . failure( . methodNotFound( R . method) ) )
131
+ reply ( . failure( . methodNotFound( Request . method) ) )
120
132
}
121
133
}
122
134
123
135
extension TestMessageHandler : Connection {
124
-
125
- /// Send a notification to the language server.
126
- public func send( _ notification: some NotificationType ) {
136
+ /// Send a notification to the LSP server.
137
+ public nonisolated func send( _ notification: some NotificationType ) {
127
138
server. send ( notification)
128
139
}
129
140
130
- /// Send a request to the language server and (asynchronously) receive a reply.
131
- public func send< Request: RequestType > (
141
+ /// Send a request to the LSP server and (asynchronously) receive a reply.
142
+ public nonisolated func send< Request: RequestType > (
132
143
_ request: Request ,
133
144
reply: @escaping ( LSPResult < Request . Response > ) -> Void
134
145
) -> RequestID {
0 commit comments