Skip to content

Commit c2af2f5

Browse files
authored
Merge pull request #1240 from DougGregor/code-action-add-target
Add code actions for adding library/executable/macro targets to a package manifest
2 parents 5a7ab33 + 78f6132 commit c2af2f5

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,47 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
2424
return []
2525
}
2626

27-
return addTestTargetActions(call: call, in: scope) + addProductActions(call: call, in: scope)
27+
return addTargetActions(call: call, in: scope) + addTestTargetActions(call: call, in: scope)
28+
+ addProductActions(call: call, in: scope)
29+
}
30+
31+
/// Produce code actions to add new targets of various kinds.
32+
static func addTargetActions(
33+
call: FunctionCallExprSyntax,
34+
in scope: SyntaxCodeActionScope
35+
) -> [CodeAction] {
36+
do {
37+
var actions: [CodeAction] = []
38+
let variants: [(TargetDescription.TargetType, String)] = [
39+
(.regular, "library"),
40+
(.executable, "executable"),
41+
(.macro, "macro"),
42+
]
43+
44+
for (type, name) in variants {
45+
let target = try TargetDescription(
46+
name: "NewTarget",
47+
type: type
48+
)
49+
50+
let edits = try AddTarget.addTarget(
51+
target,
52+
to: scope.file
53+
)
54+
55+
actions.append(
56+
CodeAction(
57+
title: "Add \(name) target",
58+
kind: .refactor,
59+
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot)
60+
)
61+
)
62+
}
63+
64+
return actions
65+
} catch {
66+
return []
67+
}
2868
}
2969

3070
/// Produce code actions to add test target(s) if we are currently on
@@ -77,7 +117,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
77117
}
78118

79119
/// A list of target kinds that allow the creation of tests.
80-
static let targetsThatAllowTests: Set<String> = [
120+
static let targetsThatAllowTests: [String] = [
81121
"executableTarget",
82122
"macro",
83123
"target",
@@ -123,7 +163,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
123163
}
124164

125165
/// A list of target kinds that allow the creation of tests.
126-
static let targetsThatAllowProducts: Set<String> = [
166+
static let targetsThatAllowProducts: [String] = [
127167
"executableTarget",
128168
"target",
129169
]

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,12 @@ final class CodeActionTests: XCTestCase {
609609
}
610610
XCTAssertNotNil(addTestAction)
611611

612+
XCTAssertTrue(
613+
codeActions.contains { action in
614+
action.title == "Add library target"
615+
}
616+
)
617+
612618
guard let addTestChanges = addTestAction?.edit?.documentChanges else {
613619
XCTFail("Didn't have changes in the 'Add test target (Swift Testing)' action")
614620
return

0 commit comments

Comments
 (0)