@@ -66,7 +66,7 @@ private func expandFreestandingMemberDeclList(
66
66
let indentedSource =
67
67
expanded
68
68
. indented ( by: node. indentationOfFirstLine)
69
- . wrappingInNonCommentTrivia ( from: node)
69
+ . wrappingInTrivia ( from: node)
70
70
return " \( raw: indentedSource) "
71
71
}
72
72
@@ -94,7 +94,7 @@ private func expandFreestandingCodeItemList(
94
94
let indentedSource =
95
95
expanded
96
96
. indented ( by: node. indentationOfFirstLine)
97
- . wrappingInNonCommentTrivia ( from: node)
97
+ . wrappingInTrivia ( from: node)
98
98
return " \( raw: indentedSource) "
99
99
}
100
100
@@ -119,7 +119,7 @@ private func expandFreestandingExpr(
119
119
let indentedSource =
120
120
expanded
121
121
. indented ( by: node. indentationOfFirstLine)
122
- . wrappingInNonCommentTrivia ( from: node)
122
+ . wrappingInTrivia ( from: node)
123
123
return " \( raw: indentedSource) "
124
124
}
125
125
@@ -947,17 +947,18 @@ private extension AccessorBlockSyntax {
947
947
}
948
948
949
949
private extension String {
950
- /// Add any non-comment trivia from `node` before/after this string.
950
+ /// Add any trivia from `node` before/after this string.
951
951
///
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
959
960
+ self
960
- + node. leadingTrivia . removingComments . removingIndentation . description
961
+ + node. trailingTrivia . description
961
962
}
962
963
}
963
964
@@ -972,37 +973,3 @@ private extension SyntaxProtocol {
972
973
return self . detached
973
974
}
974
975
}
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