Skip to content

Commit 7d5b5de

Browse files
committed
Update code gen formatting
1 parent 11d3bed commit 7d5b5de

File tree

9 files changed

+1504
-817
lines changed

9 files changed

+1504
-817
lines changed

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,61 @@ public class CodeGenerationFormat: BasicFormat {
4242
return super.visit(node)
4343
}
4444
}
45+
46+
public override func visit(_ node: TupleExprElementListSyntax) -> TupleExprElementListSyntax {
47+
let children = node.children(viewMode: .all)
48+
// If the function only takes a single argument, display it on the same line
49+
if let callee = node.parent?.as(FunctionCallExprSyntax.self)?.calledExpression.as(MemberAccessExprSyntax.self), callee.base == nil {
50+
// This is a constructor for tokens. Write them on a single line
51+
return super.visit(node)
52+
}
53+
if children.count > 3 {
54+
return TupleExprElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: TupleExprElementSyntax.self))
55+
} else {
56+
return super.visit(node)
57+
}
58+
}
59+
60+
public override func visit(_ node: ArrayElementListSyntax) -> ArrayElementListSyntax {
61+
let children = node.children(viewMode: .all)
62+
// Short array literals are presented on one line, list each element on a different line.
63+
if children.count > 2 {
64+
return ArrayElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: ArrayElementSyntax.self))
65+
} else {
66+
return super.visit(node)
67+
}
68+
}
69+
70+
public override func visit(_ node: DictionaryElementListSyntax) -> DictionaryElementListSyntax {
71+
let children = node.children(viewMode: .all)
72+
// Short array literals are presented on one line, list each element on a different line.
73+
if children.count > 2 {
74+
return DictionaryElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: DictionaryElementSyntax.self))
75+
} else {
76+
return super.visit(node)
77+
}
78+
}
79+
}
80+
81+
// MARK: - Private helpers
82+
83+
fileprivate extension CodeGenerationFormat {
84+
func formatChildrenSeparatedByNewline<SyntaxType: SyntaxProtocol>(children: SyntaxChildren, elementType: SyntaxType.Type) -> [SyntaxType] {
85+
indentationLevel += 1
86+
var formattedChildren = children.map {
87+
self.visit($0).as(SyntaxType.self)!
88+
}
89+
formattedChildren = formattedChildren.map {
90+
if $0.leadingTrivia?.first?.isNewline == true {
91+
return $0
92+
} else {
93+
return $0.with(\.leadingTrivia, indentedNewline + ($0.leadingTrivia ?? []))
94+
}
95+
}
96+
indentationLevel -= 1
97+
if !formattedChildren.isEmpty {
98+
formattedChildren[formattedChildren.count - 1] = formattedChildren[formattedChildren.count - 1].with(\.trailingTrivia, indentedNewline)
99+
}
100+
return formattedChildren
101+
}
45102
}

CodeGeneration/Sources/generate-swiftsyntax/templates/swiftsyntax/KeywordFile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Utils
1717

1818
let lookupTable = ArrayExprSyntax(leftSquare: .leftSquareBracketToken(trailingTrivia: .newline)) {
1919
for keyword in KEYWORDS {
20-
ArrayElementSyntax(expression: ExprSyntax("\(literal: keyword.name)"), trailingComma: .commaToken(), trailingTrivia: .newline)
20+
ArrayElementSyntax(expression: ExprSyntax("\(literal: keyword.name)"), trailingComma: .commaToken())
2121
}
2222
}
2323

Sources/SwiftBasicFormat/generated/BasicFormat.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ open class BasicFormat: SyntaxRewriter {
6161
leadingTrivia = leadingTrivia.indented(indentation: indentation)
6262
trailingTrivia = trailingTrivia.indented(indentation: indentation)
6363
let rewritten = TokenSyntax(
64-
node.tokenKind,
65-
leadingTrivia: leadingTrivia,
66-
trailingTrivia: trailingTrivia,
67-
presence: node.presence
64+
node.tokenKind,
65+
leadingTrivia: leadingTrivia,
66+
trailingTrivia: trailingTrivia,
67+
presence: node.presence
68+
6869
)
6970
lastRewrittenToken = rewritten
7071
putNextTokenOnNewLine = false

0 commit comments

Comments
 (0)