Skip to content

Commit 6c22572

Browse files
committed
Add lineBreakBeforeEachSwitchCaseOrDefaultBody configuration option
1 parent 1208917 commit 6c22572

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sources/SwiftFormatConfiguration/Configuration.swift

+13
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public struct Configuration: Codable, Equatable {
2929
case lineBreakBeforeControlFlowKeywords
3030
case lineBreakBeforeEachArgument
3131
case lineBreakBeforeEachGenericRequirement
32+
case lineBreakBeforeEachSwitchCaseOrDefaultBody
3233
case prioritizeKeepingFunctionOutputTogether
3334
case indentConditionalCompilationBlocks
3435
case lineBreakAroundMultilineExpressionChainComponents
@@ -97,6 +98,15 @@ public struct Configuration: Codable, Equatable {
9798
/// horizontally first, with line breaks only being fired when the line length would be exceeded.
9899
public var lineBreakBeforeEachGenericRequirement = false
99100

101+
/// Determines the line-breaking behavior for the bodies of `case` and `default` items within
102+
/// a `switch` statement.
103+
///
104+
/// If true, a line break will be added after the colon following `case` or `default`, forcing the
105+
/// body to be on a separate line from the `case` or `default`. If false (the default), these bodies
106+
/// will be laid out on the same line as the `case` or `default`, with line breaks only being added
107+
/// when the line length would be exceeded.
108+
public var lineBreakBeforeEachSwitchCaseOrDefaultBody = false
109+
100110
/// Determines if function-like declaration outputs should be prioritized to be together with the
101111
/// function signature right (closing) parenthesis.
102112
///
@@ -187,6 +197,8 @@ public struct Configuration: Codable, Equatable {
187197
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachArgument) ?? false
188198
self.lineBreakBeforeEachGenericRequirement
189199
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachGenericRequirement) ?? false
200+
self.lineBreakBeforeEachSwitchCaseOrDefaultBody
201+
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachSwitchCaseOrDefaultBody) ?? false
190202
self.prioritizeKeepingFunctionOutputTogether
191203
= try container.decodeIfPresent(Bool.self, forKey: .prioritizeKeepingFunctionOutputTogether) ?? false
192204
self.indentConditionalCompilationBlocks
@@ -221,6 +233,7 @@ public struct Configuration: Codable, Equatable {
221233
try container.encode(lineBreakBeforeControlFlowKeywords, forKey: .lineBreakBeforeControlFlowKeywords)
222234
try container.encode(lineBreakBeforeEachArgument, forKey: .lineBreakBeforeEachArgument)
223235
try container.encode(lineBreakBeforeEachGenericRequirement, forKey: .lineBreakBeforeEachGenericRequirement)
236+
try container.encode(lineBreakBeforeEachSwitchCaseOrDefaultBody, forKey: .lineBreakBeforeEachSwitchCaseOrDefaultBody)
224237
try container.encode(prioritizeKeepingFunctionOutputTogether, forKey: .prioritizeKeepingFunctionOutputTogether)
225238
try container.encode(indentConditionalCompilationBlocks, forKey: .indentConditionalCompilationBlocks)
226239
try container.encode(

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+6-1
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,12 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
659659
before(node.firstToken, tokens: openBreak)
660660

661661
after(node.unknownAttr?.lastToken, tokens: .space)
662-
after(node.label.lastToken, tokens: .break(.reset, size: 0), .break(.open), .open)
662+
after(
663+
node.label.lastToken,
664+
tokens: .break(.reset, size: 0),
665+
.break(.open, newlines: config.lineBreakBeforeEachSwitchCaseOrDefaultBody ? .hard : .elective),
666+
.open
667+
)
663668

664669
// If switch/case labels were configured to be indented, insert an extra `close` break after the
665670
// case body to match the `open` break above

0 commit comments

Comments
 (0)