Skip to content

Commit c5ba967

Browse files
committed
Migrate getting the list of all source files to BSP
1 parent cf47f2c commit c5ba967

15 files changed

+422
-136
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import LanguageServerProtocol
14+
15+
/// The build target sources request is sent from the client to the server to
16+
/// query for the list of text documents and directories that are belong to a
17+
/// build target. The sources response must not include sources that are
18+
/// external to the workspace.
19+
public struct BuildTargetSourcesRequest: RequestType, Hashable {
20+
public static let method: String = "buildTarget/sources"
21+
public typealias Response = BuildTargetSourcesResponse
22+
23+
public var targets: [BuildTargetIdentifier]
24+
25+
public init(targets: [BuildTargetIdentifier]) {
26+
self.targets = targets
27+
}
28+
}
29+
30+
public struct BuildTargetSourcesResponse: ResponseType, Hashable {
31+
public var items: [SourcesItem]
32+
33+
public init(items: [SourcesItem]) {
34+
self.items = items
35+
}
36+
}
37+
38+
public struct SourcesItem: Codable, Hashable, Sendable {
39+
public var target: BuildTargetIdentifier
40+
41+
/// The text documents and directories that belong to this build target.
42+
public var sources: [SourceItem]
43+
44+
public init(target: BuildTargetIdentifier, sources: [SourceItem]) {
45+
self.target = target
46+
self.sources = sources
47+
}
48+
}
49+
50+
public struct SourceItem: Codable, Hashable, Sendable {
51+
/// Either a text document or a directory. A directory entry must end with a
52+
/// forward slash "/" and a directory entry implies that every nested text
53+
/// document within the directory belongs to this source item.
54+
public var uri: URI
55+
56+
/// Type of file of the source item, such as whether it is file or directory.
57+
public var kind: SourceItemKind
58+
59+
/// Indicates if this source is automatically generated by the build and is
60+
/// not intended to be manually edited by the user.
61+
public var generated: Bool
62+
63+
/// Kind of data to expect in the `data` field. If this field is not set, the kind of data is not specified.
64+
public var dataKind: SourceItemDataKind?
65+
66+
/// Language-specific metadata about this source item.
67+
public var data: LSPAny?
68+
69+
public init(
70+
uri: URI,
71+
kind: SourceItemKind,
72+
generated: Bool,
73+
dataKind: SourceItemDataKind? = nil,
74+
data: LSPAny? = nil
75+
) {
76+
self.uri = uri
77+
self.kind = kind
78+
self.generated = generated
79+
self.dataKind = dataKind
80+
self.data = data
81+
}
82+
}
83+
84+
public enum SourceItemKind: Int, Codable, Hashable, Sendable {
85+
/// The source item references a normal file.
86+
case file = 1
87+
88+
/// The source item references a directory.
89+
case directory = 2
90+
}
91+
92+
public struct SourceItemDataKind: RawRepresentable, Codable, Hashable, Sendable {
93+
public var rawValue: String
94+
95+
public init(rawValue: String) {
96+
self.rawValue = rawValue
97+
}
98+
99+
/// `data` field must contain a JvmSourceItemData object.
100+
public static let jvm = "jvm"
101+
}

Sources/BuildServerProtocol/BuildTargets.swift renamed to Sources/BuildServerProtocol/BuildTargetsRequest.swift

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

1415
public typealias URI = DocumentURI
1516

