@@ -104,6 +104,25 @@ public class CodeGenerationFormat: BasicFormat {
104104 }
105105 }
106106
107+ public override func requiresIndent( _ node: some SyntaxProtocol ) -> Bool {
108+ switch node. kind {
109+ case . arrayElementList, . dictionaryElementList, . functionParameterList, . labeledExprList:
110+ let indentManually = node. children ( viewMode: . sourceAccurate) . count > maxElementsOnSameLine
111+ if indentManually {
112+ return false
113+ }
114+ let startsOnNewline =
115+ node. leadingTrivia. contains ( where: \. isNewline)
116+ || node. previousToken ( viewMode: . sourceAccurate) ? . trailingTrivia. contains ( where: \. isNewline) ?? false
117+ if !startsOnNewline {
118+ return false
119+ }
120+ default :
121+ break
122+ }
123+ return super. requiresIndent ( node)
124+ }
125+
107126 // MARK: - Private
108127
109128 private func shouldBeSeparatedByTwoNewlines( node: CodeBlockItemSyntax ) -> Bool {
@@ -114,9 +133,9 @@ public class CodeGenerationFormat: BasicFormat {
114133
115134 private func ensuringTwoLeadingNewlines< NodeType: SyntaxProtocol > ( node: NodeType ) -> NodeType {
116135 if node. leadingTrivia. first? . isNewline ?? false {
117- return node. with ( \. leadingTrivia, indentedNewline + node. leadingTrivia)
136+ return node. with ( \. leadingTrivia, . newline + node. leadingTrivia)
118137 } else {
119- return node. with ( \. leadingTrivia, indentedNewline + indentedNewline + node. leadingTrivia)
138+ return node. with ( \. leadingTrivia, . newlines ( 2 ) + node. leadingTrivia)
120139 }
121140 }
122141
@@ -126,14 +145,20 @@ public class CodeGenerationFormat: BasicFormat {
126145 ) -> [ SyntaxType ] {
127146 increaseIndentationLevel ( )
128147 var formattedChildren = children. map {
129- self . rewrite ( $0. cast ( SyntaxType . self) ) . cast ( SyntaxType . self)
148+ return self . rewrite ( $0. cast ( SyntaxType . self) ) . cast ( SyntaxType . self)
130149 }
131- formattedChildren = formattedChildren. map {
132- if $0. leadingTrivia. first? . isNewline == true {
133- return $0
134- } else {
135- return $0. with ( \. leadingTrivia, indentedNewline + $0. leadingTrivia)
150+ formattedChildren = formattedChildren. map { child in
151+ var child = child
152+ child. trailingTrivia = Trivia ( pieces: child. trailingTrivia. drop ( while: \. isSpaceOrTab) )
153+
154+ let startsOnNewline =
155+ child. leadingTrivia. contains ( where: \. isNewline)
156+ || child. previousToken ( viewMode: . sourceAccurate) ? . trailingTrivia. contains ( where: \. isNewline) ?? false
157+
158+ if !startsOnNewline {
159+ child. leadingTrivia = indentedNewline + child. leadingTrivia
136160 }
161+ return child
137162 }
138163 decreaseIndentationLevel ( )
139164 if let lastChild = formattedChildren. last {
0 commit comments