@@ -50,16 +50,20 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
50
50
type: type
51
51
)
52
52
53
- let edits = try AddPackageTarget . textRefactor (
54
- syntax: scope. file,
55
- in: . init( target: target)
56
- )
53
+ guard
54
+ let edit = try AddPackageTarget . textRefactor (
55
+ syntax: scope. file,
56
+ in: . init( target: target)
57
+ ) . asWorkspaceEdit ( snapshot: scope. snapshot)
58
+ else {
59
+ continue
60
+ }
57
61
58
62
actions. append (
59
63
CodeAction (
60
64
title: " Add \( name) target " ,
61
65
kind: . refactor,
62
- edit: edits . asWorkspaceEdit ( snapshot : scope . snapshot )
66
+ edit: edit
63
67
)
64
68
)
65
69
}
@@ -98,16 +102,20 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
98
102
dependencies: [ . byName( name: targetName) ] ,
99
103
)
100
104
101
- let edits = try AddPackageTarget . textRefactor (
102
- syntax: scope. file,
103
- in: . init( target: target, testHarness: testingLibrary)
104
- )
105
+ guard
106
+ let edit = try AddPackageTarget . textRefactor (
107
+ syntax: scope. file,
108
+ in: . init( target: target, testHarness: testingLibrary)
109
+ ) . asWorkspaceEdit ( snapshot: scope. snapshot)
110
+ else {
111
+ continue
112
+ }
105
113
106
114
actions. append (
107
115
CodeAction (
108
116
title: " Add test target ( \( libraryName) ) " ,
109
117
kind: . refactor,
110
- edit: edits . asWorkspaceEdit ( snapshot : scope . snapshot )
118
+ edit: edit
111
119
)
112
120
)
113
121
}
@@ -151,16 +159,20 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
151
159
targets: [ targetName]
152
160
)
153
161
154
- let edits = try AddProduct . textRefactor (
155
- syntax: scope. file,
156
- in: . init( product: product)
157
- )
162
+ guard
163
+ let edit = try AddProduct . textRefactor (
164
+ syntax: scope. file,
165
+ in: . init( product: product)
166
+ ) . asWorkspaceEdit ( snapshot: scope. snapshot)
167
+ else {
168
+ return [ ]
169
+ }
158
170
159
171
return [
160
172
CodeAction (
161
173
title: " Add product to export this target " ,
162
174
kind: . refactor,
163
- edit: edits . asWorkspaceEdit ( snapshot : scope . snapshot )
175
+ edit: edit
164
176
)
165
177
]
166
178
} catch {
@@ -175,24 +187,6 @@ struct PackageManifestEdits: SyntaxCodeActionProvider {
175
187
]
176
188
}
177
189
178
- fileprivate extension [ SourceEdit ] {
179
- /// Translate package manifest edits into a workspace edit.
180
- /// `snapshot` is the latest snapshot of the `Package.swift` file.
181
- func asWorkspaceEdit( snapshot: DocumentSnapshot ) -> WorkspaceEdit {
182
- // The edits to perform on the manifest itself.
183
- let manifestTextEdits = map { edit in
184
- TextEdit (
185
- range: snapshot. absolutePositionRange ( of: edit. range) ,
186
- newText: edit. replacement
187
- )
188
- }
189
-
190
- return WorkspaceEdit (
191
- changes: [ snapshot. uri: manifestTextEdits]
192
- )
193
- }
194
- }
195
-
196
190
fileprivate extension SyntaxProtocol {
197
191
// Find an enclosing call syntax expression.
198
192
func findEnclosingCall( ) -> FunctionCallExprSyntax ? {
0 commit comments