@@ -66,7 +66,7 @@ private func expandFreestandingMemberDeclList(
6666 let indentedSource =
6767 expanded
6868 . indented ( by: node. indentationOfFirstLine)
69- . wrappingInNonCommentTrivia ( from: node)
69+ . wrappingInTrivia ( from: node)
7070 return " \( raw: indentedSource) "
7171}
7272
@@ -94,7 +94,7 @@ private func expandFreestandingCodeItemList(
9494 let indentedSource =
9595 expanded
9696 . indented ( by: node. indentationOfFirstLine)
97- . wrappingInNonCommentTrivia ( from: node)
97+ . wrappingInTrivia ( from: node)
9898 return " \( raw: indentedSource) "
9999}
100100
@@ -119,7 +119,7 @@ private func expandFreestandingExpr(
119119 let indentedSource =
120120 expanded
121121 . indented ( by: node. indentationOfFirstLine)
122- . wrappingInNonCommentTrivia ( from: node)
122+ . wrappingInTrivia ( from: node)
123123 return " \( raw: indentedSource) "
124124}
125125
@@ -947,17 +947,18 @@ private extension AccessorBlockSyntax {
947947}
948948
949949private extension String {
950- /// Add any non-comment trivia from `node` before/after this string.
950+ /// Add any trivia from `node` before/after this string.
951951 ///
952- /// We need to do this because the macro is responsible for copying trivia
953- /// from the freestanding macro to the generated declaration.
954- ///
955- /// Essentially, what we want to keep any empty newlines in front of the
956- /// freestanding macro that separate it from the previous declarations.
957- func wrappingInNonCommentTrivia( from node: some SyntaxProtocol ) -> String {
958- return node. leadingTrivia. removingComments. removingIndentation. description
952+ /// We need to do this because we are replacing the entire macro expansion
953+ /// declaration / expression with the expanded source but semantically, the
954+ /// user should think about it as just replacing the `#...` expression without
955+ /// any trivia.
956+ func wrappingInTrivia( from node: some SyntaxProtocol ) -> String {
957+ // We need to remove the indentation from the last line because the macro
958+ // expansion buffer already contains the indentation.
959+ return node. leadingTrivia. removingIndentationOnLastLine. description
959960 + self
960- + node. leadingTrivia . removingComments . removingIndentation . description
961+ + node. trailingTrivia . description
961962 }
962963}
963964
@@ -972,37 +973,3 @@ private extension SyntaxProtocol {
972973 return self . detached
973974 }
974975}
975-
976- private extension Trivia {
977- /// Drop all comments from the trivia.
978- ///
979- /// If a comment is the only entry on a line, drop the entire line instead of
980- /// leaving an empty line.
981- var removingComments : Trivia {
982- var result : [ TriviaPiece ] = [ ]
983-
984- var lineContainedComment = false
985- var lineContainedNonWhitespaceNonComment = false
986- for piece in self . pieces {
987- switch piece {
988- case . spaces, . tabs:
989- result. append ( piece)
990- case . backslashes, . formfeeds, . pounds, . unexpectedText, . verticalTabs:
991- lineContainedNonWhitespaceNonComment = true
992- result. append ( piece)
993- case . blockComment, . docBlockComment, . docLineComment, . lineComment:
994- lineContainedComment = true
995- case . carriageReturns, . carriageReturnLineFeeds, . newlines:
996- if lineContainedComment && !lineContainedNonWhitespaceNonComment {
997- continue
998- } else {
999- result. append ( piece)
1000- }
1001- lineContainedComment = false
1002- lineContainedNonWhitespaceNonComment = false
1003- }
1004- }
1005-
1006- return Trivia ( pieces: result)
1007- }
1008- }
0 commit comments