@@ -70,7 +70,17 @@ import SwiftSyntaxBuilder
7070/// anything here
7171/// ```
7272struct ExpandSingleEditorPlaceholder : EditRefactoringProvider {
73- static func textRefactor( syntax token: TokenSyntax , in context: Void ) -> [ SourceEdit ] {
73+ struct Context {
74+ let indentationWidth : Trivia ?
75+ let initialIndentation : Trivia
76+
77+ init ( indentationWidth: Trivia ? = nil , initialIndentation: Trivia = [ ] ) {
78+ self . indentationWidth = indentationWidth
79+ self . initialIndentation = initialIndentation
80+ }
81+ }
82+
83+ static func textRefactor( syntax token: TokenSyntax , in context: Context = Context ( ) ) -> [ SourceEdit ] {
7484 guard let placeholder = EditorPlaceholderData ( token: token) else {
7585 return [ ]
7686 }
@@ -81,7 +91,18 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
8191 expanded = String ( text)
8292 case let . typed( text, type) :
8393 if let functionType = type. as ( FunctionTypeSyntax . self) {
84- expanded = functionType. closureExpansion. formatted ( ) . description
94+ let basicFormat = BasicFormat (
95+ indentationWidth: context. indentationWidth,
96+ initialIndentation: context. initialIndentation
97+ )
98+ var formattedExpansion = functionType. closureExpansion. formatted ( using: basicFormat) . description
99+ // Strip the initial indentation from the placeholder itself. We only introduced the initial indentation to
100+ // format consecutive lines. We don't want it at the front of the initial line because it replaces an expression
101+ // that might be in the middle of a line.
102+ if formattedExpansion. hasPrefix ( context. initialIndentation. description) {
103+ formattedExpansion = String ( formattedExpansion. dropFirst ( context. initialIndentation. description. count) )
104+ }
105+ expanded = formattedExpansion
85106 } else {
86107 expanded = String ( text)
87108 }
@@ -122,13 +143,25 @@ struct ExpandSingleEditorPlaceholder: EditRefactoringProvider {
122143///
123144/// Expansion on `closure1` and `normalArg` is the same as `ExpandSingleEditorPlaceholder`.
124145public struct ExpandEditorPlaceholder : EditRefactoringProvider {
125- public static func textRefactor( syntax token: TokenSyntax , in context: Void ) -> [ SourceEdit ] {
146+ public struct Context {
147+ public let indentationWidth : Trivia ?
148+
149+ public init ( indentationWidth: Trivia ? = nil ) {
150+ self . indentationWidth = indentationWidth
151+ }
152+ }
153+
154+ public static func textRefactor( syntax token: TokenSyntax , in context: Context = Context ( ) ) -> [ SourceEdit ] {
126155 guard let placeholder = token. parent? . as ( DeclReferenceExprSyntax . self) ,
127156 placeholder. baseName. isEditorPlaceholder,
128157 let arg = placeholder. parent? . as ( LabeledExprSyntax . self) ,
129158 let argList = arg. parent? . as ( LabeledExprListSyntax . self) ,
130159 let call = argList. parent? . as ( FunctionCallExprSyntax . self) ,
131- let expandedTrailingClosures = ExpandEditorPlaceholdersToTrailingClosures . expandTrailingClosurePlaceholders ( in: call, ifIncluded: arg)
160+ let expandedTrailingClosures = ExpandEditorPlaceholdersToTrailingClosures . expandTrailingClosurePlaceholders (
161+ in: call,
162+ ifIncluded: arg,
163+ indentationWidth: context. indentationWidth
164+ )
132165 else {
133166 return ExpandSingleEditorPlaceholder . textRefactor ( syntax: token)
134167 }
@@ -159,8 +192,16 @@ public struct ExpandEditorPlaceholder: EditRefactoringProvider {
159192/// }
160193/// ```
161194public struct ExpandEditorPlaceholdersToTrailingClosures : SyntaxRefactoringProvider {
162- public static func refactor( syntax call: FunctionCallExprSyntax , in context: Void = ( ) ) -> FunctionCallExprSyntax ? {
163- return Self . expandTrailingClosurePlaceholders ( in: call, ifIncluded: nil )
195+ public struct Context {
196+ public let indentationWidth : Trivia ?
197+
198+ public init ( indentationWidth: Trivia ? = nil ) {
199+ self . indentationWidth = indentationWidth
200+ }
201+ }
202+
203+ public static func refactor( syntax call: FunctionCallExprSyntax , in context: Context = Context ( ) ) -> FunctionCallExprSyntax ? {
204+ return Self . expandTrailingClosurePlaceholders ( in: call, ifIncluded: nil , indentationWidth: context. indentationWidth)
164205 }
165206
166207 /// If the given argument is `nil` or one of the last arguments that are all
@@ -170,9 +211,10 @@ public struct ExpandEditorPlaceholdersToTrailingClosures: SyntaxRefactoringProvi
170211 /// Otherwise return nil.
171212 fileprivate static func expandTrailingClosurePlaceholders(
172213 in call: FunctionCallExprSyntax ,
173- ifIncluded arg: LabeledExprSyntax ?
214+ ifIncluded arg: LabeledExprSyntax ? ,
215+ indentationWidth: Trivia ?
174216 ) -> FunctionCallExprSyntax ? {
175- guard let expanded = call. expandTrailingClosurePlaceholders ( ifIncluded: arg) else {
217+ guard let expanded = call. expandTrailingClosurePlaceholders ( ifIncluded: arg, indentationWidth : indentationWidth ) else {
176218 return nil
177219 }
178220
@@ -260,7 +302,8 @@ extension FunctionCallExprSyntax {
260302 /// closures based on the function types provided by each editor placeholder.
261303 /// Otherwise return nil.
262304 fileprivate func expandTrailingClosurePlaceholders(
263- ifIncluded: LabeledExprSyntax ?
305+ ifIncluded: LabeledExprSyntax ? ,
306+ indentationWidth: Trivia ?
264307 ) -> ( expr: FunctionCallExprSyntax , numClosures: Int ) ? {
265308 var includedArg = false
266309 var argsToExpand = 0
@@ -283,9 +326,14 @@ extension FunctionCallExprSyntax {
283326 return nil
284327 }
285328
329+ let lineIndentation = self . firstToken ( viewMode: . sourceAccurate) ? . indentationOfLine ?? [ ]
330+
286331 var expandedArgs = [ LabeledExprSyntax] ( )
287332 for arg in arguments. suffix ( argsToExpand) {
288- let edits = ExpandSingleEditorPlaceholder . textRefactor ( syntax: arg. expression. cast ( DeclReferenceExprSyntax . self) . baseName)
333+ let edits = ExpandSingleEditorPlaceholder . textRefactor (
334+ syntax: arg. expression. cast ( DeclReferenceExprSyntax . self) . baseName,
335+ in: ExpandSingleEditorPlaceholder . Context ( indentationWidth: indentationWidth, initialIndentation: lineIndentation)
336+ )
289337 guard edits. count == 1 , let edit = edits. first, !edit. replacement. isEmpty else {
290338 return nil
291339 }
0 commit comments