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>(
453453 var expansions = expansions
454454 var separator = " \n \n "
455455
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+
456467 switch role {
457468 case . accessor:
458469 let onDeclarationWithoutAccessor : Bool
@@ -469,16 +480,18 @@ public func collapse<Node: SyntaxProtocol>(
469480 onDeclarationWithoutAccessor = false
470481 }
471482 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 ( )
479484 }
480485 case . memberAttribute:
481486 separator = " "
487+
488+ case . body:
489+ wrapInBraces ( )
490+
491+ case . preamble:
492+ // Only place a single newline between statements.
493+ separator = " \n "
494+
482495 default :
483496 break
484497 }
Original file line number Diff line number Diff line change @@ -379,7 +379,7 @@ private func expandPreambleMacro(
379379 // Match the indentation of the statements if we can, and put newlines around
380380 // the preamble to separate it from the rest of the body.
381381 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 "
383383 return " \( raw: indentedSource) "
384384}
385385
@@ -409,10 +409,13 @@ private func expandBodyMacro(
409409 return nil
410410 }
411411
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) "
416419}
417420
418421// MARK: - MacroSystem
Original file line number Diff line number Diff line change @@ -80,7 +80,6 @@ final class PreambleMacroTests: XCTestCase {
8080
8181 func doSomethingDangerous(operation: String) {
8282 log( " Entering doSomethingDangerous(operation: \\ (operation)) " )
83-
8483 defer {
8584 log( " Exiting doSomethingDangerous(operation:) " )
8685 }
@@ -104,12 +103,10 @@ final class PreambleMacroTests: XCTestCase {
104103
105104 func doSomethingDangerous(operation: String) {
106105 log( " Entering doSomethingDangerous(operation: \\ (operation)) " )
107-
108106 defer {
109107 log( " Exiting doSomethingDangerous(operation:) " )
110108 }
111109 log( " Entering doSomethingDangerous(operation: \\ (operation)) " )
112-
113110 defer {
114111 log( " Exiting doSomethingDangerous(operation:) " )
115112 }
@@ -131,7 +128,6 @@ final class PreambleMacroTests: XCTestCase {
131128
132129 func f(a: Int, b: String) async throws -> String {
133130 log( " Entering f(a: \\ (a), b: \\ (b)) " )
134-
135131 defer {
136132 log( " Exiting f(a:b:) " )
137133 }
You can’t perform that action at this time.
0 commit comments