Skip to content

Commit 0683ba7

Browse files
committed
Add lineBreakBeforeControlFlowBodies configuration option
1 parent fee799f commit 0683ba7

File tree

2 files changed

+36
-6
lines changed

2 files changed

+36
-6
lines changed

Sources/SwiftFormatConfiguration/Configuration.swift

+6
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public struct Configuration: Codable, Equatable {
2626
case tabWidth
2727
case indentation
2828
case respectsExistingLineBreaks
29+
case lineBreakBeforeControlFlowBodies
2930
case lineBreakBeforeControlFlowKeywords
3031
case lineBreakBeforeEachArgument
3132
case lineBreakBeforeFuncBodies
@@ -75,6 +76,8 @@ public struct Configuration: Codable, Equatable {
7576

7677
/// MARK: Rule-specific configuration
7778

79+
public var lineBreakBeforeControlFlowBodies = false
80+
7881
/// Determines the line-breaking behavior for control flow keywords that follow a closing brace,
7982
/// like `else` and `catch`.
8083
///
@@ -201,6 +204,8 @@ public struct Configuration: Codable, Equatable {
201204
= try container.decodeIfPresent(Indent.self, forKey: .indentation) ?? .spaces(2)
202205
self.respectsExistingLineBreaks
203206
= try container.decodeIfPresent(Bool.self, forKey: .respectsExistingLineBreaks) ?? true
207+
self.lineBreakBeforeControlFlowBodies
208+
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeControlFlowBodies) ?? false
204209
self.lineBreakBeforeControlFlowKeywords
205210
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeControlFlowKeywords) ?? false
206211
self.lineBreakBeforeEachArgument
@@ -242,6 +247,7 @@ public struct Configuration: Codable, Equatable {
242247
try container.encode(tabWidth, forKey: .tabWidth)
243248
try container.encode(indentation, forKey: .indentation)
244249
try container.encode(respectsExistingLineBreaks, forKey: .respectsExistingLineBreaks)
250+
try container.encode(lineBreakBeforeControlFlowBodies, forKey: .lineBreakBeforeControlFlowBodies)
245251
try container.encode(lineBreakBeforeControlFlowKeywords, forKey: .lineBreakBeforeControlFlowKeywords)
246252
try container.encode(lineBreakBeforeEachArgument, forKey: .lineBreakBeforeEachArgument)
247253
try container.encode(lineBreakBeforeEachGenericRequirement, forKey: .lineBreakBeforeEachGenericRequirement)

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+30-6
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
496496
}
497497
}
498498

499-
arrangeBracesAndContents(of: node.elseBody?.as(CodeBlockSyntax.self), contentsKeyPath: \.statements)
499+
arrangeBracesAndContents(
500+
of: node.elseBody?.as(CodeBlockSyntax.self),
501+
contentsKeyPath: \.statements,
502+
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
503+
)
500504

501505
return .visitChildren
502506
}
@@ -535,7 +539,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
535539
after(typeAnnotation.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
536540
}
537541

538-
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
542+
arrangeBracesAndContents(
543+
of: node.body,
544+
contentsKeyPath: \.statements,
545+
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
546+
)
539547

540548
return .visitChildren
541549
}
@@ -557,14 +565,22 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
557565
after(condition.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
558566
}
559567

560-
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
568+
arrangeBracesAndContents(
569+
of: node.body,
570+
contentsKeyPath: \.statements,
571+
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
572+
)
561573

562574
return .visitChildren
563575
}
564576

565577
override func visit(_ node: RepeatWhileStmtSyntax) -> SyntaxVisitorContinueKind {
566578
after(node.labelColon, tokens: .space)
567-
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
579+
arrangeBracesAndContents(
580+
of: node.body,
581+
contentsKeyPath: \.statements,
582+
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
583+
)
568584

569585
if config.lineBreakBeforeControlFlowKeywords {
570586
before(node.whileKeyword, tokens: .break(.same), .open)
@@ -584,7 +600,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
584600

585601
override func visit(_ node: DoStmtSyntax) -> SyntaxVisitorContinueKind {
586602
after(node.labelColon, tokens: .space)
587-
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
603+
arrangeBracesAndContents(
604+
of: node.body,
605+
contentsKeyPath: \.statements,
606+
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
607+
)
588608
return .visitChildren
589609
}
590610

@@ -607,7 +627,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
607627
}
608628
}
609629

610-
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
630+
arrangeBracesAndContents(
631+
of: node.body,
632+
contentsKeyPath: \.statements,
633+
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
634+
)
611635

612636
return .visitChildren
613637
}

0 commit comments

Comments
 (0)