Skip to content

Commit 0d670e7

Browse files
authored
Add API for background preparation in SourceKit-LSP (#7540)
- Add an API to get the name of a target from SourceKit-LSP - We will need this API to prepare a target for indexing. - Add an API to get all the targets in a build description / modules graph in a topological order - We need this for background preparation for indexing so that we can prepare and index lower-level targets first.
1 parent 8698475 commit 0d670e7

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

Sources/PackageGraph/ModulesGraph.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,14 @@ public struct ModulesGraph {
101101
/// Returns all the targets in the graph, regardless if they are reachable from the root targets or not.
102102
public private(set) var allTargets: IdentifiableSet<ResolvedModule>
103103

104+
/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
105+
/// dependencies).
106+
package var allTargetsInTopologicalOrder: [ResolvedModule] {
107+
get throws {
108+
try topologicalSort(Array(allTargets)) { $0.dependencies.compactMap { $0.target } }.reversed()
109+
}
110+
}
111+
104112
/// Returns all the products in the graph, regardless if they are reachable from the root targets or not.
105113
public private(set) var allProducts: IdentifiableSet<ResolvedProduct>
106114

Sources/SourceKitLSPAPI/BuildDescription.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ import struct PackageGraph.ModulesGraph
2727
public protocol BuildTarget {
2828
var sources: [URL] { get }
2929

30+
/// The name of the target. It should be possible to build a target by passing this name to `swift build --target`
31+
var name: String { get }
32+
3033
/// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
3134
var isPartOfRootPackage: Bool { get }
3235

@@ -46,6 +49,10 @@ private struct WrappedClangTargetBuildDescription: BuildTarget {
4649
return (try? description.compilePaths().map { URL(fileURLWithPath: $0.source.pathString) }) ?? []
4750
}
4851

52+
public var name: String {
53+
return description.clangTarget.name
54+
}
55+
4956
public func compileArguments(for fileURL: URL) throws -> [String] {
5057
let filePath = try resolveSymlinks(try AbsolutePath(validating: fileURL.path))
5158
let commandLine = try description.emitCommandLine(for: filePath)
@@ -63,6 +70,10 @@ private struct WrappedSwiftTargetBuildDescription: BuildTarget {
6370
self.isPartOfRootPackage = isPartOfRootPackage
6471
}
6572

73+
public var name: String {
74+
return description.target.name
75+
}
76+
6677
var sources: [URL] {
6778
return description.sources.map { URL(fileURLWithPath: $0.pathString) }
6879
}
@@ -110,4 +121,12 @@ public struct BuildDescription {
110121
return nil
111122
}
112123
}
124+
125+
/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
126+
/// dependencies).
127+
public func allTargetsInTopologicalOrder(in modulesGraph: ModulesGraph) throws -> [BuildTarget] {
128+
try modulesGraph.allTargetsInTopologicalOrder.compactMap {
129+
getBuildTarget(for: $0, in: modulesGraph)
130+
}
131+
}
113132
}

Sources/SourceKitLSPAPI/PluginTargetBuildDescription.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ struct PluginTargetBuildDescription: BuildTarget {
3434
return target.sources.paths.map { URL(fileURLWithPath: $0.pathString) }
3535
}
3636

37+
var name: String {
38+
return target.name
39+
}
40+
3741
func compileArguments(for fileURL: URL) throws -> [String] {
3842
// FIXME: This is very odd and we should clean this up by merging `ManifestLoader` and `DefaultPluginScriptRunner` again.
3943
let loader = ManifestLoader(toolchain: try UserToolchain(swiftSDK: .hostSwiftSDK()))

0 commit comments

Comments
 (0)