Skip to content

Add API for background preparation in SourceKit-LSP #7540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Sources/PackageGraph/ModulesGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@ public struct ModulesGraph {
/// Returns all the targets in the graph, regardless if they are reachable from the root targets or not.
public private(set) var allTargets: IdentifiableSet<ResolvedModule>

/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
/// dependencies).
package var allTargetsInTopologicalOrder: [ResolvedModule] {
get throws {
try topologicalSort(Array(allTargets)) { $0.dependencies.compactMap { $0.target } }.reversed()
}
}

/// Returns all the products in the graph, regardless if they are reachable from the root targets or not.
public private(set) var allProducts: IdentifiableSet<ResolvedProduct>

Expand Down
19 changes: 19 additions & 0 deletions Sources/SourceKitLSPAPI/BuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ import struct PackageGraph.ModulesGraph
public protocol BuildTarget {
var sources: [URL] { get }

/// The name of the target. It should be possible to build a target by passing this name to `swift build --target`
var name: String { get }

/// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
var isPartOfRootPackage: Bool { get }

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

public var name: String {
return description.clangTarget.name
}

public func compileArguments(for fileURL: URL) throws -> [String] {
let filePath = try resolveSymlinks(try AbsolutePath(validating: fileURL.path))
let commandLine = try description.emitCommandLine(for: filePath)
Expand All @@ -63,6 +70,10 @@ private struct WrappedSwiftTargetBuildDescription: BuildTarget {
self.isPartOfRootPackage = isPartOfRootPackage
}

public var name: String {
return description.target.name
}

var sources: [URL] {
return description.sources.map { URL(fileURLWithPath: $0.pathString) }
}
Expand Down Expand Up @@ -110,4 +121,12 @@ public struct BuildDescription {
return nil
}
}

/// Returns all targets within the module graph in topological order, starting with low-level targets (that have no
/// dependencies).
public func allTargetsInTopologicalOrder(in modulesGraph: ModulesGraph) throws -> [BuildTarget] {
try modulesGraph.allTargetsInTopologicalOrder.compactMap {
getBuildTarget(for: $0, in: modulesGraph)
}
}
}
4 changes: 4 additions & 0 deletions Sources/SourceKitLSPAPI/PluginTargetBuildDescription.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ struct PluginTargetBuildDescription: BuildTarget {
return target.sources.paths.map { URL(fileURLWithPath: $0.pathString) }
}

var name: String {
return target.name
}

func compileArguments(for fileURL: URL) throws -> [String] {
// FIXME: This is very odd and we should clean this up by merging `ManifestLoader` and `DefaultPluginScriptRunner` again.
let loader = ManifestLoader(toolchain: try UserToolchain(swiftSDK: .hostSwiftSDK()))
Expand Down