@@ -50,9 +50,7 @@ import SwiftSyntaxBuilder
50
50
///
51
51
/// ### After
52
52
/// ```swift
53
- /// { someInt in
54
- /// <#T##String#>
55
- /// }
53
+ /// <#{ <#someInt#> in <#T##String#> }#>
56
54
/// ```
57
55
///
58
56
/// ## Other Type Placeholder
@@ -94,18 +92,18 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
94
92
95
93
let expanded : String
96
94
if let functionType = placeholder. typeForExpansion? . as ( FunctionTypeSyntax . self) {
97
- let basicFormat = BasicFormat (
95
+ let format = ClosureLiteralFormat (
98
96
indentationWidth: context. indentationWidth,
99
97
initialIndentation: context. initialIndentation
100
98
)
101
- var formattedExpansion = functionType. closureExpansion. formatted ( using: basicFormat ) . description
99
+ var formattedExpansion = functionType. closureExpansion. formatted ( using: format ) . description
102
100
// Strip the initial indentation from the placeholder itself. We only introduced the initial indentation to
103
101
// format consecutive lines. We don't want it at the front of the initial line because it replaces an expression
104
102
// that might be in the middle of a line.
105
103
if formattedExpansion. hasPrefix ( context. initialIndentation. description) {
106
104
formattedExpansion = String ( formattedExpansion. dropFirst ( context. initialIndentation. description. count) )
107
105
}
108
- expanded = formattedExpansion
106
+ expanded = wrapInPlaceholder ( formattedExpansion)
109
107
} else {
110
108
expanded = placeholder. displayText
111
109
}
@@ -117,9 +115,9 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
117
115
}
118
116
119
117
/// If a function-typed placeholder is the argument to a non-trailing closure
120
- /// call, expands it and any adjacent function-typed placeholders to trailing
121
- /// closures on that call. All other placeholders will expand as per
122
- /// `ExpandEditorPlaceholder`.
118
+ /// call, expands it and any adjacent function-typed placeholders to literal
119
+ /// closures with inner placeholders on that call. All other placeholders will
120
+ /// expand as per `ExpandEditorPlaceholder`.
123
121
///
124
122
/// ## Before
125
123
/// ```swift
@@ -137,12 +135,10 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
137
135
/// foo(
138
136
/// closure1: <#T##(Int) -> String##(Int) -> String##(_ someInt: Int) -> String#>,
139
137
/// normalArg: <#T##Int#>,
140
- /// closure2: { ... }
141
- /// ) { someInt in
142
- /// <#T##String#>
143
- /// } closure2: { someInt in
144
- /// <#T##String#>
145
- /// }
138
+ /// closure2: { ... },
139
+ /// closure3: { <#someInt#> in <#T##String#> },
140
+ /// closure4: { <#someInt#> in <#T##String#> }
141
+ /// )
146
142
/// ```
147
143
///
148
144
/// Expansion on `closure1` and `normalArg` is the same as `ExpandSingleEditorPlaceholder`.
@@ -161,7 +157,7 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
161
157
let arg = placeholder. parent? . as ( LabeledExprSyntax . self) ,
162
158
let argList = arg. parent? . as ( LabeledExprListSyntax . self) ,
163
159
let call = argList. parent? . as ( FunctionCallExprSyntax . self) ,
164
- let expandedTrailingClosures = ExpandEditorPlaceholdersToTrailingClosures . expandTrailingClosurePlaceholders (
160
+ let expandedClosures = ExpandEditorPlaceholdersToLiteralClosures . expandClosurePlaceholders (
165
161
in: call,
166
162
ifIncluded: arg,
167
163
indentationWidth: context. indentationWidth
@@ -170,11 +166,11 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
170
166
return ExpandSingleEditorPlaceholder . textRefactor ( syntax: token)
171
167
}
172
168
173
- return [ SourceEdit . replace ( call, with: expandedTrailingClosures . description) ]
169
+ return [ SourceEdit . replace ( call, with: expandedClosures . description) ]
174
170
}
175
171
}
176
172
177
- /// Expand all the editor placeholders in the function call that can be converted to trailing closures.
173
+ /// Expand all the editor placeholders in the function call to literal closures.
178
174
///
179
175
/// ## Before
180
176
/// ```swift
@@ -189,13 +185,11 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
189
185
/// ```swift
190
186
/// foo(
191
187
/// arg: <#T##Int#>,
192
- /// ) { someInt in
193
- /// <#T##String#>
194
- /// } secondClosure: { someInt in
195
- /// <#T##String#>
196
- /// }
188
+ /// firstClosure: { <#someInt#> in <#T##String#> },
189
+ /// secondClosure: { <#someInt#> in <#T##String#> }
190
+ /// )
197
191
/// ```
198
- public struct ExpandEditorPlaceholdersToTrailingClosures : SyntaxRefactoringProvider {
192
+ public struct ExpandEditorPlaceholdersToLiteralClosures : SyntaxRefactoringProvider {
199
193
public struct Context {
200
194
public let indentationWidth : Trivia ?
201
195
@@ -208,32 +202,24 @@ public struct ExpandEditorPlaceholdersToTrailingClosures: SyntaxRefactoringProvi
208
202
syntax call: FunctionCallExprSyntax ,
209
203
in context: Context = Context ( )
210
204
) -> FunctionCallExprSyntax ? {
211
- return Self . expandTrailingClosurePlaceholders ( in: call, ifIncluded: nil , indentationWidth: context. indentationWidth)
205
+ return Self . expandClosurePlaceholders (
206
+ in: call,
207
+ ifIncluded: nil ,
208
+ indentationWidth: context. indentationWidth
209
+ )
212
210
}
213
211
214
212
/// If the given argument is `nil` or one of the last arguments that are all
215
213
/// function-typed placeholders and this call doesn't have a trailing
216
214
/// closure, then return a replacement of this call with one that uses
217
215
/// closures based on the function types provided by each editor placeholder.
218
216
/// Otherwise return nil.
219
- fileprivate static func expandTrailingClosurePlaceholders (
217
+ fileprivate static func expandClosurePlaceholders (
220
218
in call: FunctionCallExprSyntax ,
221
219
ifIncluded arg: LabeledExprSyntax ? ,
222
220
indentationWidth: Trivia ?
223
221
) -> FunctionCallExprSyntax ? {
224
- guard let expanded = call. expandTrailingClosurePlaceholders ( ifIncluded: arg, indentationWidth: indentationWidth)
225
- else {
226
- return nil
227
- }
228
-
229
- let callToTrailingContext = CallToTrailingClosures . Context (
230
- startAtArgument: call. arguments. count - expanded. numClosures
231
- )
232
- guard let trailing = CallToTrailingClosures . refactor ( syntax: expanded. expr, in: callToTrailingContext) else {
233
- return nil
234
- }
235
-
236
- return trailing
222
+ return call. expandClosurePlaceholders ( ifIncluded: arg, indentationWidth: indentationWidth)
237
223
}
238
224
}
239
225
@@ -244,9 +230,7 @@ extension FunctionTypeSyntax {
244
230
/// ```
245
231
/// would become
246
232
/// ```
247
- /// { someInt in
248
- /// <#T##String#>
249
- /// }
233
+ /// { <#someInt#> in <#T##String#> }
250
234
/// ```
251
235
fileprivate var closureExpansion : ClosureExprSyntax {
252
236
let closureSignature : ClosureSignatureSyntax ?
@@ -311,10 +295,10 @@ extension FunctionCallExprSyntax {
311
295
/// closure, then return a replacement of this call with one that uses
312
296
/// closures based on the function types provided by each editor placeholder.
313
297
/// Otherwise return nil.
314
- fileprivate func expandTrailingClosurePlaceholders (
298
+ fileprivate func expandClosurePlaceholders (
315
299
ifIncluded: LabeledExprSyntax ? ,
316
300
indentationWidth: Trivia ?
317
- ) -> ( expr : FunctionCallExprSyntax , numClosures : Int ) ? {
301
+ ) -> FunctionCallExprSyntax ? {
318
302
var includedArg = false
319
303
var argsToExpand = 0
320
304
for arg in arguments. reversed ( ) {
@@ -359,10 +343,7 @@ extension FunctionCallExprSyntax {
359
343
}
360
344
361
345
let originalArgs = arguments. dropLast ( argsToExpand)
362
- return (
363
- detached. with ( \. arguments, LabeledExprListSyntax ( originalArgs + expandedArgs) ) ,
364
- expandedArgs. count
365
- )
346
+ return detached. with ( \. arguments, LabeledExprListSyntax ( originalArgs + expandedArgs) )
366
347
}
367
348
}
368
349
0 commit comments