File tree Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Expand file tree Collapse file tree 1 file changed +17
-1
lines changed Original file line number Diff line number Diff line change @@ -327,7 +327,23 @@ public extension SyntaxProtocol {
327
327
/// Returns a new syntax node with its leading trivia replaced
328
328
/// by the provided trivia.
329
329
func withLeadingTrivia( _ leadingTrivia: Trivia ) -> Self {
330
- return Self ( Syntax ( data. withLeadingTrivia ( leadingTrivia) ) ) !
330
+ var leadingTrivia : [ TriviaPiece ] = leadingTrivia. pieces
331
+
332
+ // If there is 3 or more, then we see if the first two is a newline and a space
333
+ // If so, we need to replace add the correct amount of space after each newline
334
+ // comming after to have the right indentation of the code.
335
+ if leadingTrivia. count > 2 ,
336
+ case . newlines = leadingTrivia [ 0 ] ,
337
+ case . spaces( let spaces) = leadingTrivia [ 1 ] {
338
+
339
+ for i in ( 2 ..< leadingTrivia. count) . reversed ( ) {
340
+ if case . newlines = leadingTrivia [ i] {
341
+ leadingTrivia. insert ( . spaces( spaces) , at: i + 1 )
342
+ }
343
+ }
344
+ }
345
+
346
+ return Self ( Syntax ( data. withLeadingTrivia ( Trivia ( pieces: leadingTrivia) ) ) ) !
331
347
}
332
348
333
349
/// Returns a new syntax node with its trailing trivia replaced
You can’t perform that action at this time.
0 commit comments