Skip to content

Commit

Permalink
Add lineBreakBeforeEachSwitchCaseOrDefaultBody configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
natestedman committed Oct 14, 2020
1 parent 1208917 commit 6c22572
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
13 changes: 13 additions & 0 deletions Sources/SwiftFormatConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public struct Configuration: Codable, Equatable {
case lineBreakBeforeControlFlowKeywords
case lineBreakBeforeEachArgument
case lineBreakBeforeEachGenericRequirement
case lineBreakBeforeEachSwitchCaseOrDefaultBody
case prioritizeKeepingFunctionOutputTogether
case indentConditionalCompilationBlocks
case lineBreakAroundMultilineExpressionChainComponents
Expand Down Expand Up @@ -97,6 +98,15 @@ public struct Configuration: Codable, Equatable {
/// horizontally first, with line breaks only being fired when the line length would be exceeded.
public var lineBreakBeforeEachGenericRequirement = false

/// Determines the line-breaking behavior for the bodies of `case` and `default` items within
/// a `switch` statement.
///
/// If true, a line break will be added after the colon following `case` or `default`, forcing the
/// body to be on a separate line from the `case` or `default`. If false (the default), these bodies
/// will be laid out on the same line as the `case` or `default`, with line breaks only being added
/// when the line length would be exceeded.
public var lineBreakBeforeEachSwitchCaseOrDefaultBody = false

/// Determines if function-like declaration outputs should be prioritized to be together with the
/// function signature right (closing) parenthesis.
///
Expand Down Expand Up @@ -187,6 +197,8 @@ public struct Configuration: Codable, Equatable {
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachArgument) ?? false
self.lineBreakBeforeEachGenericRequirement
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachGenericRequirement) ?? false
self.lineBreakBeforeEachSwitchCaseOrDefaultBody
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachSwitchCaseOrDefaultBody) ?? false
self.prioritizeKeepingFunctionOutputTogether
= try container.decodeIfPresent(Bool.self, forKey: .prioritizeKeepingFunctionOutputTogether) ?? false
self.indentConditionalCompilationBlocks
Expand Down Expand Up @@ -221,6 +233,7 @@ public struct Configuration: Codable, Equatable {
try container.encode(lineBreakBeforeControlFlowKeywords, forKey: .lineBreakBeforeControlFlowKeywords)
try container.encode(lineBreakBeforeEachArgument, forKey: .lineBreakBeforeEachArgument)
try container.encode(lineBreakBeforeEachGenericRequirement, forKey: .lineBreakBeforeEachGenericRequirement)
try container.encode(lineBreakBeforeEachSwitchCaseOrDefaultBody, forKey: .lineBreakBeforeEachSwitchCaseOrDefaultBody)
try container.encode(prioritizeKeepingFunctionOutputTogether, forKey: .prioritizeKeepingFunctionOutputTogether)
try container.encode(indentConditionalCompilationBlocks, forKey: .indentConditionalCompilationBlocks)
try container.encode(
Expand Down
7 changes: 6 additions & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,12 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
before(node.firstToken, tokens: openBreak)

after(node.unknownAttr?.lastToken, tokens: .space)
after(node.label.lastToken, tokens: .break(.reset, size: 0), .break(.open), .open)
after(
node.label.lastToken,
tokens: .break(.reset, size: 0),
.break(.open, newlines: config.lineBreakBeforeEachSwitchCaseOrDefaultBody ? .hard : .elective),
.open
)

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

0 comments on commit 6c22572

Please sign in to comment.