1617
/// The workspace build targets request is sent from the client to the server to
1718
/// ask for the list of all available build targets in the workspace.
18-
public struct BuildTargets: RequestType, Hashable {
19+
public struct BuildTargetsRequest: RequestType, Hashable {
1920
public static let method: String = "workspace/buildTargets"
20-
public typealias Response = BuildTargetsResult
21+
public typealias Response = BuildTargetsResponse
2122

2223
public init() {}
2324
}
2425

25-
public struct BuildTargetsResult: ResponseType, Hashable {
26+
public struct BuildTargetsResponse: ResponseType, Hashable {
2627
public var targets: [BuildTarget]
28+
29+
public init(targets: [BuildTarget]) {
30+
self.targets = targets
31+
}
2732
}
2833

2934
public struct BuildTarget: Codable, Hashable, Sendable {
@@ -52,16 +57,23 @@ public struct BuildTarget: Codable, Hashable, Sendable {
5257
/// are free to define new tags for custom purposes.
5358
public var tags: [BuildTargetTag]
5459

55-
/// The capabilities of this build target.
56-
public var capabilities: BuildTargetCapabilities
57-
5860
/// The set of languages that this target contains.
5961
/// The ID string for each language is defined in the LSP.
6062
public var languageIds: [Language]
6163

6264
/// The direct upstream build target dependencies of this build target
6365
public var dependencies: [BuildTargetIdentifier]
6466

67+
/// The capabilities of this build target.
68+
public var capabilities: BuildTargetCapabilities
69+
70+
/// Kind of data to expect in the `data` field. If this field is not set, the kind of data is not specified.
71+
public var dataKind: BuildTargetDataKind?
72+
73+
/// Language-specific metadata about this target.
74+
/// See ScalaBuildTarget as an example.
75+
public var data: LSPAny?
76+
6577
public init(
6678
id: BuildTargetIdentifier,
6779
displayName: String?,
@@ -96,17 +108,13 @@ public struct BuildTargetTag: Codable, Hashable, RawRepresentable, Sendable {
96108
self.rawValue = rawValue
97109
}
98110

99-
/// Target contains re-usable functionality for downstream targets. May have
100-
/// any combination of capabilities.
101-
public static let library: Self = Self(rawValue: "library")
102-
103111
/// Target contains source code for producing any kind of application, may
104112
/// have but does not require the `canRun` capability.
105113
public static let application: Self = Self(rawValue: "application")
106114

107-
/// Target contains source code for testing purposes, may have but does not
108-
/// require the `canTest` capability.
109-
public static let test: Self = Self(rawValue: "test")
115+
/// Target contains source code to measure performance of a program, may have
116+
/// but does not require the `canRun` build target capability.
117+
public static let benchmark: Self = Self(rawValue: "benchmark")
110118

111119
/// Target contains source code for integration testing purposes, may have
112120
/// but does not require the `canTest` capability. The difference between
@@ -115,77 +123,76 @@ public struct BuildTargetTag: Codable, Hashable, RawRepresentable, Sendable {
115123
/// execute.
116124
public static let integrationTest: Self = Self(rawValue: "integration-test")
117125

118-
/// Target contains source code to measure performance of a program, may have
119-
/// but does not require the `canRun` build target capability.
120-
public static let benchmark: Self = Self(rawValue: "benchmark")
126+
/// Target contains re-usable functionality for downstream targets. May have
127+
/// any combination of capabilities.
128+
public static let library: Self = Self(rawValue: "library")
129+
130+
/// Actions on the target such as build and test should only be invoked manually
131+
/// and explicitly. For example, triggering a build on all targets in the workspace
132+
/// should by default not include this target.
133+
/// The original motivation to add the "manual" tag comes from a similar functionality
134+
/// that exists in Bazel, where targets with this tag have to be specified explicitly
135+
/// on the command line.
136+
public static let manual: Self = Self(rawValue: "manual")
121137

122138
/// Target should be ignored by IDEs.
123139
public static let noIDE: Self = Self(rawValue: "no-ide")
140+
141+
/// Target contains source code for testing purposes, may have but does not
142+
/// require the `canTest` capability.
143+
public static let test: Self = Self(rawValue: "test")
144+
145+
/// This is a target of a dependency from the project the user opened, eg. a target that builds a SwiftPM dependency.
146+
///
147+
/// **(BSP Extension)**
148+
public static let dependency: Self = Self(rawValue: "dependency")
124149
}
125150

126151
public struct BuildTargetCapabilities: Codable, Hashable, Sendable {
127152
/// This target can be compiled by the BSP server.
128-
public var canCompile: Bool
153+
public var canCompile: Bool?
129154

130155
/// This target can be tested by the BSP server.
131-
public var canTest: Bool
156+
public var canTest: Bool?
132157

133158
/// This target can be run by the BSP server.
134-
public var canRun: Bool
159+
public var canRun: Bool?
160+
161+
/// This target can be debugged by the BSP server.
162+
public var canDebug: Bool?
135163

136-
public init(canCompile: Bool, canTest: Bool, canRun: Bool) {
164+
public init(canCompile: Bool? = nil, canTest: Bool? = nil, canRun: Bool? = nil, canDebug: Bool? = nil) {
137165
self.canCompile = canCompile
138166
self.canTest = canTest
139167
self.canRun = canRun
168+
self.canDebug = canDebug
140169
}
141170
}
142171

143-
/// The build target sources request is sent from the client to the server to
144-
/// query for the list of text documents and directories that are belong to a
145-
/// build target. The sources response must not include sources that are
146-
/// external to the workspace.
147-
public struct BuildTargetSources: RequestType, Hashable {
148-
public static let method: String = "buildTarget/sources"
149-
public typealias Response = BuildTargetSourcesResult
150-
151-
public var targets: [BuildTargetIdentifier]
172+
public struct BuildTargetDataKind: RawRepresentable, Codable, Hashable, Sendable {
173+
public var rawValue: String
152174

153-
public init(targets: [BuildTargetIdentifier]) {
154-
self.targets = targets
175+
public init(rawValue: String) {
176+
self.rawValue = rawValue
155177
}
156-
}
157-
158-
public struct BuildTargetSourcesResult: ResponseType, Hashable {
159-
public var items: [SourcesItem]
160-
}
161-
162-
public struct SourcesItem: Codable, Hashable, Sendable {
163-
public var target: BuildTargetIdentifier
164178

165-
/// The text documents and directories that belong to this build target.
166-
public var sources: [SourceItem]
167-
}
179+
/// `data` field must contain a CargoBuildTarget object.
180+
public static let cargo = "cargo"
168181

169-
public struct SourceItem: Codable, Hashable, Sendable {
170-
/// Either a text document or a directory. A directory entry must end with a
171-
/// forward slash "/" and a directory entry implies that every nested text
172-
/// document within the directory belongs to this source item.
173-
public var uri: URI
182+
/// `data` field must contain a CppBuildTarget object.
183+
public static let cpp = "cpp"
174184

175-
/// Type of file of the source item, such as whether it is file or directory.
176-
public var kind: SourceItemKind
185+
/// `data` field must contain a JvmBuildTarget object.
186+
public static let jvm = "jvm"
177187

178-
/// Indicates if this source is automatically generated by the build and is
179-
/// not intended to be manually edited by the user.
180-
public var generated: Bool
181-
}
188+
/// `data` field must contain a PythonBuildTarget object.
189+
public static let python = "python"
182190

183-
public enum SourceItemKind: Int, Codable, Hashable, Sendable {
184-
/// The source item references a normal file.
185-
case file = 1
191+
/// `data` field must contain a SbtBuildTarget object.
192+
public static let sbt = "sbt"
186193

187-
/// The source item references a directory.
188-
case directory = 2
194+
/// `data` field must contain a ScalaBuildTarget object.
195+
public static let scala = "scala"
189196
}
190197

191198
/// The build target output paths request is sent from the client to the server

Sources/BuildServerProtocol/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
add_library(BuildServerProtocol STATIC
2-
BuildTargets.swift
2+
BuildTargetSourcesRequest.swift
3+
BuildTargetsRequest.swift
34
DidChangeBuildTargetNotification.swift
45
DidChangeWatchedFilesNotification.swift
56
InitializeBuild.swift

Sources/BuildServerProtocol/Messages.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import LanguageServerProtocol
1313

1414
fileprivate let requestTypes: [_RequestType.Type] = [
1515
BuildTargetOutputPaths.self,
16-
BuildTargets.self,
17-
BuildTargetSources.self,
16+
BuildTargetsRequest.self,
17+
BuildTargetSourcesRequest.self,
1818
InitializeBuild.self,
1919
InverseSourcesRequest.self,
2020
RegisterForChanges.self,

Sources/BuildSystemIntegration/BuildServerBuildSystem.swift

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,39 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
270270

271271
package nonisolated var supportsPreparation: Bool { false }
272272

273+
package func buildTargets(request: BuildTargetsRequest) async throws -> BuildTargetsResponse {
274+
// TODO: (BSP migration) Forward this request to the BSP server
275+
return BuildTargetsResponse(targets: [
276+
BuildTarget(
277+
id: .dummy,
278+
displayName: "Compilation database",
279+
baseDirectory: nil,
280+
tags: [.test],
281+
capabilities: BuildTargetCapabilities(),
282+
// Be conservative with the languages that might be used in the target. SourceKit-LSP doesn't use this property.
283+
languageIds: [.c, .cpp, .objective_c, .objective_cpp, .swift],
284+
dependencies: []
285+
)
286+
])
287+
}
288+
289+
package func buildTargetSources(request: BuildTargetSourcesRequest) async throws -> BuildTargetSourcesResponse {
290+
// BuildServerBuildSystem does not support syntactic test discovery or background indexing.
291+
// (https://github.com/swiftlang/sourcekit-lsp/issues/1173).
292+
// TODO: (BSP migration) Forward this request to the BSP server
293+
return BuildTargetSourcesResponse(items: [])
294+
}
295+
296+
package func inverseSources(request: InverseSourcesRequest) -> InverseSourcesResponse {
297+
return InverseSourcesResponse(targets: [BuildTargetIdentifier.dummy])
298+
}
299+
300+
package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {}
301+
302+
package func prepare(request: PrepareTargetsRequest) async throws -> VoidResponse {
303+
throw PrepareNotSupportedError()
304+
}
305+
273306
package func sourceKitOptions(request: SourceKitOptionsRequest) async throws -> SourceKitOptionsResponse? {
274307
// FIXME: (BSP Migration) If the BSP server supports it, send the `SourceKitOptions` request to it. Only do the
275308
// `RegisterForChanges` dance if we are in the legacy mode.
@@ -313,10 +346,6 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
313346
return nil
314347
}
315348

316-
package func inverseSources(request: InverseSourcesRequest) -> InverseSourcesResponse {
317-
return InverseSourcesResponse(targets: [BuildTargetIdentifier.dummy])
318-
}
319-
320349
package func scheduleBuildGraphGeneration() {}
321350

322351
package func waitForUpToDateBuildGraph() async {}
@@ -329,18 +358,6 @@ extension BuildServerBuildSystem: BuiltInBuildSystem {
329358
return nil
330359
}
331360

332-
package func prepare(request: PrepareTargetsRequest) async throws -> VoidResponse {
333-
throw PrepareNotSupportedError()
334-
}
335-
336-
package func didChangeWatchedFiles(notification: BuildServerProtocol.DidChangeWatchedFilesNotification) {}
337-
338-
package func sourceFiles() async -> [SourceFileInfo] {
339-
// BuildServerBuildSystem does not support syntactic test discovery or background indexing.
340-
// (https://github.com/swiftlang/sourcekit-lsp/issues/1173).
341-
return []
342-
}
343-
344361
package func addSourceFilesDidChangeCallback(_ callback: @escaping () async -> Void) {
345362
// BuildServerBuildSystem does not support syntactic test discovery or background indexing.
346363
// (https://github.com/swiftlang/sourcekit-lsp/issues/1173).

0 commit comments

Comments
 (0)