Skip to content

Commit 78f6132

Browse files
committed
Add code actions for adding library/executable/macro targets to a package manifest
1 parent 638fd5b commit 78f6132

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
@@ -26,7 +26,47 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
2626
return []
2727
}
2828

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

3272
/// Produce code actions to add test target(s) if we are currently on
@@ -79,7 +119,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
79119
}
80120

81121
/// A list of target kinds that allow the creation of tests.
82-
static let targetsThatAllowTests: Set<String> = [
122+
static let targetsThatAllowTests: [String] = [
83123
"executableTarget",
84124
"macro",
85125
"target",
@@ -125,7 +165,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
125165
}
126166

127167
/// A list of target kinds that allow the creation of tests.
128-
static let targetsThatAllowProducts: Set<String> = [
168+
static let targetsThatAllowProducts: [String] = [
129169
"executableTarget",
130170
"target",
131171
]

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,12 @@ final class CodeActionTests: XCTestCase {
613613
}
614614
XCTAssertNotNil(addTestAction)
615615

616+
XCTAssertTrue(
617+
codeActions.contains { action in
618+
action.title == "Add library target"
619+
}
620+
)
621+
616622
guard let addTestChanges = addTestAction?.edit?.documentChanges else {
617623
XCTFail("Didn't have changes in the 'Add test target (Swift Testing)' action")
618624
return

0 commit comments

Comments
 (0)