Skip to content

Commit 09d60b3

Browse files
committed
Update code gen formatting
1 parent 11d3bed commit 09d60b3

File tree

9 files changed

+2191
-908
lines changed

9 files changed

+2191
-908
lines changed

CodeGeneration/Sources/Utils/CodeGenerationFormat.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,71 @@ 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: FunctionParameterListSyntax) -> FunctionParameterListSyntax {
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 > 3 {
64+
return FunctionParameterListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: FunctionParameterSyntax.self))
65+
} else {
66+
return super.visit(node)
67+
}
68+
}
69+
70+
public override func visit(_ node: ArrayElementListSyntax) -> ArrayElementListSyntax {
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 ArrayElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: ArrayElementSyntax.self))
75+
} else {
76+
return super.visit(node)
77+
}
78+
}
79+
80+
public override func visit(_ node: DictionaryElementListSyntax) -> DictionaryElementListSyntax {
81+
let children = node.children(viewMode: .all)
82+
// Short array literals are presented on one line, list each element on a different line.
83+
if children.count > 2 {
84+
return DictionaryElementListSyntax(formatChildrenSeparatedByNewline(children: children, elementType: DictionaryElementSyntax.self))
85+
} else {
86+
return super.visit(node)
87+
}
88+
}
89+
}
90+
91+
// MARK: - Private helpers
92+
93+
fileprivate extension CodeGenerationFormat {
94+
func formatChildrenSeparatedByNewline<SyntaxType: SyntaxProtocol>(children: SyntaxChildren, elementType: SyntaxType.Type) -> [SyntaxType] {
95+
indentationLevel += 1
96+
var formattedChildren = children.map {
97+
self.visit($0).as(SyntaxType.self)!
98+
}
99+
formattedChildren = formattedChildren.map {
100+
if $0.leadingTrivia?.first?.isNewline == true {
101+
return $0
102+
} else {
103+
return $0.with(\.leadingTrivia, indentedNewline + ($0.leadingTrivia ?? []))
104+
}
105+
}
106+
indentationLevel -= 1
107+
if !formattedChildren.isEmpty {
108+
formattedChildren[formattedChildren.count - 1] = formattedChildren[formattedChildren.count - 1].with(\.trailingTrivia, indentedNewline)
109+
}
110+
return formattedChildren
111+
}
45112
}

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)