Skip to content

Commit

Permalink
Add lineBreakBeforeControlFlowBodies configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
natestedman committed Oct 14, 2020
1 parent fee799f commit 0683ba7
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 6 deletions.
6 changes: 6 additions & 0 deletions Sources/SwiftFormatConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public struct Configuration: Codable, Equatable {
case tabWidth
case indentation
case respectsExistingLineBreaks
case lineBreakBeforeControlFlowBodies
case lineBreakBeforeControlFlowKeywords
case lineBreakBeforeEachArgument
case lineBreakBeforeFuncBodies
Expand Down Expand Up @@ -75,6 +76,8 @@ public struct Configuration: Codable, Equatable {

/// MARK: Rule-specific configuration

public var lineBreakBeforeControlFlowBodies = false

/// Determines the line-breaking behavior for control flow keywords that follow a closing brace,
/// like `else` and `catch`.
///
Expand Down Expand Up @@ -201,6 +204,8 @@ public struct Configuration: Codable, Equatable {
= try container.decodeIfPresent(Indent.self, forKey: .indentation) ?? .spaces(2)
self.respectsExistingLineBreaks
= try container.decodeIfPresent(Bool.self, forKey: .respectsExistingLineBreaks) ?? true
self.lineBreakBeforeControlFlowBodies
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeControlFlowBodies) ?? false
self.lineBreakBeforeControlFlowKeywords
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeControlFlowKeywords) ?? false
self.lineBreakBeforeEachArgument
Expand Down Expand Up @@ -242,6 +247,7 @@ public struct Configuration: Codable, Equatable {
try container.encode(tabWidth, forKey: .tabWidth)
try container.encode(indentation, forKey: .indentation)
try container.encode(respectsExistingLineBreaks, forKey: .respectsExistingLineBreaks)
try container.encode(lineBreakBeforeControlFlowBodies, forKey: .lineBreakBeforeControlFlowBodies)
try container.encode(lineBreakBeforeControlFlowKeywords, forKey: .lineBreakBeforeControlFlowKeywords)
try container.encode(lineBreakBeforeEachArgument, forKey: .lineBreakBeforeEachArgument)
try container.encode(lineBreakBeforeEachGenericRequirement, forKey: .lineBreakBeforeEachGenericRequirement)
Expand Down
36 changes: 30 additions & 6 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}
}

arrangeBracesAndContents(of: node.elseBody?.as(CodeBlockSyntax.self), contentsKeyPath: \.statements)
arrangeBracesAndContents(
of: node.elseBody?.as(CodeBlockSyntax.self),
contentsKeyPath: \.statements,
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
)

return .visitChildren
}
Expand Down Expand Up @@ -535,7 +539,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
after(typeAnnotation.lastToken, tokens: .break(.close(mustBreak: false), size: 0))
}

arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
arrangeBracesAndContents(
of: node.body,
contentsKeyPath: \.statements,
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
)

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

arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
arrangeBracesAndContents(
of: node.body,
contentsKeyPath: \.statements,
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
)

return .visitChildren
}

override func visit(_ node: RepeatWhileStmtSyntax) -> SyntaxVisitorContinueKind {
after(node.labelColon, tokens: .space)
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
arrangeBracesAndContents(
of: node.body,
contentsKeyPath: \.statements,
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
)

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

override func visit(_ node: DoStmtSyntax) -> SyntaxVisitorContinueKind {
after(node.labelColon, tokens: .space)
arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
arrangeBracesAndContents(
of: node.body,
contentsKeyPath: \.statements,
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
)
return .visitChildren
}

Expand All @@ -607,7 +627,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}
}

arrangeBracesAndContents(of: node.body, contentsKeyPath: \.statements)
arrangeBracesAndContents(
of: node.body,
contentsKeyPath: \.statements,
openBraceNewlineBehavior: config.lineBreakBeforeControlFlowBodies ? .hard : .elective
)

return .visitChildren
}
Expand Down

0 comments on commit 0683ba7

Please sign in to comment.