Skip to content

Commit 6f55f29

Browse files
authored
Merge pull request #2823 from roopekv/fix-extra-newline-after-code-gen-list-format
Fix `CodeGenerationFormat.formatChildrenSeparatedByNewline()` to not add an extra newline to the end of lists that already have one
2 parents f0d2d35 + aa44f26 commit 6f55f29

12 files changed

+9
-645
lines changed

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

+9-5
Original file line numberDiff line numberDiff line change
@@ -136,11 +136,15 @@ public class CodeGenerationFormat: BasicFormat {
136136
}
137137
}
138138
decreaseIndentationLevel()
139-
if !formattedChildren.isEmpty {
140-
formattedChildren[formattedChildren.count - 1] = formattedChildren[formattedChildren.count - 1].with(
141-
\.trailingTrivia,
142-
indentedNewline
143-
)
139+
if let lastChild = formattedChildren.last {
140+
let nextTokenStartsWithNewline =
141+
lastChild.nextToken(viewMode: .sourceAccurate)?.leadingTrivia.first?.isNewline ?? false
142+
if !nextTokenStartsWithNewline {
143+
formattedChildren[formattedChildren.count - 1] = lastChild.with(
144+
\.trailingTrivia,
145+
indentedNewline
146+
)
147+
}
144148
}
145149
return formattedChildren
146150
}

0 commit comments

Comments
 (0)