@@ -156,7 +156,7 @@ public func expandFreestandingMacro(
156
156
( . codeItem, _) , ( . preamble, _) , ( . body, _) :
157
157
throw MacroExpansionError . unmatchedMacroRole ( definition, macroRole)
158
158
}
159
- return expandedSyntax. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
159
+ return expandedSyntax. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
160
160
} catch {
161
161
context. addDiagnostics ( from: error, node: node)
162
162
return nil
@@ -273,7 +273,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
273
273
in: context
274
274
)
275
275
return accessors. map {
276
- $0. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
276
+ $0. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
277
277
}
278
278
279
279
case ( let attachedMacro as MemberAttributeMacro . Type , . memberAttribute) :
@@ -294,7 +294,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
294
294
295
295
// Form a buffer containing an attribute list to return to the caller.
296
296
return attributes. map {
297
- $0. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
297
+ $0. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
298
298
}
299
299
300
300
case ( let attachedMacro as MemberMacro . Type , . member) :
@@ -313,7 +313,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
313
313
314
314
// Form a buffer of member declarations to return to the caller.
315
315
return members. map {
316
- $0. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
316
+ $0. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
317
317
}
318
318
319
319
case ( let attachedMacro as PeerMacro . Type , . peer) :
@@ -326,7 +326,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
326
326
327
327
// Form a buffer of peer declarations to return to the caller.
328
328
return peers. map {
329
- $0. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
329
+ $0. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
330
330
}
331
331
332
332
case ( let attachedMacro as ExtensionMacro . Type , . extension) :
@@ -357,7 +357,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
357
357
358
358
// Form a buffer of peer declarations to return to the caller.
359
359
return extensions. map {
360
- $0. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
360
+ $0. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
361
361
}
362
362
363
363
case ( let attachedMacro as PreambleMacro . Type , . preamble) :
@@ -375,7 +375,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
375
375
in: context
376
376
)
377
377
return preamble. map {
378
- $0. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
378
+ $0. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
379
379
}
380
380
381
381
case ( let attachedMacro as BodyMacro . Type , . body) :
@@ -400,7 +400,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
400
400
}
401
401
402
402
return body. map {
403
- $0. formattedExpansion ( definition. formatMode , indentationWidth: indentationWidth)
403
+ $0. adjustedMacroExpansion ( for : definition, indentationWidth: indentationWidth)
404
404
}
405
405
406
406
default :
@@ -511,15 +511,24 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
511
511
}
512
512
513
513
fileprivate extension SyntaxProtocol {
514
- /// Perform a format if required and then trim any leading/trailing
515
- /// whitespace.
516
- func formattedExpansion( _ mode: FormatMode , indentationWidth: Trivia ? ) -> String {
517
- switch mode {
514
+ /// Perform post-expansion adjustments to the result of a macro expansion.
515
+ ///
516
+ /// This applies adjustments to the result of a macro expansion to normalize
517
+ /// it for use in later tools. Each of the adjustments here should have a
518
+ /// corresponding configuration option in the `Macro` protocol.
519
+ func adjustedMacroExpansion(
520
+ for macro: Macro . Type ,
521
+ indentationWidth: Trivia ?
522
+ ) -> String {
523
+ let syntax = Syntax ( self )
524
+
525
+ // Formatting.
526
+ switch macro. formatMode {
518
527
case . auto:
519
- return self . formatted ( using: BasicFormat ( indentationWidth: indentationWidth) )
528
+ return syntax . formatted ( using: BasicFormat ( indentationWidth: indentationWidth) )
520
529
. trimmedDescription ( matching: \. isWhitespace)
521
530
case . disabled:
522
- return Syntax ( self ) . description
531
+ return syntax . description
523
532
#if RESILIENT_LIBRARIES
524
533
@unknown default :
525
534
fatalError ( )
0 commit comments