Skip to content

Commit 500f325

Browse files
committed
Add lineBreakBeforeTypeBodies configuration option
1 parent f8f8032 commit 500f325

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

Sources/SwiftFormatConfiguration/Configuration.swift

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public struct Configuration: Codable, Equatable {
3232
case lineBreakBeforeFuncBodies
3333
case lineBreakBeforeEachGenericRequirement
3434
case lineBreakBeforeSwitchCaseOrDefaultBodies
35+
case lineBreakBeforeTypeBodies
3536
case prioritizeKeepingFunctionOutputTogether
3637
case indentConditionalCompilationBlocks
3738
case lineBreakAroundMultilineExpressionChainComponents
@@ -126,6 +127,14 @@ public struct Configuration: Codable, Equatable {
126127
/// when the line length would be exceeded.
127128
public var lineBreakBeforeSwitchCaseOrDefaultBodies = false
128129

130+
/// Determines the line-breaking behavior for the bodies of types: `class`, `enum`, `extension`,
131+
/// `protocol`, and `struct`.
132+
///
133+
/// If true, a line break will be added after the opening brace for all non-empty types. If false
134+
/// (the default), these bodies will be laid out on the same line as the type declaration, with
135+
/// line breaks only being added when the line length would be exceeded.
136+
public var lineBreakBeforeTypeBodies = false
137+
129138
/// Determines if function-like declaration outputs should be prioritized to be together with the
130139
/// function signature right (closing) parenthesis.
131140
///
@@ -222,6 +231,8 @@ public struct Configuration: Codable, Equatable {
222231
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeFuncBodies) ?? false
223232
self.lineBreakBeforeSwitchCaseOrDefaultBodies
224233
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeSwitchCaseOrDefaultBodies) ?? false
234+
self.lineBreakBeforeTypeBodies
235+
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeTypeBodies) ?? false
225236
self.prioritizeKeepingFunctionOutputTogether
226237
= try container.decodeIfPresent(Bool.self, forKey: .prioritizeKeepingFunctionOutputTogether) ?? false
227238
self.indentConditionalCompilationBlocks
@@ -259,6 +270,7 @@ public struct Configuration: Codable, Equatable {
259270
try container.encode(lineBreakBeforeEachGenericRequirement, forKey: .lineBreakBeforeEachGenericRequirement)
260271
try container.encode(lineBreakBeforeFuncBodies, forKey: .lineBreakBeforeFuncBodies)
261272
try container.encode(lineBreakBeforeSwitchCaseOrDefaultBodies, forKey: .lineBreakBeforeSwitchCaseOrDefaultBodies)
273+
try container.encode(lineBreakBeforeTypeBodies, forKey: .lineBreakBeforeTypeBodies)
262274
try container.encode(prioritizeKeepingFunctionOutputTogether, forKey: .prioritizeKeepingFunctionOutputTogether)
263275
try container.encode(indentConditionalCompilationBlocks, forKey: .indentConditionalCompilationBlocks)
264276
try container.encode(

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+7-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
246246
before(firstTokenAfterAttributes, tokens: .open)
247247
after(typeKeyword, tokens: .break)
248248

249-
arrangeBracesAndContents(of: members, contentsKeyPath: \.members)
249+
arrangeBracesAndContents(
250+
of: members,
251+
contentsKeyPath: \.members,
252+
openBraceNewlineBehavior: !areBracesCompletelyEmpty(members, contentsKeyPath: \.members)
253+
&& config.lineBreakBeforeTypeBodies
254+
? .hard : .elective
255+
)
250256

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

0 commit comments

Comments
 (0)