Skip to content

Commit 367b19a

Browse files
authored
Merge pull request #1663 from ahoppen/language-dependencies-initialize
Migrate `defaultLanguage(for:)` and `filesDependenciesUpdated` to BSP and use `InitializeRequest` from BSP to communicate static options
2 parents dd50183 + 9006ab6 commit 367b19a

17 files changed

+368
-250
lines changed

Sources/BuildServerProtocol/BuildTargetSourcesRequest.swift

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,30 @@ public struct SourceItemDataKind: RawRepresentable, Codable, Hashable, Sendable
9797
}
9898

9999
/// `data` field must contain a JvmSourceItemData object.
100-
public static let jvm = "jvm"
100+
public static let jvm = SourceItemDataKind(rawValue: "jvm")
101+
102+
/// `data` field must contain a `SourceKitSourceItemData` object.
103+
public static let sourceKit = SourceItemDataKind(rawValue: "sourceKit")
104+
}
105+
106+
public struct SourceKitSourceItemData: LSPAnyCodable, Codable {
107+
public var language: Language?
108+
109+
public init(language: Language? = nil) {
110+
self.language = language
111+
}
112+
113+
public init?(fromLSPDictionary dictionary: [String: LanguageServerProtocol.LSPAny]) {
114+
if case .string(let language) = dictionary[CodingKeys.language.stringValue] {
115+
self.language = Language(rawValue: language)
116+
}
117+
}
118+
119+
public func encodeToLSPAny() -> LanguageServerProtocol.LSPAny {
120+
var result: [String: LSPAny] = [:]
121+
if let language {
122+
result[CodingKeys.language.stringValue] = .string(language.rawValue)
123+
}
124+
return .dictionary(result)
125+
}
101126
}

Sources/BuildServerProtocol/InitializeBuild.swift

Lines changed: 160 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,17 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import LanguageServerProtocol
1314

