@@ -104,6 +104,25 @@ public class CodeGenerationFormat: BasicFormat {
104
104
}
105
105
}
106
106
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
+
107
126
// MARK: - Private
108
127
109
128
private func shouldBeSeparatedByTwoNewlines( node: CodeBlockItemSyntax ) -> Bool {
@@ -114,9 +133,9 @@ public class CodeGenerationFormat: BasicFormat {
114
133
115
134
private func ensuringTwoLeadingNewlines< NodeType: SyntaxProtocol > ( node: NodeType ) -> NodeType {
116
135
if node. leadingTrivia. first? . isNewline ?? false {
117
- return node. with ( \. leadingTrivia, indentedNewline + node. leadingTrivia)
136
+ return node. with ( \. leadingTrivia, . newline + node. leadingTrivia)
118
137
} else {
119
- return node. with ( \. leadingTrivia, indentedNewline + indentedNewline + node. leadingTrivia)
138
+ return node. with ( \. leadingTrivia, . newlines ( 2 ) + node. leadingTrivia)
120
139
}
121
140
}
122
141
@@ -126,14 +145,20 @@ public class CodeGenerationFormat: BasicFormat {
126
145
) -> [ SyntaxType ] {
127
146
increaseIndentationLevel ( )
128
147
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)
130
149
}
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
136
160
}
161
+ return child
137
162
}
138
163
decreaseIndentationLevel ( )
139
164
if let lastChild = formattedChildren. last {
0 commit comments