Skip to content

Commit 801b535

Browse files
authored
Merge pull request #2833 from roopekv/fix-code-gen-list-format-indentation
Fix `CodeGenerationFormat` to not add extra indentation, leave trailing spaces when adding newlines, or indent empty lines
2 parents 9b9765b + 29616eb commit 801b535

40 files changed

+25189
-25164
lines changed

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

+33-8
Original file line numberDiff line numberDiff line change
@@ -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 {

Sources/SwiftParser/generated/ExperimentalFeatures.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ extension Parser {
1616
@_spi(ExperimentalLanguageFeatures)
1717
public struct ExperimentalFeatures: OptionSet, Sendable {
1818
public let rawValue: UInt
19-
19+
2020
public init(rawValue: UInt) {
2121
self.rawValue = rawValue
2222
}
@@ -26,16 +26,16 @@ extension Parser {
2626
extension Parser.ExperimentalFeatures {
2727
/// Whether to enable the parsing of reference bindings.
2828
public static let referenceBindings = Self (rawValue: 1 << 0)
29-
29+
3030
/// Whether to enable the parsing of 'then' statements.
3131
public static let thenStatements = Self (rawValue: 1 << 1)
32-
32+
3333
/// Whether to enable the parsing of 'do' expressions.
3434
public static let doExpressions = Self (rawValue: 1 << 2)
35-
35+
3636
/// Whether to enable the parsing of NonEscableTypes.
3737
public static let nonescapableTypes = Self (rawValue: 1 << 3)
38-
38+
3939
/// Whether to enable the parsing of trailing comma.
4040
public static let trailingComma = Self (rawValue: 1 << 4)
4141
}

Sources/SwiftParser/generated/LayoutNodes+Parsable.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ fileprivate extension Parser {
383383
}
384384
return node
385385
}
386-
386+
387387
mutating func parseExpression() -> RawExprSyntax {
388388
return self.parseExpression(flavor: .basic, pattern: .none)
389389
}

0 commit comments

Comments
 (0)