15+
public struct InitializeBuildRequestDataKind: RawRepresentable, Hashable, Codable, Sendable {
16+
public let rawValue: String
17+
18+
public init(rawValue: String) {
19+
self.rawValue = rawValue
20+
}
21+
}
22+
1423
/// Like the language server protocol, the initialize request is sent
1524
/// as the first request from the client to the server. If the server
1625
/// receives a request or notification before the initialize request
@@ -25,9 +34,9 @@ import LanguageServerProtocol
2534
/// Until the server has responded to the initialize request with an
2635
/// InitializeBuildResult, the client must not send any additional
2736
/// requests or notifications to the server.
28-
public struct InitializeBuild: RequestType, Hashable {
37+
public struct InitializeBuildRequest: RequestType, Hashable {
2938
public static let method: String = "build/initialize"
30-
public typealias Response = InitializeBuildResult
39+
public typealias Response = InitializeBuildResponse
3140

3241
/// Name of the client
3342
public var displayName: String
@@ -44,18 +53,28 @@ public struct InitializeBuild: RequestType, Hashable {
4453
/// The capabilities of the client
4554
public var capabilities: BuildClientCapabilities
4655

56+
/// Kind of data to expect in the `data` field. If this field is not set, the kind of data is not specified. */
57+
public var dataKind: InitializeBuildRequestDataKind?
58+
59+
/// Additional metadata about the client
60+
public var data: LSPAny?
61+
4762
public init(
4863
displayName: String,
4964
version: String,
5065
bspVersion: String,
5166
rootUri: URI,
52-
capabilities: BuildClientCapabilities
67+
capabilities: BuildClientCapabilities,
68+
dataKind: InitializeBuildRequestDataKind? = nil,
69+
data: LSPAny? = nil
5370
) {
5471
self.displayName = displayName
5572
self.version = version
5673
self.bspVersion = bspVersion
5774
self.rootUri = rootUri
5875
self.capabilities = capabilities
76+
self.dataKind = dataKind
77+
self.data = data
5978
}
6079
}
6180

@@ -66,12 +85,67 @@ public struct BuildClientCapabilities: Codable, Hashable, Sendable {
6685
/// languages than those that appear in this list.
6786
public var languageIds: [Language]
6887

69-
public init(languageIds: [Language]) {
88+
/// Mirror capability to BuildServerCapabilities.jvmCompileClasspathProvider
89+
/// The client will request classpath via `buildTarget/jvmCompileClasspath` so
90+
/// it's safe to return classpath in ScalacOptionsItem empty. */
91+
public var jvmCompileClasspathReceiver: Bool?
92+
93+
public init(languageIds: [Language], jvmCompileClasspathReceiver: Bool? = nil) {
7094
self.languageIds = languageIds
95+
self.jvmCompileClasspathReceiver = jvmCompileClasspathReceiver
7196
}
7297
}
7398

74-
public struct InitializeBuildResult: ResponseType, Hashable {
99+
public struct InitializeBuildResponseDataKind: RawRepresentable, Hashable, Codable, Sendable {
100+
public let rawValue: String
101+
102+
public init(rawValue: String) {
103+
self.rawValue = rawValue
104+
}
105+
106+
/// `data` field must contain a `SourceKitInitializeBuildResponseData` object.
107+
public static let sourceKit = InitializeBuildResponseDataKind(rawValue: "sourcekit")
108+
}
109+
110+
public struct SourceKitInitializeBuildResponseData: LSPAnyCodable, Codable, Sendable {
111+
public var indexDatabasePath: String?
112+
public var indexStorePath: String?
113+
public var supportsPreparation: Bool?
114+
115+
public init(indexDatabasePath: String?, indexStorePath: String?, supportsPreparation: Bool?) {
116+
self.indexDatabasePath = indexDatabasePath
117+
self.indexStorePath = indexStorePath
118+
self.supportsPreparation = supportsPreparation
119+
}
120+
121+
public init?(fromLSPDictionary dictionary: [String: LanguageServerProtocol.LSPAny]) {
122+
if case .string(let indexDatabasePath) = dictionary[CodingKeys.indexDatabasePath.stringValue] {
123+
self.indexDatabasePath = indexDatabasePath
124+
}
125+
if case .string(let indexStorePath) = dictionary[CodingKeys.indexStorePath.stringValue] {
126+
self.indexStorePath = indexStorePath
127+
}
128+
if case .bool(let supportsPreparation) = dictionary[CodingKeys.supportsPreparation.stringValue] {
129+
self.supportsPreparation = supportsPreparation
130+
}
131+
}
132+
133+
public func encodeToLSPAny() -> LanguageServerProtocol.LSPAny {
134+
var result: [String: LSPAny] = [:]
135+
if let indexDatabasePath {
136+
result[CodingKeys.indexDatabasePath.stringValue] = .string(indexDatabasePath)
137+
}
138+
if let indexStorePath {
139+
result[CodingKeys.indexStorePath.stringValue] = .string(indexStorePath)
140+
}
141+
if let supportsPreparation {
142+
result[CodingKeys.supportsPreparation.stringValue] = .bool(supportsPreparation)
143+
}
144+
return .dictionary(result)
145+
}
146+
}
147+
148+
public struct InitializeBuildResponse: ResponseType, Hashable {
75149
/// Name of the server
76150
public var displayName: String
77151

@@ -84,6 +158,9 @@ public struct InitializeBuildResult: ResponseType, Hashable {
84158
/// The capabilities of the build server
85159
public var capabilities: BuildServerCapabilities
86160

161+
/// Kind of data to expect in the `data` field. If this field is not set, the kind of data is not specified.
162+
public var dataKind: InitializeBuildResponseDataKind?
163+
87164
/// Optional metadata about the server
88165
public var data: LSPAny?
89166

@@ -92,41 +169,103 @@ public struct InitializeBuildResult: ResponseType, Hashable {
92169
version: String,
93170
bspVersion: String,
94171
capabilities: BuildServerCapabilities,
172+
dataKind: InitializeBuildResponseDataKind? = nil,
95173
data: LSPAny? = nil
96174
) {
97175
self.displayName = displayName
98176
self.version = version
99177
self.bspVersion = bspVersion
100178
self.capabilities = capabilities
179+
self.dataKind = dataKind
101180
self.data = data
102181
}
103182
}
104183

105184
public struct BuildServerCapabilities: Codable, Hashable, Sendable {
106185
/// The languages the server supports compilation via method buildTarget/compile.
107-
public var compileProvider: CompileProvider? = nil
186+
public var compileProvider: CompileProvider?
108187

109188
/// The languages the server supports test execution via method buildTarget/test
110-
public var testProvider: TestProvider? = nil
189+
public var testProvider: TestProvider?
111190

112191
/// The languages the server supports run via method buildTarget/run
113-
public var runProvider: RunProvider? = nil
192+
public var runProvider: RunProvider?
193+
194+
/// The languages the server supports debugging via method debugSession/start.
195+
public var debugProvider: DebugProvider?
114196

115197
/// The server can provide a list of targets that contain a
116198
/// single text document via the method buildTarget/inverseSources
117-
public var inverseSourcesProvider: Bool? = nil
199+
public var inverseSourcesProvider: Bool?
118200

119201
/// The server provides sources for library dependencies
120202
/// via method buildTarget/dependencySources
121-
public var dependencySourcesProvider: Bool? = nil
203+
public var dependencySourcesProvider: Bool?
122204

123205
/// The server provides all the resource dependencies
124206
/// via method buildTarget/resources
125-
public var resourcesProvider: Bool? = nil
207+
public var resourcesProvider: Bool?
208+
209+
/// The server provides all output paths
210+
/// via method buildTarget/outputPaths
211+
public var outputPathsProvider: Bool?
126212

127213
/// The server sends notifications to the client on build
128-
/// target change events via buildTarget/didChange
129-
public var buildTargetChangedProvider: Bool? = nil
214+
/// target change events via `buildTarget/didChange`
215+
public var buildTargetChangedProvider: Bool?
216+
217+
/// The server can respond to `buildTarget/jvmRunEnvironment` requests with the
218+
/// necessary information required to launch a Java process to run a main class.
219+
public var jvmRunEnvironmentProvider: Bool?
220+
221+
/// The server can respond to `buildTarget/jvmTestEnvironment` requests with the
222+
/// necessary information required to launch a Java process for testing or
223+
/// debugging.
224+
public var jvmTestEnvironmentProvider: Bool?
225+
226+
/// The server can respond to `workspace/cargoFeaturesState` and
227+
/// `setCargoFeatures` requests. In other words, supports Cargo Features extension.
228+
public var cargoFeaturesProvider: Bool?
229+
230+
/// Reloading the build state through workspace/reload is supported
231+
public var canReload: Bool?
232+
233+
/// The server can respond to `buildTarget/jvmCompileClasspath` requests with the
234+
/// necessary information about the target's classpath.
235+
public var jvmCompileClasspathProvider: Bool?
236+
237+
public init(
238+
compileProvider: CompileProvider? = nil,
239+
testProvider: TestProvider? = nil,
240+
runProvider: RunProvider? = nil,
241+
debugProvider: DebugProvider? = nil,
242+
inverseSourcesProvider: Bool? = nil,
243+
dependencySourcesProvider: Bool? = nil,
244+
resourcesProvider: Bool? = nil,
245+
outputPathsProvider: Bool? = nil,
246+
buildTargetChangedProvider: Bool? = nil,
247+
jvmRunEnvironmentProvider: Bool? = nil,
248+
jvmTestEnvironmentProvider: Bool? = nil,
249+
cargoFeaturesProvider: Bool? = nil,
250+
canReload: Bool? = nil,
251+
jvmCompileClasspathProvider: Bool? = nil
252+
) {
253+
self.compileProvider = compileProvider
254+
self.testProvider = testProvider
255+
self.runProvider = runProvider
256+
self.debugProvider = debugProvider
257+
self.inverseSourcesProvider = inverseSourcesProvider
258+
self.dependencySourcesProvider = dependencySourcesProvider
259+
self.resourcesProvider = resourcesProvider
260+
self.outputPathsProvider = outputPathsProvider
261+
self.buildTargetChangedProvider = buildTargetChangedProvider
262+
self.jvmRunEnvironmentProvider = jvmRunEnvironmentProvider
263+
self.jvmTestEnvironmentProvider = jvmTestEnvironmentProvider
264+
self.cargoFeaturesProvider = cargoFeaturesProvider
265+
self.canReload = canReload
266+
self.jvmCompileClasspathProvider = jvmCompileClasspathProvider
267+
}
268+
130269
}
131270

132271
public struct CompileProvider: Codable, Hashable, Sendable {
@@ -153,6 +292,14 @@ public struct TestProvider: Codable, Hashable, Sendable {
153292
}
154293
}
155294

295+
public struct DebugProvider: Codable, Hashable, Sendable {
296+
public var languageIds: [Language]
297+
298+
public init(languageIds: [Language]) {
299+
self.languageIds = languageIds
300+
}
301+
}
302+
156303
public struct InitializedBuildNotification: NotificationType {
157304
public static let method: String = "build/initialized"
158305

Sources/BuildServerProtocol/Messages.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fileprivate let requestTypes: [_RequestType.Type] = [
1515
BuildTargetOutputPaths.self,
1616
BuildTargetsRequest.self,
1717
BuildTargetSourcesRequest.self,
18-
InitializeBuild.self,
18+
InitializeBuildRequest.self,
1919
InverseSourcesRequest.self,
2020
RegisterForChanges.self,
2121
ShutdownBuild.self,

Sources/BuildSystemIntegration/BuildServerBuildSystem.swift

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,6 @@ package actor BuildServerBuildSystem: MessageHandler {
6767
package private(set) var indexDatabasePath: AbsolutePath?
6868
package private(set) var indexStorePath: AbsolutePath?
6969

70-
/// Delegate to handle any build system events.
71-
package weak var delegate: BuildSystemDelegate?
72-
73-
/// - Note: Needed to set the delegate from a different actor isolation context
74-
package func setDelegate(_ delegate: BuildSystemDelegate?) async {
75-
self.delegate = delegate
76-
}
77-
7870
package weak var messageHandler: BuiltInBuildSystemMessageHandler?
7971

8072
/// The build settings that have been received from the build server.
@@ -166,7 +158,7 @@ package actor BuildServerBuildSystem: MessageHandler {
166158
Language.swift,
167159
]
168160

169-
let initializeRequest = InitializeBuild(
161+
let initializeRequest = InitializeBuildRequest(
170162
displayName: "SourceKit-LSP",
171163
version: "1.0",
172164
bspVersion: "2.0",
@@ -338,10 +330,6 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
338330
)
339331
}
340332

341-
package func defaultLanguage(for document: DocumentURI) async -> Language? {
342-
return nil
343-
}
344-
345333
package func toolchain(for uri: DocumentURI, _ language: Language) async -> Toolchain? {
346334
return nil
347335
}

Sources/BuildSystemIntegration/BuildSystemDelegate.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,10 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
1213
import BuildServerProtocol
1314
import LanguageServerProtocol
1415

15-
/// Handles build system events, such as file build settings changes.
16-
// FIXME: (BSP migration) The build system should exclusively communicate back to SourceKit-LSP using BSP and this protocol should be deleted.
17-
package protocol BuildSystemDelegate: AnyObject, Sendable {
18-
/// Notify the delegate that the dependencies of the given files have changed
19-
/// and that ASTs may need to be refreshed. If the given set is empty, assume
20-
/// that all watched files are affected.
21-
///
22-
/// The callee should refresh ASTs unless it is able to determine that a
23-
/// refresh is not necessary.
24-
func filesDependenciesUpdated(_ changedFiles: Set<DocumentURI>) async
25-
}
26-
2716
/// Handles build system events, such as file build settings changes.
2817
package protocol BuildSystemManagerDelegate: AnyObject, Sendable {
2918
/// Notify the delegate that the given files' build settings have changed.

0 commit comments

Comments
 (0)