File tree Expand file tree Collapse file tree 3 files changed +28
-16
lines changed
Sources/SwiftSyntaxMacroExpansion
Tests/SwiftSyntaxMacroExpansionTest Expand file tree Collapse file tree 3 files changed +28
-16
lines changed Original file line number Diff line number Diff line change @@ -453,6 +453,17 @@ public func collapse<Node: SyntaxProtocol>(
453
453
var expansions = expansions
454
454
var separator = " \n \n "
455
455
456
+ // Wrap the expansions in a set of braces.
457
+ func wrapInBraces( ) {
458
+ // Default to 4 spaces if no indentation was passed.
459
+ // In the future, we could consider inferring the indentation width from
460
+ // the expansions to collapse.
461
+ expansions = expansions. map ( { $0. indented ( by: indentationWidth ?? . spaces( 4 ) ) } )
462
+ expansions [ 0 ] = " { \n " + expansions[ 0 ]
463
+ expansions [ expansions. count - 1 ] += " \n } "
464
+ separator = " \n "
465
+ }
466
+
456
467
switch role {
457
468
case . accessor:
458
469
let onDeclarationWithoutAccessor : Bool
@@ -469,16 +480,18 @@ public func collapse<Node: SyntaxProtocol>(
469
480
onDeclarationWithoutAccessor = false
470
481
}
471
482
if onDeclarationWithoutAccessor {
472
- // Default to 4 spaces if no indentation was passed.
473
- // In the future, we could consider inferring the indentation width from
474
- // the expansions to collapse.
475
- expansions = expansions. map ( { $0. indented ( by: indentationWidth ?? . spaces( 4 ) ) } )
476
- expansions [ 0 ] = " { \n " + expansions[ 0 ]
477
- expansions [ expansions. count - 1 ] += " \n } "
478
- separator = " \n "
483
+ wrapInBraces ( )
479
484
}
480
485
case . memberAttribute:
481
486
separator = " "
487
+
488
+ case . body:
489
+ wrapInBraces ( )
490
+
491
+ case . preamble:
492
+ // Only place a single newline between statements.
493
+ separator = " \n "
494
+
482
495
default :
483
496
break
484
497
}
Original file line number Diff line number Diff line change @@ -379,7 +379,7 @@ private func expandPreambleMacro(
379
379
// Match the indentation of the statements if we can, and put newlines around
380
380
// the preamble to separate it from the rest of the body.
381
381
let indentation = decl. body? . statements. indentationOfFirstLine ?? ( decl. indentationOfFirstLine + indentationWidth)
382
- let indentedSource = " \n " + expanded. indented ( by: indentation) + " \n \n "
382
+ let indentedSource = " \n " + expanded. indented ( by: indentation) + " \n "
383
383
return " \( raw: indentedSource) "
384
384
}
385
385
@@ -409,10 +409,13 @@ private func expandBodyMacro(
409
409
return nil
410
410
}
411
411
412
- // Wrap the body in braces.
413
- let beforeBody = decl. body == nil ? " " : " " ;
414
- let indentedSource = beforeBody + " { \n " + expanded. indented ( by: decl. indentationOfFirstLine + indentationWidth) + " \n } \n "
415
- return " \( raw: indentedSource) " as CodeBlockSyntax
412
+ // `expandAttachedMacro` adds the `{` and `}` to wrap the accessor block and
413
+ // then indents it.
414
+ // Remove any indentaiton from the first line using `drop(while:)` and then
415
+ // prepend a space to separate it from the variable declaration
416
+ let leadingWhitespace = decl. body == nil ? " " : " "
417
+ let indentedSource = leadingWhitespace + expanded. indented ( by: decl. indentationOfFirstLine) . drop ( while: { $0. isWhitespace } )
418
+ return " \( raw: indentedSource) "
416
419
}
417
420
418
421
// MARK: - MacroSystem
Original file line number Diff line number Diff line change @@ -80,7 +80,6 @@ final class PreambleMacroTests: XCTestCase {
80
80
81
81
func doSomethingDangerous(operation: String) {
82
82
log( " Entering doSomethingDangerous(operation: \\ (operation)) " )
83
-
84
83
defer {
85
84
log( " Exiting doSomethingDangerous(operation:) " )
86
85
}
@@ -104,12 +103,10 @@ final class PreambleMacroTests: XCTestCase {
104
103
105
104
func doSomethingDangerous(operation: String) {
106
105
log( " Entering doSomethingDangerous(operation: \\ (operation)) " )
107
-
108
106
defer {
109
107
log( " Exiting doSomethingDangerous(operation:) " )
110
108
}
111
109
log( " Entering doSomethingDangerous(operation: \\ (operation)) " )
112
-
113
110
defer {
114
111
log( " Exiting doSomethingDangerous(operation:) " )
115
112
}
@@ -131,7 +128,6 @@ final class PreambleMacroTests: XCTestCase {
131
128
132
129
func f(a: Int, b: String) async throws -> String {
133
130
log( " Entering f(a: \\ (a), b: \\ (b)) " )
134
-
135
131
defer {
136
132
log( " Exiting f(a:b:) " )
137
133
}
You can’t perform that action at this time.
0 commit comments