Skip to content

Commit 0594744

Browse files
committed
[SwiftLanguageService] Adopt changes to package manifest refactoring actions
Previously package manifest refactoring actions conformed to a custom refactoring provider and produced a `PackageEdit` that, in addition to manifest file changes, included a list of auxiliary files. This is no longer the case and all package manifest refactoring actions are now responsible only for manifest file transformations, the rest is handled separately.
1 parent 3274349 commit 0594744

File tree

2 files changed

+11
-82
lines changed

2 files changed

+11
-82
lines changed

Sources/SwiftLanguageService/CodeActions/PackageManifestEdits.swift

Lines changed: 7 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
5050
type: type
5151
)
5252

53-
let edits = try AddPackageTarget.manifestRefactor(
53+
let edits = try AddPackageTarget.textRefactor(
5454
syntax: scope.file,
5555
in: .init(target: target)
5656
)
@@ -98,7 +98,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
9898
dependencies: [.byName(name: targetName)],
9999
)
100100

101-
let edits = try AddPackageTarget.manifestRefactor(
101+
let edits = try AddPackageTarget.textRefactor(
102102
syntax: scope.file,
103103
in: .init(target: target, testHarness: testingLibrary)
104104
)
@@ -151,7 +151,7 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
151151
targets: [targetName]
152152
)
153153

154-
let edits = try AddProduct.manifestRefactor(
154+
let edits = try AddProduct.textRefactor(
155155
syntax: scope.file,
156156
in: .init(product: product)
157157
)
@@ -175,81 +175,20 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
175175
]
176176
}
177177

178-
fileprivate extension PackageEdit {
179-
/// Translate package manifest edits into a workspace edit. This can
180-
/// involve both modifications to the manifest file as well as the creation
181-
/// of new files.
178+
fileprivate extension [SourceEdit] {
179+
/// Translate package manifest edits into a workspace edit.
182180
/// `snapshot` is the latest snapshot of the `Package.swift` file.
183181
func asWorkspaceEdit(snapshot: DocumentSnapshot) -> WorkspaceEdit {
184182
// The edits to perform on the manifest itself.
185-
let manifestTextEdits = manifestEdits.map { edit in
183+
let manifestTextEdits = map { edit in
186184
TextEdit(
187185
range: snapshot.absolutePositionRange(of: edit.range),
188186
newText: edit.replacement
189187
)
190188
}
191189

192-
// If we couldn't figure out the manifest directory, or there are no
193-
// files to add, the only changes are the manifest edits. We're done
194-
// here.
195-
let manifestDirectoryURL = snapshot.uri.fileURL?
196-
.deletingLastPathComponent()
197-
guard let manifestDirectoryURL, !auxiliaryFiles.isEmpty else {
198-
return WorkspaceEdit(
199-
changes: [snapshot.uri: manifestTextEdits]
200-
)
201-
}
202-
203-
// Use the more full-featured documentChanges, which takes precedence
204-
// over the individual changes to documents.
205-
var documentChanges: [WorkspaceEditDocumentChange] = []
206-
207-
// Put the manifest changes into the array.
208-
documentChanges.append(
209-
.textDocumentEdit(
210-
TextDocumentEdit(
211-
textDocument: .init(snapshot.uri, version: snapshot.version),
212-
edits: manifestTextEdits.map { .textEdit($0) }
213-
)
214-
)
215-
)
216-
217-
// Create an populate all of the auxiliary files.
218-
for (relativePath, contents) in auxiliaryFiles {
219-
guard
220-
let url = URL(
221-
string: relativePath,
222-
relativeTo: manifestDirectoryURL
223-
)
224-
else {
225-
continue
226-
}
227-
228-
let documentURI = DocumentURI(url)
229-
let createFile = CreateFile(
230-
uri: documentURI
231-
)
232-
233-
let zeroPosition = Position(line: 0, utf16index: 0)
234-
let edit = TextEdit(
235-
range: zeroPosition..<zeroPosition,
236-
newText: contents.description
237-
)
238-
239-
documentChanges.append(.createFile(createFile))
240-
documentChanges.append(
241-
.textDocumentEdit(
242-
TextDocumentEdit(
243-
textDocument: .init(documentURI, version: snapshot.version),
244-
edits: [.textEdit(edit)]
245-
)
246-
)
247-
)
248-
}
249-
250190
return WorkspaceEdit(
251-
changes: [snapshot.uri: manifestTextEdits],
252-
documentChanges: documentChanges
191+
changes: [snapshot.uri: manifestTextEdits]
253192
)
254193
}
255194
}

Tests/SourceKitLSPTests/CodeActionTests.swift

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -623,29 +623,19 @@ final class CodeActionTests: XCTestCase {
623623
}
624624
)
625625

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

631-
guard
632-
let addTestEdit = addTestChanges.lazy.compactMap({ change in
633-
switch change {
634-
case .textDocumentEdit(let edit): edit
635-
default: nil
636-
}
637-
}).first
638-
else {
631+
guard let manifestEdits = addTestChanges[uri] else {
639632
XCTFail("Didn't have edits")
640633
return
641634
}
642635

643636
XCTAssertTrue(
644-
addTestEdit.edits.contains { edit in
645-
switch edit {
646-
case .textEdit(let edit): edit.newText.contains("testTarget")
647-
case .annotatedTextEdit(let edit): edit.newText.contains("testTarget")
648-
}
637+
manifestEdits.contains { edit in
638+
edit.newText.contains("testTarget")
649639
}
650640
)
651641

0 commit comments

Comments
 (0)