@@ -99,19 +99,20 @@ extension SyntaxProtocol {
99
99
/// Returns a Swift expression that, when parsed, constructs this syntax node
100
100
/// (or at least an expression that's very close to constructing this node, the addition of a few manual upcast by hand is still needed).
101
101
/// The intended use case for this is to print a syntax tree and create a substructure assertion from the generated expression.
102
- public var debugInitCall : String {
103
- return self . debugInitCallExpr. formatted ( using: InitializerExprFormat ( ) ) . description
102
+ /// When `includeTrivia` is set to `false`, the token's leading and trailing trivia will not be included in the generated expression.
103
+ public func debugInitCall( includeTrivia: Bool = false ) -> String {
104
+ return self . debugInitCallExpr ( includeTrivia: includeTrivia) . formatted ( using: InitializerExprFormat ( ) ) . description
104
105
}
105
106
106
- private var debugInitCallExpr : ExprSyntaxProtocol {
107
+ private func debugInitCallExpr( includeTrivia : Bool ) -> ExprSyntaxProtocol {
107
108
let mirror = Mirror ( reflecting: self )
108
109
if self . isCollection {
109
110
return FunctionCallExpr ( callee: " \( type ( of: self ) ) " ) {
110
111
TupleExprElement (
111
112
expression: ArrayExpr ( ) {
112
113
for child in mirror. children {
113
114
let value = child. value as! SyntaxProtocol ?
114
- ArrayElement ( expression: value? . debugInitCallExpr ?? NilLiteralExpr ( ) )
115
+ ArrayElement ( expression: value? . debugInitCallExpr ( includeTrivia : includeTrivia ) ?? NilLiteralExpr ( ) )
115
116
}
116
117
}
117
118
)
@@ -137,14 +138,14 @@ extension SyntaxProtocol {
137
138
expression: StringLiteralExpr ( content: token. text)
138
139
)
139
140
}
140
- if !token. leadingTrivia. isEmpty {
141
+ if includeTrivia , !token. leadingTrivia. isEmpty {
141
142
TupleExprElement (
142
143
label: . identifier( " leadingTrivia " ) ,
143
144
colon: . colon,
144
145
expression: token. leadingTrivia. initializerExpr
145
146
)
146
147
}
147
- if !token. trailingTrivia. isEmpty {
148
+ if includeTrivia , !token. trailingTrivia. isEmpty {
148
149
TupleExprElement (
149
150
label: . identifier( " trailingTrivia " ) ,
150
151
colon: . colon,
@@ -169,7 +170,7 @@ extension SyntaxProtocol {
169
170
TupleExprElement (
170
171
label: isUnexpected ? nil : . identifier( label) ,
171
172
colon: isUnexpected ? nil : . colon,
172
- expression: value? . debugInitCallExpr ?? NilLiteralExpr ( )
173
+ expression: value? . debugInitCallExpr ( includeTrivia : includeTrivia ) ?? NilLiteralExpr ( )
173
174
)
174
175
}
175
176
}
0 commit comments