@@ -50,9 +50,7 @@ import SwiftSyntaxBuilder
5050///
5151/// ### After
5252/// ```swift
53- /// { someInt in
54- /// <#T##String#>
55- /// }
53+ /// <#{ <#someInt#> in <#T##String#> }#>
5654/// ```
5755///
5856/// ## Other Type Placeholder
@@ -94,18 +92,18 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
9492
9593 let expanded : String
9694 if let functionType = placeholder. typeForExpansion? . as ( FunctionTypeSyntax . self) {
97- let basicFormat = BasicFormat (
95+ let format = ClosureLiteralFormat (
9896 indentationWidth: context. indentationWidth,
9997 initialIndentation: context. initialIndentation
10098 )
101- var formattedExpansion = functionType. closureExpansion. formatted ( using: basicFormat ) . description
99+ var formattedExpansion = functionType. closureExpansion. formatted ( using: format ) . description
102100 // Strip the initial indentation from the placeholder itself. We only introduced the initial indentation to
103101 // format consecutive lines. We don't want it at the front of the initial line because it replaces an expression
104102 // that might be in the middle of a line.
105103 if formattedExpansion. hasPrefix ( context. initialIndentation. description) {
106104 formattedExpansion = String ( formattedExpansion. dropFirst ( context. initialIndentation. description. count) )
107105 }
108- expanded = formattedExpansion
106+ expanded = wrapInPlaceholder ( formattedExpansion)
109107 } else {
110108 expanded = placeholder. displayText
111109 }
@@ -117,9 +115,9 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
117115}
118116
119117/// 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`.
123121///
124122/// ## Before
125123/// ```swift
@@ -137,12 +135,10 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
137135/// foo(
138136/// closure1: <#T##(Int) -> String##(Int) -> String##(_ someInt: Int) -> String#>,
139137/// 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+ /// )
146142/// ```
147143///
148144/// Expansion on `closure1` and `normalArg` is the same as `ExpandSingleEditorPlaceholder`.
@@ -161,7 +157,7 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
161157 let arg = placeholder. parent? . as ( LabeledExprSyntax . self) ,
162158 let argList = arg. parent? . as ( LabeledExprListSyntax . self) ,
163159 let call = argList. parent? . as ( FunctionCallExprSyntax . self) ,
164- let expandedTrailingClosures = ExpandEditorPlaceholdersToTrailingClosures . expandTrailingClosurePlaceholders (
160+ let expandedClosures = ExpandEditorPlaceholdersToLiteralClosures . expandClosurePlaceholders (
165161 in: call,
166162 ifIncluded: arg,
167163 indentationWidth: context. indentationWidth
@@ -170,11 +166,11 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
170166 return ExpandSingleEditorPlaceholder . textRefactor ( syntax: token)
171167 }
172168
173- return [ SourceEdit . replace ( call, with: expandedTrailingClosures . description) ]
169+ return [ SourceEdit . replace ( call, with: expandedClosures . description) ]
174170 }
175171}
176172
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.
178174///
179175/// ## Before
180176/// ```swift
@@ -189,13 +185,11 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
189185/// ```swift
190186/// foo(
191187/// 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+ /// )
197191/// ```
198- public struct ExpandEditorPlaceholdersToTrailingClosures : SyntaxRefactoringProvider {
192+ public struct ExpandEditorPlaceholdersToLiteralClosures : SyntaxRefactoringProvider {
199193 public struct Context {
200194 public let indentationWidth : Trivia ?
201195
@@ -208,32 +202,24 @@ public struct ExpandEditorPlaceholdersToTrailingClosures: SyntaxRefactoringProvi
208202 syntax call: FunctionCallExprSyntax ,
209203 in context: Context = Context ( )
210204 ) -> 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+ )
212210 }
213211
214212 /// If the given argument is `nil` or one of the last arguments that are all
215213 /// function-typed placeholders and this call doesn't have a trailing
216214 /// closure, then return a replacement of this call with one that uses
217215 /// closures based on the function types provided by each editor placeholder.
218216 /// Otherwise return nil.
219- fileprivate static func expandTrailingClosurePlaceholders (
217+ fileprivate static func expandClosurePlaceholders (
220218 in call: FunctionCallExprSyntax ,
221219 ifIncluded arg: LabeledExprSyntax ? ,
222220 indentationWidth: Trivia ?
223221 ) -> 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)
237223 }
238224}
239225
@@ -244,9 +230,7 @@ extension FunctionTypeSyntax {
244230 /// ```
245231 /// would become
246232 /// ```
247- /// { someInt in
248- /// <#T##String#>
249- /// }
233+ /// { <#someInt#> in <#T##String#> }
250234 /// ```
251235 fileprivate var closureExpansion : ClosureExprSyntax {
252236 let closureSignature : ClosureSignatureSyntax ?
@@ -311,10 +295,10 @@ extension FunctionCallExprSyntax {
311295 /// closure, then return a replacement of this call with one that uses
312296 /// closures based on the function types provided by each editor placeholder.
313297 /// Otherwise return nil.
314- fileprivate func expandTrailingClosurePlaceholders (
298+ fileprivate func expandClosurePlaceholders (
315299 ifIncluded: LabeledExprSyntax ? ,
316300 indentationWidth: Trivia ?
317- ) -> ( expr : FunctionCallExprSyntax , numClosures : Int ) ? {
301+ ) -> FunctionCallExprSyntax ? {
318302 var includedArg = false
319303 var argsToExpand = 0
320304 for arg in arguments. reversed ( ) {
@@ -359,10 +343,7 @@ extension FunctionCallExprSyntax {
359343 }
360344
361345 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) )
366347 }
367348}
368349
0 commit comments