Skip to content

Commit

Permalink
Add lineBreakBeforeTypeBodies configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
natestedman committed Oct 14, 2020
1 parent 2feea58 commit 95e315e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
12 changes: 12 additions & 0 deletions Sources/SwiftFormatConfiguration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public struct Configuration: Codable, Equatable {
case lineBreakBeforeFuncBodies
case lineBreakBeforeEachGenericRequirement
case lineBreakBeforeSwitchCaseOrDefaultBodies
case lineBreakBeforeTypeBodies
case prioritizeKeepingFunctionOutputTogether
case indentConditionalCompilationBlocks
case lineBreakAroundMultilineExpressionChainComponents
Expand Down Expand Up @@ -126,6 +127,14 @@ public struct Configuration: Codable, Equatable {
/// when the line length would be exceeded.
public var lineBreakBeforeSwitchCaseOrDefaultBodies = false

/// Determines the line-breaking behavior for the bodies of types: `class`, `enum`, `extension`,
/// `protocol`, and `struct`.
///
/// If true, a line break will be added after the opening brace for all non-empty types. If false
/// (the default), these bodies will be laid out on the same line as the type declaration, with
/// line breaks only being added when the line length would be exceeded.
public var lineBreakBeforeTypeBodies = false

/// Determines if function-like declaration outputs should be prioritized to be together with the
/// function signature right (closing) parenthesis.
///
Expand Down Expand Up @@ -222,6 +231,8 @@ public struct Configuration: Codable, Equatable {
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeFuncBodies) ?? false
self.lineBreakBeforeSwitchCaseOrDefaultBodies
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeSwitchCaseOrDefaultBodies) ?? false
self.lineBreakBeforeTypeBodies
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeTypeBodies) ?? false
self.prioritizeKeepingFunctionOutputTogether
= try container.decodeIfPresent(Bool.self, forKey: .prioritizeKeepingFunctionOutputTogether) ?? false
self.indentConditionalCompilationBlocks
Expand Down Expand Up @@ -259,6 +270,7 @@ public struct Configuration: Codable, Equatable {
try container.encode(lineBreakBeforeEachGenericRequirement, forKey: .lineBreakBeforeEachGenericRequirement)
try container.encode(lineBreakBeforeFuncBodies, forKey: .lineBreakBeforeFuncBodies)
try container.encode(lineBreakBeforeSwitchCaseOrDefaultBodies, forKey: .lineBreakBeforeSwitchCaseOrDefaultBodies)
try container.encode(lineBreakBeforeTypeBodies, forKey: .lineBreakBeforeTypeBodies)
try container.encode(prioritizeKeepingFunctionOutputTogether, forKey: .prioritizeKeepingFunctionOutputTogether)
try container.encode(indentConditionalCompilationBlocks, forKey: .indentConditionalCompilationBlocks)
try container.encode(
Expand Down
8 changes: 7 additions & 1 deletion Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
before(firstTokenAfterAttributes, tokens: .open)
after(typeKeyword, tokens: .break)

arrangeBracesAndContents(of: members, contentsKeyPath: \.members)
arrangeBracesAndContents(
of: members,
contentsKeyPath: \.members,
openBraceNewlineBehavior: !areBracesCompletelyEmpty(members, contentsKeyPath: \.members)
&& config.lineBreakBeforeTypeBodies
? .hard : .elective
)

if let genericWhereClause = genericWhereClause {
before(genericWhereClause.firstToken, tokens: .break(.same), .open)
Expand Down

0 comments on commit 95e315e

Please sign in to comment.