10
10
//
11
11
//===----------------------------------------------------------------------===//
12
12
13
+ import SwiftBasicFormat
13
14
import SwiftDiagnostics
14
15
import SwiftSyntax
15
16
import SwiftSyntaxMacros
@@ -49,21 +50,21 @@ extension CompilerPluginMessageHandler {
49
50
try exprMacroDef. expansion ( of: node, in: context)
50
51
}
51
52
let rewritten = try _openExistential ( macroSyntax, do: _expand)
52
- expandedSource = rewritten. description
53
+ expandedSource = rewritten. formattedExpansion ( macroDefinition . formatMode )
53
54
54
55
case let declMacroDef as DeclarationMacro . Type :
55
56
func _expand< Node: FreestandingMacroExpansionSyntax > ( node: Node ) throws -> [ DeclSyntax ] {
56
57
try declMacroDef. expansion ( of: node, in: context)
57
58
}
58
59
let rewritten = try _openExistential ( macroSyntax, do: _expand)
59
- expandedSource = CodeBlockItemListSyntax ( rewritten. map { CodeBlockItemSyntax ( item: . decl( $0) ) } ) . description
60
+ expandedSource = CodeBlockItemListSyntax ( rewritten. map { CodeBlockItemSyntax ( item: . decl( $0) ) } ) . formattedExpansion ( macroDefinition . formatMode )
60
61
61
62
case let codeItemMacroDef as CodeItemMacro . Type :
62
63
func _expand< Node: FreestandingMacroExpansionSyntax > ( node: Node ) throws -> [ CodeBlockItemSyntax ] {
63
64
try codeItemMacroDef. expansion ( of: node, in: context)
64
65
}
65
66
let rewritten = try _openExistential ( macroSyntax, do: _expand)
66
- expandedSource = CodeBlockItemListSyntax ( rewritten) . description
67
+ expandedSource = CodeBlockItemListSyntax ( rewritten) . formattedExpansion ( macroDefinition . formatMode )
67
68
68
69
default :
69
70
throw MacroExpansionError . unmathedMacroRole
@@ -113,7 +114,7 @@ extension CompilerPluginMessageHandler {
113
114
in: context
114
115
)
115
116
expandedSources = accessors. map {
116
- $0. trimmedDescription
117
+ $0. formattedExpansion ( macroDefinition . formatMode )
117
118
}
118
119
119
120
case ( let attachedMacro as MemberAttributeMacro . Type , . memberAttribute) :
@@ -145,7 +146,7 @@ extension CompilerPluginMessageHandler {
145
146
146
147
// Form a buffer containing an attribute list to return to the caller.
147
148
expandedSources = attributes. map {
148
- $0. trimmedDescription
149
+ $0. formattedExpansion ( macroDefinition . formatMode )
149
150
}
150
151
151
152
case ( let attachedMacro as MemberMacro . Type , . member) :
@@ -170,7 +171,7 @@ extension CompilerPluginMessageHandler {
170
171
let members = try _openExistential ( declGroup, do: expandMemberMacro)
171
172
172
173
// Form a buffer of member declarations to return to the caller.
173
- expandedSources = members. map { $0. trimmedDescription }
174
+ expandedSources = members. map { $0. formattedExpansion ( macroDefinition . formatMode ) }
174
175
175
176
case ( let attachedMacro as PeerMacro . Type , . peer) :
176
177
let peers = try attachedMacro. expansion (
@@ -181,7 +182,7 @@ extension CompilerPluginMessageHandler {
181
182
182
183
// Form a buffer of peer declarations to return to the caller.
183
184
expandedSources = peers. map {
184
- $0. trimmedDescription
185
+ $0. formattedExpansion ( macroDefinition . formatMode )
185
186
}
186
187
187
188
case ( let attachedMacro as ConformanceMacro . Type , . conformance) :
@@ -234,3 +235,17 @@ extension CompilerPluginMessageHandler {
234
235
)
235
236
}
236
237
}
238
+
239
+ fileprivate extension SyntaxProtocol {
240
+ /// Perform a format if required and then trim any leading/trailing
241
+ /// whitespace.
242
+ func formattedExpansion( _ mode: FormatMode ) -> String {
243
+ let formatted : Syntax
244
+ if mode == . auto {
245
+ formatted = self . formatted ( )
246
+ } else {
247
+ formatted = Syntax ( self )
248
+ }
249
+ return formatted. trimmedDescription
250
+ }
251
+ }
0 commit comments