Skip to content

Commit 119b3fa

Browse files
committed
Rename Message -> JSONRPCMessage in preparation for making it public
1 parent b3a89f9 commit 119b3fa

File tree

4 files changed

+24
-23
lines changed

4 files changed

+24
-23
lines changed

Sources/LanguageServerProtocolJSONRPC/JSONRPCConnection.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public final class JSONRPCConection {
144144
return nil
145145
}
146146
return outstanding.responseType
147-
} as Message.ResponseTypeCallback
147+
} as JSONRPCMessage.ResponseTypeCallback
148148

149149
var bytes = bytes[...]
150150

@@ -156,7 +156,7 @@ public final class JSONRPCConection {
156156
bytes = rest
157157

158158
let pointer = UnsafeMutableRawPointer(mutating: UnsafeBufferPointer(rebasing: messageBytes).baseAddress!)
159-
let message = try decoder.decode(Message.self, from: Data(bytesNoCopy: pointer, count: messageBytes.count, deallocator: .none))
159+
let message = try decoder.decode(JSONRPCMessage.self, from: Data(bytesNoCopy: pointer, count: messageBytes.count, deallocator: .none))
160160

161161
handle(message)
162162

@@ -166,7 +166,7 @@ public final class JSONRPCConection {
166166
case .request:
167167
if let id = error.id {
168168
send { encoder in
169-
try encoder.encode(Message.errorResponse(ResponseError(error), id: id))
169+
try encoder.encode(JSONRPCMessage.errorResponse(ResponseError(error), id: id))
170170
}
171171
continue MESSAGE_LOOP
172172
}
@@ -198,7 +198,7 @@ public final class JSONRPCConection {
198198
}
199199

200200
/// Handle a single message by dispatching it to `receiveHandler` or an appropriate reply handler.
201-
func handle(_ message: Message) {
201+
func handle(_ message: JSONRPCMessage) {
202202
switch message {
203203
case .notification(let notification):
204204
notification._handle(receiveHandler!, connection: self)
@@ -289,7 +289,7 @@ extension JSONRPCConection: _IndirectConnection {
289289
public func send<Notification>(_ notification: Notification) where Notification: NotificationType {
290290
guard readyToSend() else { return }
291291
send { encoder in
292-
return try encoder.encode(Message.notification(notification))
292+
return try encoder.encode(JSONRPCMessage.notification(notification))
293293
}
294294
}
295295

@@ -316,7 +316,7 @@ extension JSONRPCConection: _IndirectConnection {
316316
}
317317

318318
send { encoder in
319-
return try encoder.encode(Message.request(request, id: id))
319+
return try encoder.encode(JSONRPCMessage.request(request, id: id))
320320
}
321321

322322
return id
@@ -328,9 +328,9 @@ extension JSONRPCConection: _IndirectConnection {
328328
send { encoder in
329329
switch response {
330330
case .success(let result):
331-
return try encoder.encode(Message.response(result, id: id))
331+
return try encoder.encode(JSONRPCMessage.response(result, id: id))
332332
case .failure(let error):
333-
return try encoder.encode(Message.errorResponse(error, id: id))
333+
return try encoder.encode(JSONRPCMessage.errorResponse(error, id: id))
334334
}
335335
}
336336
}

Sources/LanguageServerProtocolJSONRPC/MessageCoding.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212

1313
import LanguageServerProtocol
1414

15-
enum Message {
15+
/// A single JSONRPC message suitable for encoding/decoding.
16+
enum JSONRPCMessage {
1617
case notification(NotificationType)
1718
case request(_RequestType, id: RequestID)
1819
case response(ResponseType, id: RequestID)
@@ -23,7 +24,7 @@ extension CodingUserInfoKey {
2324
public static let responseTypeCallbackKey: CodingUserInfoKey = CodingUserInfoKey(rawValue: "lsp.jsonrpc.responseTypeCallback")!
2425
}
2526

26-
extension Message: Codable {
27+
extension JSONRPCMessage: Codable {
2728

2829
typealias ResponseTypeCallback = (RequestID) -> ResponseType.Type?
2930

Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ final class CodingTests: XCTestCase {
187187
{"jsonrpc":"2.0","method":"$/cancelRequest","params":{}}
188188
""")
189189

190-
let responseTypeCallback: Message.ResponseTypeCallback = {
190+
let responseTypeCallback: JSONRPCMessage.ResponseTypeCallback = {
191191
return $0 == .string("unknown") ? nil : InitializeResult.self
192192
}
193193

@@ -216,9 +216,9 @@ final class CodingTests: XCTestCase {
216216
}
217217

218218
private func checkMessageCoding<Request>(_ value: Request, id: RequestID, json: String, file: StaticString = #file, line: UInt = #line) where Request: RequestType & Equatable {
219-
checkCoding(Message.request(value, id: id), json: json, file: file, line: line) {
219+
checkCoding(JSONRPCMessage.request(value, id: id), json: json, file: file, line: line) {
220220

221-
guard case Message.request(let decodedValueOpaque, let decodedID) = $0, let decodedValue = decodedValueOpaque as? Request else {
221+
guard case JSONRPCMessage.request(let decodedValueOpaque, let decodedID) = $0, let decodedValue = decodedValueOpaque as? Request else {
222222
XCTFail("decodedValue \($0) does not match expected \(value)", file: file, line: line)
223223
return
224224
}
@@ -229,9 +229,9 @@ private func checkMessageCoding<Request>(_ value: Request, id: RequestID, json:
229229
}
230230

231231
private func checkMessageCoding<Notification>(_ value: Notification, json: String, file: StaticString = #file, line: UInt = #line) where Notification: NotificationType & Equatable {
232-
checkCoding(Message.notification(value), json: json, file: file, line: line) {
232+
checkCoding(JSONRPCMessage.notification(value), json: json, file: file, line: line) {
233233

234-
guard case Message.notification(let decodedValueOpaque) = $0, let decodedValue = decodedValueOpaque as? Notification else {
234+
guard case JSONRPCMessage.notification(let decodedValueOpaque) = $0, let decodedValue = decodedValueOpaque as? Notification else {
235235
XCTFail("decodedValue \($0) does not match expected \(value)", file: file, line: line)
236236
return
237237
}
@@ -242,13 +242,13 @@ private func checkMessageCoding<Notification>(_ value: Notification, json: Strin
242242

243243
private func checkMessageCoding<Response>(_ value: Response, id: RequestID, json: String, file: StaticString = #file, line: UInt = #line) where Response: ResponseType & Equatable {
244244

245-
let callback: Message.ResponseTypeCallback = {
245+
let callback: JSONRPCMessage.ResponseTypeCallback = {
246246
return $0 == .string("unknown") ? nil : Response.self
247247
}
248248

249-
checkCoding(Message.response(value, id: id), json: json, userInfo: [.responseTypeCallbackKey: callback], file: file, line: line) {
249+
checkCoding(JSONRPCMessage.response(value, id: id), json: json, userInfo: [.responseTypeCallbackKey: callback], file: file, line: line) {
250250

251-
guard case Message.response(let decodedValueOpaque, let decodedID) = $0, let decodedValue = decodedValueOpaque as? Response else {
251+
guard case JSONRPCMessage.response(let decodedValueOpaque, let decodedID) = $0, let decodedValue = decodedValueOpaque as? Response else {
252252
XCTFail("decodedValue \($0) does not match expected \(value)", file: file, line: line)
253253
return
254254
}
@@ -259,9 +259,9 @@ private func checkMessageCoding<Response>(_ value: Response, id: RequestID, json
259259
}
260260

261261
private func checkMessageCoding(_ value: ResponseError, id: RequestID, json: String, file: StaticString = #file, line: UInt = #line) {
262-
checkCoding(Message.errorResponse(value, id: id), json: json, file: file, line: line) {
262+
checkCoding(JSONRPCMessage.errorResponse(value, id: id), json: json, file: file, line: line) {
263263

264-
guard case Message.errorResponse(let decodedValue, let decodedID) = $0 else {
264+
guard case JSONRPCMessage.errorResponse(let decodedValue, let decodedID) = $0 else {
265265
XCTFail("decodedValue \($0) does not match expected \(value)", file: file, line: line)
266266
return
267267
}
@@ -277,7 +277,7 @@ private func checkMessageDecodingError(_ expected: MessageDecodingError, json: S
277277
decoder.userInfo = userInfo
278278

279279
do {
280-
_ = try decoder.decode(Message.self, from: data)
280+
_ = try decoder.decode(JSONRPCMessage.self, from: data)
281281
XCTFail("expected error not seen", file: file, line: line)
282282
} catch let error as MessageDecodingError {
283283
XCTAssertEqual(expected, error, file: file, line: line)

Tests/LanguageServerProtocolJSONRPCTests/ConnectionTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ class ConnectionTests: XCTestCase {
6060
expectation.fulfill()
6161
}
6262

63-
let note1 = try! JSONEncoder().encode(Message.notification(EchoNotification(string: "hello!")))
64-
let note2 = try! JSONEncoder().encode(Message.notification(EchoNotification(string: "no way!")))
63+
let note1 = try! JSONEncoder().encode(JSONRPCMessage.notification(EchoNotification(string: "hello!")))
64+
let note2 = try! JSONEncoder().encode(JSONRPCMessage.notification(EchoNotification(string: "no way!")))
6565

6666
let note1Str: String = "Content-Length: \(note1.count)\r\n\r\n\(String(data: note1, encoding: .utf8)!)"
6767
let note2Str: String = "Content-Length: \(note2.count)\r\n\r\n\(String(data: note2, encoding: .utf8)!)"

0 commit comments

Comments
 (0)