Skip to content

Commit d444930

Browse files
authored
Merge pull request #48 from rintaro/sourcekit-extensions
Prefix LSP/BSP extension methods with `sourcekit/`
2 parents eb10d35 + 5bb2332 commit d444930

33 files changed

Lines changed: 440 additions & 41 deletions

Package.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ var targets: [Target] = [
9191
"LanguageServerProtocol",
9292
"LanguageServerProtocolTransport",
9393
"SKLogging",
94+
"ToolsProtocolsSwiftExtensions",
9495
"ToolsProtocolsTestSupport",
9596
],
9697
swiftSettings: globalSwiftSettings

Sources/BuildServerProtocol/Messages.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,22 @@ private let notificationTypes: [NotificationType.Type] = [
4242

4343
extension MessageRegistry {
4444
public static let bspProtocol: MessageRegistry =
45-
MessageRegistry(requests: requestTypes, notifications: notificationTypes)
45+
MessageRegistry(requests: requestTypes, notifications: notificationTypes, legacyNames: bspLegacyNames)
46+
47+
/// Maps current `sourcekit/`-prefixed BSP method names to the legacy names used before the
48+
/// prefix migration. Consumed by `MessageRegistry` (incoming routing) and
49+
/// `LegacyNameFallbackConnection` (outgoing retries).
50+
///
51+
/// This table is frozen. Do not add new entries for newly introduced methods.
52+
public static let bspLegacyNames: [String: String] = [
53+
BuildTargetPrepareRequest.method: "buildTarget/prepare",
54+
FileOptionsChangedNotification.method: "build/sourceKitOptionsChanged",
55+
TextDocumentSourceKitOptionsRequest.method: "textDocument/sourceKitOptions",
56+
WorkspaceWaitForBuildSystemUpdatesRequest.method: "workspace/waitForBuildSystemUpdates",
57+
]
58+
#if compiler(>=6.7)
59+
#warning("Remove the legacy method names")
60+
#endif
4661
}
4762

4863
@available(*, deprecated, message: "use MessageRegistry.bspProtocol instead")

Sources/BuildServerProtocol/Messages/BuildTargetPrepareRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public typealias OriginId = String
2626
/// The server communicates during the initialize handshake whether this method is supported or not by setting
2727
/// `prepareProvider: true` in `SourceKitInitializeBuildResponseData`.
2828
public struct BuildTargetPrepareRequest: BSPRequest, Hashable {
29-
public static let method: String = "buildTarget/prepare"
29+
public static let method: String = "sourcekit/buildTarget/prepare"
3030
public typealias Response = BuildTargetPrepareResponse
3131

3232
/// A list of build targets to prepare.

Sources/BuildServerProtocol/Messages/InitializeBuildRequest.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,13 @@ public struct BuildClientCapabilities: Codable, Hashable, Sendable {
8282
/// it's safe to return classpath in ScalacOptionsItem empty. */
8383
public var jvmCompileClasspathReceiver: Bool?
8484

85-
public init(languageIds: [Language], jvmCompileClasspathReceiver: Bool? = nil) {
85+
/// Experimental client capabilities.
86+
public var experimental: LSPAny?
87+
88+
public init(languageIds: [Language], jvmCompileClasspathReceiver: Bool? = nil, experimental: LSPAny? = nil) {
8689
self.languageIds = languageIds
8790
self.jvmCompileClasspathReceiver = jvmCompileClasspathReceiver
91+
self.experimental = experimental
8892
}
8993
}
9094

Sources/BuildServerProtocol/Messages/RegisterForChangeNotifications.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public struct FileOptionsChangedNotification: BSPNotification {
5757
public var workingDirectory: String?
5858
}
5959

60-
public static let method: String = "build/sourceKitOptionsChanged"
60+
public static let method: String = "sourcekit/build/sourceKitOptionsChanged"
6161

6262
/// The URI of the document that has changed settings.
6363
public var uri: URI

Sources/BuildServerProtocol/Messages/TextDocumentSourceKitOptionsRequest.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public import LanguageServerProtocol
2020
///
2121
/// The request may return `nil` if it doesn't have any build settings for this file in the given target.
2222
public struct TextDocumentSourceKitOptionsRequest: BSPRequest, Hashable {
23-
public static let method: String = "textDocument/sourceKitOptions"
23+
public static let method: String = "sourcekit/textDocument/sourceKitOptions"
2424
public typealias Response = TextDocumentSourceKitOptionsResponse?
2525

2626
/// The URI of the document to get options for

Sources/BuildServerProtocol/Messages/WorkspaceWaitForBuildSystemUpdates.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public import LanguageServerProtocol
1919
public struct WorkspaceWaitForBuildSystemUpdatesRequest: BSPRequest, Hashable {
2020
public typealias Response = VoidResponse
2121

22-
public static let method: String = "workspace/waitForBuildSystemUpdates"
22+
public static let method: String = "sourcekit/workspace/waitForBuildSystemUpdates"
2323

2424
public init() {}
2525
}

Sources/LanguageServerProtocol/Connection.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ public protocol Connection: Sendable {
2828
/// Send a request with a pre-defined request ID and (asynchronously) receive a reply.
2929
///
3030
/// The request ID must not conflict with any request ID generated by `nextRequestID()`.
31+
/// `method` is the JSON-RPC method string; pass `Request.method` for the normal case or a
32+
/// legacy method name when talking to an old peer.
3133
func send<Request: RequestType>(
3234
_ request: Request,
35+
method: String,
3336
id: RequestID,
3437
reply: @escaping @Sendable (LSPResult<Request.Response>) -> Void
3538
)
@@ -42,7 +45,7 @@ extension Connection {
4245
reply: @escaping @Sendable (LSPResult<Request.Response>) -> Void
4346
) -> RequestID {
4447
let id = nextRequestID()
45-
self.send(request, id: id, reply: reply)
48+
self.send(request, method: Request.method, id: id, reply: reply)
4649
return id
4750
}
4851
}

Sources/LanguageServerProtocol/MessageRegistry.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,26 @@ public final class MessageRegistry: Sendable {
1414
private let methodToRequest: [String: _RequestType.Type]
1515
private let methodToNotification: [String: NotificationType.Type]
1616

17-
public init(requests: [_RequestType.Type], notifications: [NotificationType.Type]) {
18-
self.methodToRequest = Dictionary(uniqueKeysWithValues: requests.map { ($0.method, $0) })
19-
self.methodToNotification = Dictionary(uniqueKeysWithValues: notifications.map { ($0.method, $0) })
17+
public init(
18+
requests: [_RequestType.Type],
19+
notifications: [NotificationType.Type],
20+
legacyNames: [String: String] = [:]
21+
) {
22+
var methodToRequest: [String: _RequestType.Type] = Dictionary(
23+
uniqueKeysWithValues: requests.map { ($0.method, $0) }
24+
)
25+
for request in requests {
26+
if let legacy = legacyNames[request.method] { methodToRequest[legacy] = request }
27+
}
28+
self.methodToRequest = methodToRequest
29+
30+
var methodToNotification: [String: NotificationType.Type] = Dictionary(
31+
uniqueKeysWithValues: notifications.map { ($0.method, $0) }
32+
)
33+
for notification in notifications {
34+
if let legacy = legacyNames[notification.method] { methodToNotification[legacy] = notification }
35+
}
36+
self.methodToNotification = methodToNotification
2037
}
2138

2239
/// Returns the type of the message named `method`, or nil if it is unknown.

Sources/LanguageServerProtocol/Messages.swift

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,33 @@ public let builtinNotifications: [NotificationType.Type] = [
134134

135135
extension MessageRegistry {
136136
public static let lspProtocol: MessageRegistry =
137-
MessageRegistry(requests: builtinRequests, notifications: builtinNotifications)
137+
MessageRegistry(requests: builtinRequests, notifications: builtinNotifications, legacyNames: lspLegacyNames)
138+
139+
/// Maps current `sourcekit/`-prefixed method names to the legacy names used before the prefix
140+
/// migration. Consumed by `MessageRegistry` (incoming routing) and `LegacyNameFallbackConnection`
141+
/// (outgoing retries).
142+
///
143+
/// This table is frozen. Do not add new entries for newly introduced methods.
144+
public static let lspLegacyNames: [String: String] = [
145+
DidChangeActiveDocumentNotification.method: "window/didChangeActiveDocument",
146+
DoccDocumentationRequest.method: "textDocument/doccDocumentation",
147+
DocumentTestsRequest.method: "textDocument/tests",
148+
GetReferenceDocumentRequest.method: "workspace/getReferenceDocument",
149+
IsIndexingRequest.method: "sourceKit/_isIndexing",
150+
OutputPathsRequest.method: "workspace/_outputPaths",
151+
PeekDocumentsRequest.method: "workspace/peekDocuments",
152+
SetOptionsRequest.method: "workspace/_setOptions",
153+
SourceKitOptionsRequest.method: "workspace/_sourceKitOptions",
154+
SynchronizeRequest.method: "workspace/synchronize",
155+
TriggerReindexRequest.method: "workspace/triggerReindex",
156+
WorkspacePlaygroundsRefreshRequest.method: "workspace/playgrounds/refresh",
157+
WorkspacePlaygroundsRequest.method: "workspace/playgrounds",
158+
WorkspaceTestsRefreshRequest.method: "workspace/tests/refresh",
159+
WorkspaceTestsRequest.method: "workspace/tests",
160+
]
161+
#if compiler(>=6.7)
162+
#warning("Remove the legacy method names")
163+
#endif
138164
}
139165

140166
// MARK: Miscellaneous Message Types

0 commit comments

Comments
 (0)