Skip to content

Commit 2042367

Browse files
authored
Merge pull request #931 from ahoppen/ahoppen/remove-notification-type
Remove the `Notification` type
2 parents 78c28f5 + 9abcd2a commit 2042367

File tree

9 files changed

+43
-74
lines changed

9 files changed

+43
-74
lines changed

Sources/LSPTestSupport/TestJSONRPCConnection.swift

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import XCTest
1616

1717
import class Foundation.Pipe
1818

19-
// Workaround ambiguity with Foundation.
20-
public typealias Notification = LanguageServerProtocol.Notification
21-
2219
public final class TestJSONRPCConnection {
2320
public let clientToServer: Pipe = Pipe()
2421
public let serverToClient: Pipe = Pipe()
@@ -94,9 +91,9 @@ public final class TestMessageHandler: MessageHandler {
9491

9592
public var allowUnexpectedNotification: Bool = true
9693

97-
public func appendOneShotNotificationHandler<N>(_ handler: @escaping (Notification<N>) -> Void) {
94+
public func appendOneShotNotificationHandler<N: NotificationType>(_ handler: @escaping (N) -> Void) {
9895
oneShotNotificationHandlers.append({ anyNote in
99-
guard let note = anyNote as? Notification<N> else {
96+
guard let note = anyNote as? N else {
10097
fatalError("received notification of the wrong type \(anyNote); expected \(N.self)")
10198
}
10299
handler(note)
@@ -112,9 +109,7 @@ public final class TestMessageHandler: MessageHandler {
112109
})
113110
}
114111

115-
public func handle<N>(_ params: N, from clientID: ObjectIdentifier) where N: NotificationType {
116-
let notification = Notification(params, clientID: clientID)
117-
112+
public func handle(_ notification: some NotificationType, from clientID: ObjectIdentifier) {
118113
guard !oneShotNotificationHandlers.isEmpty else {
119114
if allowUnexpectedNotification { return }
120115
fatalError("unexpected notification \(notification)")
@@ -142,7 +137,7 @@ public final class TestMessageHandler: MessageHandler {
142137
extension TestMessageHandler: Connection {
143138

144139
/// Send a notification to the language server.
145-
public func send<Notification>(_ notification: Notification) where Notification: NotificationType {
140+
public func send(_ notification: some NotificationType) {
146141
server.send(notification)
147142
}
148143

@@ -162,10 +157,9 @@ public final class TestServer: MessageHandler {
162157
self.client = client
163158
}
164159

165-
public func handle<N: NotificationType>(_ params: N, from clientID: ObjectIdentifier) {
166-
let note = Notification(params, clientID: clientID)
160+
public func handle(_ params: some NotificationType, from clientID: ObjectIdentifier) {
167161
if params is EchoNotification {
168-
self.client.send(note.params)
162+
self.client.send(params)
169163
} else {
170164
fatalError("Unhandled notification")
171165
}

Sources/LanguageServerProtocol/Request.swift

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,6 @@ public final class Request<R: RequestType> {
6868
}
6969
}
7070

71-
/// A request object, wrapping the parameters of a `NotificationType`.
72-
public final class Notification<N: NotificationType> {
73-
74-
public typealias Params = N
75-
76-
/// The client of the request.
77-
public let clientID: ObjectIdentifier
78-
79-
/// The request parameters.
80-
public let params: Params
81-
82-
public init(_ notification: Params, clientID: ObjectIdentifier) {
83-
self.clientID = clientID
84-
self.params = notification
85-
}
86-
}
87-
8871
fileprivate extension Encodable {
8972
var prettyPrintJSON: String {
9073
let encoder = JSONEncoder()
@@ -116,16 +99,24 @@ extension Request: CustomStringConvertible, CustomLogStringConvertible {
11699
}
117100
}
118101

119-
extension Notification: CustomStringConvertible, CustomLogStringConvertible {
102+
fileprivate struct AnyNotificationType: CustomLogStringConvertible {
103+
let notification: any NotificationType
104+
120105
public var description: String {
121106
return """
122-
\(N.method)
123-
\(params.prettyPrintJSON)
107+
\(type(of: notification))
108+
\(notification.prettyPrintJSON)
124109
"""
125110
}
126111

127112
public var redactedDescription: String {
128113
// FIXME: (logging) Log the non-critical parts of the notification
129-
return "Notification<\(N.method)>"
114+
return "\(type(of: notification))"
115+
}
116+
}
117+
118+
extension NotificationType {
119+
public var forLogging: CustomLogStringConvertibleWrapper {
120+
return AnyNotificationType(notification: self).forLogging
130121
}
131122
}

Sources/SKCore/BuildServerBuildSystem.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,9 @@ public actor BuildServerBuildSystem: MessageHandler {
193193
public nonisolated func handle(_ params: some NotificationType, from clientID: ObjectIdentifier) {
194194
bspMessageHandlingQueue.async {
195195
if let params = params as? BuildTargetsChangedNotification {
196-
await self.handleBuildTargetsChanged(Notification(params, clientID: clientID))
196+
await self.handleBuildTargetsChanged(params)
197197
} else if let params = params as? FileOptionsChangedNotification {
198-
await self.handleFileOptionsChanged(Notification(params, clientID: clientID))
198+
await self.handleFileOptionsChanged(params)
199199
}
200200
}
201201
}
@@ -213,20 +213,20 @@ public actor BuildServerBuildSystem: MessageHandler {
213213
}
214214

215215
func handleBuildTargetsChanged(
216-
_ notification: LanguageServerProtocol.Notification<BuildTargetsChangedNotification>
216+
_ notification: BuildTargetsChangedNotification
217217
) async {
218-
await self.delegate?.buildTargetsChanged(notification.params.changes)
218+
await self.delegate?.buildTargetsChanged(notification.changes)
219219
}
220220

221221
func handleFileOptionsChanged(
222-
_ notification: LanguageServerProtocol.Notification<FileOptionsChangedNotification>
222+
_ notification: FileOptionsChangedNotification
223223
) async {
224-
let result = notification.params.updatedOptions
224+
let result = notification.updatedOptions
225225
let settings = FileBuildSettings(
226226
compilerArguments: result.options,
227227
workingDirectory: result.workingDirectory
228228
)
229-
await self.buildSettingsChanged(for: notification.params.uri, settings: settings)
229+
await self.buildSettingsChanged(for: notification.uri, settings: settings)
230230
}
231231

232232
/// Record the new build settings for the given document and inform the delegate

Sources/SourceKitLSP/Clang/ClangLanguageServer.swift

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ actor ClangLanguageServerShim: ToolchainLanguageServer, MessageHandler {
274274
clangdMessageHandlingQueue.async {
275275
switch params {
276276
case let publishDiags as PublishDiagnosticsNotification:
277-
await self.publishDiagnostics(Notification(publishDiags, clientID: clientID))
277+
await self.publishDiagnostics(publishDiags)
278278
default:
279279
// We don't know how to handle any other notifications and ignore them.
280280
logger.error("Ignoring unknown notification \(type(of: params))")
@@ -356,8 +356,7 @@ extension ClangLanguageServerShim {
356356

357357
/// Intercept clangd's `PublishDiagnosticsNotification` to withold it if we're using fallback
358358
/// build settings.
359-
func publishDiagnostics(_ note: Notification<PublishDiagnosticsNotification>) async {
360-
let params = note.params
359+
func publishDiagnostics(_ notification: PublishDiagnosticsNotification) async {
361360
// Technically, the publish diagnostics notification could still originate
362361
// from when we opened the file with fallback build settings and we could
363362
// have received real build settings since, which haven't been acknowledged
@@ -369,7 +368,7 @@ extension ClangLanguageServerShim {
369368
// short and we expect clangd to send us new diagnostics with the updated
370369
// non-fallback settings very shortly after, which will override the
371370
// incorrect result, making it very temporary.
372-
let buildSettings = await self.buildSettings(for: params.uri)
371+
let buildSettings = await self.buildSettings(for: notification.uri)
373372
guard let sourceKitServer else {
374373
logger.fault("Cannot publish diagnostics because SourceKitServer has been destroyed")
375374
return
@@ -378,13 +377,13 @@ extension ClangLanguageServerShim {
378377
// Fallback: send empty publish notification instead.
379378
await sourceKitServer.sendNotificationToClient(
380379
PublishDiagnosticsNotification(
381-
uri: params.uri,
382-
version: params.version,
380+
uri: notification.uri,
381+
version: notification.version,
383382
diagnostics: []
384383
)
385384
)
386385
} else {
387-
await sourceKitServer.sendNotificationToClient(note.params)
386+
await sourceKitServer.sendNotificationToClient(notification)
388387
}
389388
}
390389

Sources/SourceKitLSP/SourceKitServer.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -638,17 +638,15 @@ extension SourceKitServer: MessageHandler {
638638
}
639639
}
640640

641-
private func handleImpl(_ params: some NotificationType, from clientID: ObjectIdentifier) async {
642-
let notification = Notification(params, clientID: clientID)
643-
641+
private func handleImpl(_ notification: some NotificationType, from clientID: ObjectIdentifier) async {
644642
logger.log(
645643
"""
646644
Received notification
647645
\(notification.forLogging)
648646
"""
649647
)
650648

651-
switch notification.params {
649+
switch notification {
652650
case let notification as InitializedNotification:
653651
self.clientInitialized(notification)
654652
case let notification as ExitNotification:
@@ -2050,7 +2048,6 @@ private let minWorkspaceSymbolPatternLength = 3
20502048
/// The maximum number of results to return from a `workspace/symbol` request.
20512049
private let maxWorkspaceSymbolResults = 4096
20522050

2053-
public typealias Notification = LanguageServerProtocol.Notification
20542051
public typealias Diagnostic = LanguageServerProtocol.Diagnostic
20552052

20562053
extension IndexSymbolKind {

Tests/LanguageServerProtocolJSONRPCTests/ConnectionTests.swift

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@ import XCTest
1919
import WinSDK
2020
#endif
2121

22-
// Workaround ambiguity with Foundation.
23-
typealias Notification = LanguageServerProtocol.Notification
24-
2522
class ConnectionTests: XCTestCase {
2623

2724
var connection: TestJSONRPCConnection! = nil
@@ -60,8 +57,8 @@ class ConnectionTests: XCTestCase {
6057
let clientConnection = connection.clientConnection
6158
let expectation = self.expectation(description: "note received")
6259

63-
client.appendOneShotNotificationHandler { (note: Notification<EchoNotification>) in
64-
XCTAssertEqual(note.params.string, "hello!")
60+
client.appendOneShotNotificationHandler { (note: EchoNotification) in
61+
XCTAssertEqual(note.string, "hello!")
6562
expectation.fulfill()
6663
}
6764

@@ -83,8 +80,8 @@ class ConnectionTests: XCTestCase {
8380

8481
let expectation2 = self.expectation(description: "note received")
8582

86-
client.appendOneShotNotificationHandler { (note: Notification<EchoNotification>) in
87-
XCTAssertEqual(note.params.string, "no way!")
83+
client.appendOneShotNotificationHandler { (note: EchoNotification) in
84+
XCTAssertEqual(note.string, "no way!")
8885
expectation2.fulfill()
8986
}
9087

@@ -125,8 +122,8 @@ class ConnectionTests: XCTestCase {
125122
let client = connection.client
126123
let expectation = self.expectation(description: "note received")
127124

128-
client.appendOneShotNotificationHandler { (note: Notification<EchoNotification>) in
129-
XCTAssertEqual(note.params.string, "hello!")
125+
client.appendOneShotNotificationHandler { (note: EchoNotification) in
126+
XCTAssertEqual(note.string, "hello!")
130127
expectation.fulfill()
131128
}
132129

@@ -218,7 +215,7 @@ class ConnectionTests: XCTestCase {
218215
let server = connection.server
219216

220217
let expectation = self.expectation(description: "received notification")
221-
client.appendOneShotNotificationHandler { (note: Notification<EchoNotification>) in
218+
client.appendOneShotNotificationHandler { (note: EchoNotification) in
222219
expectation.fulfill()
223220
}
224221

@@ -232,7 +229,7 @@ class ConnectionTests: XCTestCase {
232229
let client = connection.client
233230

234231
let expectation = self.expectation(description: "received notification")
235-
client.appendOneShotNotificationHandler { (note: Notification<EchoNotification>) in
232+
client.appendOneShotNotificationHandler { (note: EchoNotification) in
236233
expectation.fulfill()
237234
}
238235
let notification = EchoNotification(string: "about to close!")

Tests/LanguageServerProtocolTests/ConnectionTests.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@ import LSPTestSupport
1414
import LanguageServerProtocol
1515
import XCTest
1616

17-
// Workaround ambiguity with Foundation.
18-
typealias Notification = LanguageServerProtocol.Notification
19-
2017
class ConnectionTests: XCTestCase {
2118

2219
var connection: TestLocalConnection! = nil
@@ -68,8 +65,8 @@ class ConnectionTests: XCTestCase {
6865
let client = connection.client
6966
let expectation = self.expectation(description: "note received")
7067

71-
client.appendOneShotNotificationHandler { (note: Notification<EchoNotification>) in
72-
XCTAssertEqual(note.params.string, "hello!")
68+
client.appendOneShotNotificationHandler { (note: EchoNotification) in
69+
XCTAssertEqual(note.string, "hello!")
7370
expectation.fulfill()
7471
}
7572

Tests/SourceKitLSPTests/BuildSystemTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ fileprivate extension SourceKitServer {
2525
}
2626
}
2727

28-
// Workaround ambiguity with Foundation.
29-
typealias LSPNotification = LanguageServerProtocol.Notification
30-
3128
/// Build system to be used for testing BuildSystem and BuildSystemDelegate functionality with SourceKitServer
3229
/// and other components.
3330
final class TestBuildSystem: BuildSystem {

Tests/SourceKitLSPTests/LocalSwiftTests.swift

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ import SwiftParser
2020
import SwiftSyntax
2121
import XCTest
2222

23-
// Workaround ambiguity with Foundation.
24-
typealias Notification = LanguageServerProtocol.Notification
25-
2623
final class LocalSwiftTests: XCTestCase {
2724
private let quickFixCapabilities = ClientCapabilities(
2825
textDocument: TextDocumentClientCapabilities(

0 commit comments

Comments
 (0)