Skip to content

Commit 3674e7f

Browse files
committed
Address code review feedback
1 parent 502c9a8 commit 3674e7f

File tree

2 files changed

+83
-26
lines changed

2 files changed

+83
-26
lines changed

Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,12 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
2626
return []
2727
}
2828

29-
var actions = [CodeAction]()
30-
31-
// Add test target(s)
32-
actions.append(
33-
contentsOf: maybeAddTestTargetActions(call: call, in: scope)
34-
)
35-
36-
// Add product(s).
37-
actions.append(
38-
contentsOf: maybeAddProductActions(call: call, in: scope)
39-
)
40-
41-
return actions
29+
return addTestTargetActions(call: call, in: scope) + addProductActions(call: call, in: scope)
4230
}
4331

4432
/// Produce code actions to add test target(s) if we are currently on
4533
/// a target for which we know how to create a test.
46-
static func maybeAddTestTargetActions(
34+
static func addTestTargetActions(
4735
call: FunctionCallExprSyntax,
4836
in scope: SyntaxCodeActionScope
4937
) -> [CodeAction] {
@@ -84,7 +72,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
8472

8573
/// Produce code actions to add a product if we are currently on
8674
/// a target for which we can create a product.
87-
static func maybeAddProductActions(
75+
static func addProductActions(
8876
call: FunctionCallExprSyntax,
8977
in scope: SyntaxCodeActionScope
9078
) -> [CodeAction] {
@@ -103,7 +91,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
10391

10492
// Describe the target we are going to create.
10593
let product = try ProductDescription(
106-
name: "\(targetName)",
94+
name: targetName,
10795
type: type,
10896
targets: [targetName]
10997
)
@@ -132,6 +120,7 @@ fileprivate extension PackageEditResult {
132120
/// Translate package manifest edits into a workspace edit. This can
133121
/// involve both modifications to the manifest file as well as the creation
134122
/// of new files.
123+
/// `snapshot` is the latest snapshot of the `Package.swift` file.
135124
func asWorkspaceEdit(snapshot: DocumentSnapshot) -> WorkspaceEdit {
136125
// The edits to perform on the manifest itself.
137126
let manifestTextEdits = manifestEdits.map { edit in
@@ -144,9 +133,9 @@ fileprivate extension PackageEditResult {
144133
// If we couldn't figure out the manifest directory, or there are no
145134
// files to add, the only changes are the manifest edits. We're done
146135
// here.
147-
let manifestDirectoryURI = snapshot.uri.fileURL?
136+
let manifestDirectoryURL = snapshot.uri.fileURL?
148137
.deletingLastPathComponent()
149-
guard let manifestDirectoryURI, !auxiliaryFiles.isEmpty else {
138+
guard let manifestDirectoryURL, !auxiliaryFiles.isEmpty else {
150139
return WorkspaceEdit(
151140
changes: [snapshot.uri: manifestTextEdits]
152141
)
@@ -159,8 +148,8 @@ fileprivate extension PackageEditResult {
159148
// Put the manifest changes into the array.
160149
documentChanges.append(
161150
.textDocumentEdit(
162-
.init(
163-
textDocument: .init(snapshot.uri, version: nil),
151+
TextDocumentEdit(
152+
textDocument: .init(snapshot.uri, version: snapshot.version),
164153
edits: manifestTextEdits.map { .textEdit($0) }
165154
)
166155
)
@@ -169,15 +158,15 @@ fileprivate extension PackageEditResult {
169158
// Create an populate all of the auxiliary files.
170159
for (relativePath, contents) in auxiliaryFiles {
171160
guard
172-
let uri = URL(
161+
let url = URL(
173162
string: relativePath.pathString,
174-
relativeTo: manifestDirectoryURI
163+
relativeTo: manifestDirectoryURL
175164
)
176165
else {
177166
continue
178167
}
179168

180-
let documentURI = DocumentURI(uri)
169+
let documentURI = DocumentURI(url)
181170
let createFile = CreateFile(
182171
uri: documentURI
183172
)
@@ -191,8 +180,8 @@ fileprivate extension PackageEditResult {
191180
documentChanges.append(.createFile(createFile))
192181
documentChanges.append(
193182
.textDocumentEdit(
194-
.init(
195-
textDocument: .init(documentURI, version: nil),
183+
TextDocumentEdit(
184+
textDocument: .init(documentURI, version: snapshot.version),
196185
edits: [.textEdit(edit)]
197186
)
198187
)

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,82 @@ final class CodeActionTests: XCTestCase {
520520
}
521521

522522
// Make sure we get the expected package manifest editing actions.
523+
let addTestAction = codeActions.first { action in
524+
return action.title == "Add test target"
525+
}
526+
XCTAssertNotNil(addTestAction)
527+
528+
guard let addTestChanges = addTestAction?.edit?.documentChanges else {
529+
XCTFail("Didn't have changes in the 'Add test target' action")
530+
return
531+
}
532+
533+
guard
534+
let addTestEdit = addTestChanges.lazy.compactMap({ change in
535+
switch change {
536+
case .textDocumentEdit(let edit): edit
537+
default: nil
538+
}
539+
}).first
540+
else {
541+
XCTFail("Didn't have edits")
542+
return
543+
}
544+
545+
XCTAssertTrue(
546+
addTestEdit.edits.contains { edit in
547+
switch edit {
548+
case .textEdit(let edit): edit.newText.contains("testTarget")
549+
case .annotatedTextEdit(let edit): edit.newText.contains("testTarget")
550+
}
551+
}
552+
)
553+
523554
XCTAssertTrue(
524555
codeActions.contains { action in
556+
return action.title == "Add product to export this target"
557+
}
558+
)
559+
}
560+
561+
func testPackageManifestEditingCodeActionNoTestResult() async throws {
562+
let testClient = try await TestSourceKitLSPClient(capabilities: clientCapabilitiesWithCodeActionSupport())
563+
let uri = DocumentURI.for(.swift)
564+
let positions = testClient.openDocument(
565+
"""
566+
// swift-tools-version: 5.5
567+
let package = Package(
568+
name: "packages",
569+
targets: [
570+
.testTar1️⃣get(name: "MyLib"),
571+
]
572+
)
573+
""",
574+
uri: uri
575+
)
576+
577+
let testPosition = positions["1️⃣"]
578+
let request = CodeActionRequest(
579+
range: Range(testPosition),
580+
context: .init(),
581+
textDocument: TextDocumentIdentifier(uri)
582+
)
583+
let result = try await testClient.send(request)
584+
585+
guard case .codeActions(let codeActions) = result else {
586+
XCTFail("Expected code actions")
587+
return
588+
}
589+
590+
// Make sure we get the expected package manifest editing actions.
591+
XCTAssertTrue(
592+
!codeActions.contains { action in
525593
return action.title == "Add test target"
526594
}
527595
)
528596

529597
XCTAssertTrue(
530-
codeActions.contains { action in
598+
!codeActions.contains { action in
531599
return action.title == "Add product to export this target"
532600
}
533601
)

0 commit comments

Comments
 (0)