From ac4a1f24d6283bdae148b9732eb555a61af932e1 Mon Sep 17 00:00:00 2001 From: Roope Virtanen Date: Mon, 16 Sep 2024 04:04:25 +0300 Subject: [PATCH 1/2] Change `CodeGenerationFormat` to keep whitespace after comma if it precedes other trivia on the same line --- CodeGeneration/Sources/Utils/CodeGenerationFormat.swift | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift b/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift index 0ff670166aa..4ed2ed1fa58 100644 --- a/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift +++ b/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift @@ -158,7 +158,14 @@ public class CodeGenerationFormat: BasicFormat { } formattedChildren = formattedChildren.map { child in var child = child - child.trailingTrivia = Trivia(pieces: child.trailingTrivia.drop(while: \.isSpaceOrTab)) + + if let firstNonSpaceOrTabIndex = child.trailingTrivia.firstIndex(where: { !$0.isSpaceOrTab }) { + if child.trailingTrivia[firstNonSpaceOrTabIndex].isNewline { + child.trailingTrivia = Trivia(pieces: child.trailingTrivia.suffix(from: firstNonSpaceOrTabIndex)) + } + } else { + child.trailingTrivia = Trivia() + } if !child.startsOnNewline { child.leadingTrivia = indentedNewline + child.leadingTrivia From 7c4f03a2669fafb5d86396ad5c799dec58ca790d Mon Sep 17 00:00:00 2001 From: Roope Virtanen Date: Sat, 28 Sep 2024 23:15:37 +0300 Subject: [PATCH 2/2] Fix `CodeGenerationFormat` to keep existing trailing trivia when adding a newline after the last element of a list --- CodeGeneration/Sources/Utils/CodeGenerationFormat.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift b/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift index 4ed2ed1fa58..8de45c947c9 100644 --- a/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift +++ b/CodeGeneration/Sources/Utils/CodeGenerationFormat.swift @@ -173,13 +173,15 @@ public class CodeGenerationFormat: BasicFormat { return child } decreaseIndentationLevel() - if let lastChild = formattedChildren.last { + if let lastChild = formattedChildren.last, + !lastChild.trailingTrivia.contains(where: \.isNewline) + { let nextTokenStartsWithNewline = lastChild.nextToken(viewMode: .sourceAccurate)?.leadingTrivia.first?.isNewline ?? false if !nextTokenStartsWithNewline { formattedChildren[formattedChildren.count - 1] = lastChild.with( \.trailingTrivia, - indentedNewline + lastChild.trailingTrivia + indentedNewline ) } }