Skip to content

Commit 86a3f58

Browse files
committed
[CodeActions] Switch to use swift-syntax package manifest abstractions and refactoring actions
The refactoring actions and supporting types have been recently ported from swift-package-manager to swift-syntax, to support this change let's move code actions to use types and refactoring actions from SwiftRefactor module.
1 parent a85bb22 commit 86a3f58

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,10 @@
1010
//
1111
//===----------------------------------------------------------------------===//
1212

13-
#if !NO_SWIFTPM_DEPENDENCY
14-
1513
import Foundation
1614
import LanguageServerProtocol
17-
import PackageModel
18-
import PackageModelSyntax
1915
import SwiftParser
20-
import SwiftRefactor
16+
@_spi(PackageRefactor) import SwiftRefactor
2117
import SwiftSyntax
2218

2319
import struct Basics.RelativePath
@@ -41,21 +37,21 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
4137
) -> [CodeAction] {
4238
do {
4339
var actions: [CodeAction] = []
44-
let variants: [(TargetDescription.TargetKind, String)] = [
45-
(.regular, "library"),
40+
let variants: [(PackageTarget.TargetKind, String)] = [
41+
(.library, "library"),
4642
(.executable, "executable"),
4743
(.macro, "macro"),
4844
]
4945

5046
for (type, name) in variants {
51-
let target = try TargetDescription(
47+
let target = PackageTarget(
5248
name: "NewTarget",
5349
type: type
5450
)
5551

56-
let edits = try AddTarget.addTarget(
57-
target,
58-
to: scope.file
52+
let edits = try AddPackageTarget.manifestRefactor(
53+
syntax: scope.file,
54+
in: .init(target: target)
5955
)
6056

6157
actions.append(
@@ -89,22 +85,21 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
8985
do {
9086
var actions: [CodeAction] = []
9187

92-
let variants: [(AddTarget.TestHarness, String)] = [
88+
let variants: [(AddPackageTarget.TestHarness, String)] = [
9389
(.swiftTesting, "Swift Testing"),
9490
(.xctest, "XCTest"),
9591
]
9692
for (testingLibrary, libraryName) in variants {
9793
// Describe the target we are going to create.
98-
let target = try TargetDescription(
94+
let target = PackageTarget(
9995
name: "\(targetName)Tests",
100-
dependencies: [.byName(name: targetName, condition: nil)],
101-
type: .test
96+
type: .test,
97+
dependencies: [.byName(name: targetName)],
10298
)
10399

104-
let edits = try AddTarget.addTarget(
105-
target,
106-
to: scope.file,
107-
configuration: .init(testHarness: testingLibrary)
100+
let edits = try AddPackageTarget.manifestRefactor(
101+
syntax: scope.file,
102+
in: .init(target: target, testHarness: testingLibrary)
108103
)
109104

110105
actions.append(
@@ -143,19 +138,23 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
143138
}
144139

145140
do {
146-
let type: ProductType =
141+
let type: ProductDescription.ProductType =
147142
calledMember == "executableTarget"
148143
? .executable
149144
: .library(.automatic)
150145

151146
// Describe the target we are going to create.
152-
let product = try ProductDescription(
147+
let product = ProductDescription(
153148
name: targetName,
154149
type: type,
155150
targets: [targetName]
156151
)
157152

158-
let edits = try AddProduct.addProduct(product, to: scope.file)
153+
let edits = try AddProduct.manifestRefactor(
154+
syntax: scope.file,
155+
in: .init(product: product)
156+
)
157+
159158
return [
160159
CodeAction(
161160
title: "Add product to export this target",
@@ -175,7 +174,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
175174
]
176175
}
177176

178-
fileprivate extension PackageEditResult {
177+
fileprivate extension PackageEdit {
179178
/// Translate package manifest edits into a workspace edit. This can
180179
/// involve both modifications to the manifest file as well as the creation
181180
/// of new files.
@@ -218,7 +217,7 @@ fileprivate extension PackageEditResult {
218217
for (relativePath, contents) in auxiliaryFiles {
219218
guard
220219
let url = URL(
221-
string: relativePath.pathString,
220+
string: relativePath,
222221
relativeTo: manifestDirectoryURL
223222
)
224223
else {
@@ -300,5 +299,3 @@ fileprivate extension FunctionCallExprSyntax {
300299
return memberAccess.declName.baseName.text
301300
}
302301
}
303-
304-
#endif

0 commit comments

Comments
 (0)