Howdy!
Disclaimer: I'm playing around with swift-syntax
for the first time, so it is very likely I am missing something obvious / doing something wrong. I am using the version from master
.
I would like to know if there is a way to modify indentation of nested structures within SwiftSyntaxBuilder
. The crux of my issue appears to be with how SwitchCaseList
is laid out within a SwitchStmt
. In particular, when generating a list of cases, all are placed within a single line like the following (is this even legal syntax?):
switch expr {case .m1: return val case .m2: return val case .m3: return val ... }
It is in contrast with how cases are laid out within a EnumDecl
(i.e. line-by-line with appropriate indentation) and which I would expect. e.g. I want it to look like this:
switch expr {
case .m1:
return val
case .m2:
return val
case .m3:
return val
...
}
I am able to get the above form by modifying trivia, but that seems like a hack? It also means I have to hard-code the indentation
.
Thanks,
smkuehnhold
I was able to get the desired format by ham-fistedly making the following changes to the source code (from 6fa8f5e
). I am pretty sure it is not a good idea in general to modify "gyb_generated" code by hand, but this is good-enough for me now.
diff --git a/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableCollectionNodes.swift b/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableCollectionNodes.swift
index 9611c0c..04422f1 100644
--- a/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableCollectionNodes.swift
+++ b/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableCollectionNodes.swift
@@ -1315,7 +1315,7 @@ public struct SwitchCaseList: ExpressibleByArrayLiteral, SyntaxBuildable, Expres
public func buildSwitchCaseList(format: Format, leadingTrivia: Trivia? = nil) -> SwitchCaseListSyntax {
let result = SyntaxFactory.makeSwitchCaseList(elements.map {
- $0.buildSyntax(format: format, leadingTrivia: nil)
+ $0.buildSyntax(format: format, leadingTrivia: Trivia.newlines(1) + format._makeIndent())
})
if let leadingTrivia = leadingTrivia {
return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
diff --git a/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift b/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift
index ddbaf30..24c1c14 100644
--- a/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift
+++ b/Sources/SwiftSyntaxBuilder/gyb_generated/BuildableNodes.swift
@@ -9531,7 +9531,7 @@ public struct SwitchStmt: StmtBuildable, ExpressibleAsSwitchStmt {
expression: expression.buildExpr(format: format, leadingTrivia: nil),
leftBrace: leftBrace,
cases: cases.buildSwitchCaseList(format: format, leadingTrivia: nil),
- rightBrace: rightBrace
+ rightBrace: rightBrace.withLeadingTrivia(.newlines(1) + format._makeIndent() + (rightBrace.leadingTrivia ?? []))
)
if let leadingTrivia = leadingTrivia {
return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
@@ -10613,12 +10613,19 @@ public struct SwitchCase: SyntaxBuildable, ExpressibleAsSwitchCase {
statements: statementsBuilder()
)
}
-
+// let result = SyntaxFactory.makeSwitchCaseList(elements.map {
+// $0.buildSyntax(format: format, leadingTrivia: Trivia.newlines(1) + format._makeIndent())
+// })
+// if let leadingTrivia = leadingTrivia {
+// return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
+// } else {
+// return result
+// }
func buildSwitchCase(format: Format, leadingTrivia: Trivia? = nil) -> SwitchCaseSyntax {
let result = SyntaxFactory.makeSwitchCase(
unknownAttr: unknownAttr?.buildAttribute(format: format, leadingTrivia: nil),
label: label.buildSyntax(format: format, leadingTrivia: nil),
- statements: statements.buildCodeBlockItemList(format: format, leadingTrivia: nil)
+ statements: statements.buildCodeBlockItemList(format: format._indented(), leadingTrivia: nil)
)
if let leadingTrivia = leadingTrivia {
return result.withLeadingTrivia(leadingTrivia + (result.leadingTrivia ?? []))
However this does not really answer my original question unfortunately, so I am still all ears if anyone has a solution to that problem.