Skip to content

Commit fee799f

Browse files
committed
Add lineBreakBeforeFuncBodies configuration option
1 parent c62e45a commit fee799f

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Sources/SwiftFormatConfiguration/Configuration.swift

+13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public struct Configuration: Codable, Equatable {
2828
case respectsExistingLineBreaks
2929
case lineBreakBeforeControlFlowKeywords
3030
case lineBreakBeforeEachArgument
31+
case lineBreakBeforeFuncBodies
3132
case lineBreakBeforeEachGenericRequirement
3233
case lineBreakBeforeSwitchCaseOrDefaultBodies
3334
case prioritizeKeepingFunctionOutputTogether
@@ -98,6 +99,15 @@ public struct Configuration: Codable, Equatable {
9899
/// horizontally first, with line breaks only being fired when the line length would be exceeded.
99100
public var lineBreakBeforeEachGenericRequirement = false
100101

102+
/// Determins the line-breaking behavior for the bodies of functions declared with `func`, as
103+
/// well as "`func`-like" declarations: initializers, deinitializers, and subscripts.
104+
///
105+
/// If true, a line break will be added after the opening brace of function bodies, forcing the
106+
/// body to be on a separate line from the function. If false (the default), these bodies will be
107+
/// laid out on the same line as the declaration, with line breaks only being added when the line
108+
/// length would be exceeded.
109+
public var lineBreakBeforeFuncBodies = false
110+
101111
/// Determines the line-breaking behavior for the bodies of `case` and `default` items within
102112
/// a `switch` statement.
103113
///
@@ -197,6 +207,8 @@ public struct Configuration: Codable, Equatable {
197207
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachArgument) ?? false
198208
self.lineBreakBeforeEachGenericRequirement
199209
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeEachGenericRequirement) ?? false
210+
self.lineBreakBeforeFuncBodies
211+
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeFuncBodies) ?? false
200212
self.lineBreakBeforeSwitchCaseOrDefaultBodies
201213
= try container.decodeIfPresent(Bool.self, forKey: .lineBreakBeforeSwitchCaseOrDefaultBodies) ?? false
202214
self.prioritizeKeepingFunctionOutputTogether
@@ -233,6 +245,7 @@ public struct Configuration: Codable, Equatable {
233245
try container.encode(lineBreakBeforeControlFlowKeywords, forKey: .lineBreakBeforeControlFlowKeywords)
234246
try container.encode(lineBreakBeforeEachArgument, forKey: .lineBreakBeforeEachArgument)
235247
try container.encode(lineBreakBeforeEachGenericRequirement, forKey: .lineBreakBeforeEachGenericRequirement)
248+
try container.encode(lineBreakBeforeFuncBodies, forKey: .lineBreakBeforeFuncBodies)
236249
try container.encode(lineBreakBeforeSwitchCaseOrDefaultBodies, forKey: .lineBreakBeforeSwitchCaseOrDefaultBodies)
237250
try container.encode(prioritizeKeepingFunctionOutputTogether, forKey: .prioritizeKeepingFunctionOutputTogether)
238251
try container.encode(indentConditionalCompilationBlocks, forKey: .indentConditionalCompilationBlocks)

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+5-1
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
416416
before(node.firstToken, tokens: .open)
417417

418418
arrangeAttributeList(attributes)
419-
arrangeBracesAndContents(of: body, contentsKeyPath: bodyContentsKeyPath)
419+
arrangeBracesAndContents(
420+
of: body,
421+
contentsKeyPath: bodyContentsKeyPath,
422+
openBraceNewlineBehavior: config.lineBreakBeforeFuncBodies ? .hard : .elective
423+
)
420424

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

0 commit comments

Comments
 (0)