Skip to content

Commit

Permalink
Merge pull request #204 from MPLew-is/indented-case
Browse files Browse the repository at this point in the history
Allow indenting case statements relative to the containing switch block
  • Loading branch information
allevato committed Jun 18, 2020
2 parents f4367f0 + 6a91b91 commit 2107603
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 3 deletions.
25 changes: 25 additions & 0 deletions Sources/SwiftFormatConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public struct Configuration: Codable, Equatable {
case indentConditionalCompilationBlocks
case lineBreakAroundMultilineExpressionChainComponents
case fileScopedDeclarationPrivacy
case indentSwitchCaseLabels
case rules
}

Expand Down Expand Up @@ -120,6 +121,27 @@ public struct Configuration: Codable, Equatable {
/// declarations whose effective access level is private to the containing file.
public var fileScopedDeclarationPrivacy = FileScopedDeclarationPrivacyConfiguration()

/// Determines if `case` statements should be indented compared to the containing `switch` block.
///
/// When `false`, the correct form is:
/// ```swift
/// switch someValue {
/// case someCase:
/// someStatement
/// ...
/// }
/// ```
///
/// When `true`, the correct form is:
/// ```swift
/// switch someValue {
/// case someCase:
/// someStatement
/// ...
/// }
///```
public var indentSwitchCaseLabels = false

/// Constructs a Configuration with all default values.
public init() {
self.version = highestSupportedConfigurationVersion
Expand Down Expand Up @@ -176,6 +198,8 @@ public struct Configuration: Codable, Equatable {
try container.decodeIfPresent(
FileScopedDeclarationPrivacyConfiguration.self, forKey: .fileScopedDeclarationPrivacy)
?? FileScopedDeclarationPrivacyConfiguration()
self.indentSwitchCaseLabels
= try container.decodeIfPresent(Bool.self, forKey: .indentSwitchCaseLabels) ?? false

// If the `rules` key is not present at all, default it to the built-in set
// so that the behavior is the same as if the configuration had been
Expand Down Expand Up @@ -203,6 +227,7 @@ public struct Configuration: Codable, Equatable {
lineBreakAroundMultilineExpressionChainComponents,
forKey: .lineBreakAroundMultilineExpressionChainComponents)
try container.encode(fileScopedDeclarationPrivacy, forKey: .fileScopedDeclarationPrivacy)
try container.encode(indentSwitchCaseLabels, forKey: .indentSwitchCaseLabels)
try container.encode(rules, forKey: .rules)
}

Expand Down
23 changes: 20 additions & 3 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -644,10 +644,27 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: SwitchCaseSyntax) -> SyntaxVisitorContinueKind {
before(node.firstToken, tokens: .break(.same, newlines: .soft))
// If switch/case labels were configured to be indented, use an `open` break; otherwise, use
// the default `same` break.
let openBreak: Token
if config.indentSwitchCaseLabels {
openBreak = .break(.open, newlines: .elective)
} else {
openBreak = .break(.same, newlines: .soft)
}
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.lastToken, tokens: .break(.close, size: 0), .close)

// If switch/case labels were configured to be indented, insert an extra `close` break after the
// case body to match the `open` break above
var afterLastTokenTokens: [Token] = [.break(.close, size: 0), .close]
if config.indentSwitchCaseLabels {
afterLastTokenTokens.append(.break(.close, size: 0))
}
after(node.lastToken, tokens: afterLastTokenTokens)

return .visitChildren
}

Expand Down Expand Up @@ -2028,7 +2045,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}
return .visitChildren
}

override func visit(_ node: SameTypeRequirementSyntax) -> SyntaxVisitorContinueKind {
before(node.equalityToken, tokens: .break)
after(node.equalityToken, tokens: .space)
Expand Down

0 comments on commit 2107603

Please sign in to comment.