Skip to content

Add code actions for adding library/executable/macro targets to a package manifest #1240

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 17, 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
87 changes: 71 additions & 16 deletions Sources/SourceKitLSP/Swift/CodeActions/PackageManifestEdits.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,47 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
return []
}

return addTestTargetActions(call: call, in: scope) + addProductActions(call: call, in: scope)
return addTargetActions(call: call, in: scope) + addTestTargetActions(call: call, in: scope)
+ addProductActions(call: call, in: scope)
}

/// Produce code actions to add new targets of various kinds.
static func addTargetActions(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be private, right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, sure

call: FunctionCallExprSyntax,
in scope: SyntaxCodeActionScope
) -> [CodeAction] {
do {
var actions: [CodeAction] = []
let variants: [(TargetDescription.TargetType, String)] = [
(.regular, "library"),
(.executable, "executable"),
(.macro, "macro"),
]

for (type, name) in variants {
let target = try TargetDescription(
name: "NewTarget",
type: type
)

let edits = try AddTarget.addTarget(
target,
to: scope.file
)

actions.append(
CodeAction(
title: "Add \(name) target",
kind: .refactor,
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot)
)
)
}

return actions
} catch {
return []
}
}

/// Produce code actions to add test target(s) if we are currently on
Expand All @@ -43,28 +83,43 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
}

do {
// Describe the target we are going to create.
let target = try TargetDescription(
name: "\(targetName)Tests",
dependencies: [.byName(name: targetName, condition: nil)],
type: .test
)
var actions: [CodeAction] = []

let edits = try AddTarget.addTarget(target, to: scope.file)
return [
CodeAction(
title: "Add test target",
kind: .refactor,
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot)
)
let variants: [(AddTarget.TestHarness, String)] = [
(.swiftTesting, "Swift Testing"),
(.xctest, "XCTest"),
]
for (testingLibrary, libraryName) in variants {
// Describe the target we are going to create.
let target = try TargetDescription(
name: "\(targetName)Tests",
dependencies: [.byName(name: targetName, condition: nil)],
type: .test
)

let edits = try AddTarget.addTarget(
target,
to: scope.file,
configuration: .init(testHarness: testingLibrary)
)

actions.append(
CodeAction(
title: "Add test target (\(libraryName))",
kind: .refactor,
edit: edits.asWorkspaceEdit(snapshot: scope.snapshot)
)
)
}

return actions
} catch {
return []
}
}

/// A list of target kinds that allow the creation of tests.
static let targetsThatAllowTests: Set<String> = [
static let targetsThatAllowTests: [String] = [
"executableTarget",
"macro",
"target",
Expand Down Expand Up @@ -110,7 +165,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
}

/// A list of target kinds that allow the creation of tests.
static let targetsThatAllowProducts: Set<String> = [
static let targetsThatAllowProducts: [String] = [
"executableTarget",
"target",
]
Expand Down
10 changes: 8 additions & 2 deletions Tests/SourceKitLSPTests/CodeActionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -609,12 +609,18 @@ final class CodeActionTests: XCTestCase {

// Make sure we get the expected package manifest editing actions.
let addTestAction = codeActions.first { action in
return action.title == "Add test target"
return action.title == "Add test target (Swift Testing)"
}
XCTAssertNotNil(addTestAction)

XCTAssertTrue(
codeActions.contains { action in
action.title == "Add library target"
}
)

guard let addTestChanges = addTestAction?.edit?.documentChanges else {
XCTFail("Didn't have changes in the 'Add test target' action")
XCTFail("Didn't have changes in the 'Add test target (Swift Testing)' action")
return
}

Expand Down