Skip to content

Commit

Permalink
Unwrap closure braces
Browse files Browse the repository at this point in the history
  • Loading branch information
nicklockwood committed Nov 9, 2020
1 parent 7f07e1e commit e0dd5c7
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
26 changes: 13 additions & 13 deletions Sources/Rules.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1640,21 +1640,21 @@ public struct _FormatRules {
else {
return
}
let maxWidth = formatter.options.maxWidth

var maxWidth = formatter.options.maxWidth
if maxWidth == 0 {
// Check that brace doesn't have inline content after it
guard formatter.next(.nonSpace, after: i)?.isLinebreak == true else {
return
}
} else {
// Check that unwrapping wouldn't exceed line length
let endOfLine = formatter.endOfLine(at: i)
let length = formatter.lineLength(from: i, upTo: endOfLine)
let prevLineLength = formatter.lineLength(at: prevIndex)
guard prevLineLength + length + 1 <= maxWidth else {
return
}
// Set reasonable default
maxWidth = 100
}

// Check that unwrapping wouldn't exceed line length
let endOfLine = formatter.endOfLine(at: i)
let length = formatter.lineLength(from: i, upTo: endOfLine)
let prevLineLength = formatter.lineLength(at: prevIndex)
guard prevLineLength + length + 1 <= maxWidth else {
return
}

// Avoid conflicts with wrapMultilineStatementBraces
// TODO: find a better solution for this
if let keywordIndex =
Expand Down
26 changes: 26 additions & 0 deletions Tests/RulesTests+Braces.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,32 @@ extension RulesTests {
testFormatting(for: input, rule: FormatRules.braces, options: options)
}

func testKnRUnwrapClosure() {
let input = """
let foo =
{ bar in
bar()
}
"""
let output = """
let foo = { bar in
bar()
}
"""
testFormatting(for: input, output, rule: FormatRules.braces)
}

func testKnRNoUnwrapClosureIfWidthExceeded() {
let input = """
let foo =
{ bar in
bar()
}
"""
let options = FormatOptions(maxWidth: 15)
testFormatting(for: input, rule: FormatRules.braces, options: options, exclude: ["indent"])
}

func testKnRClosingBraceWrapped() {
let input = "func foo() {\n print(bar) }"
let output = "func foo() {\n print(bar)\n}"
Expand Down
2 changes: 1 addition & 1 deletion Tests/RulesTests+Indentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1232,7 +1232,7 @@ extension RulesTests {
}
"""
let options = FormatOptions(wrapArguments: .disabled, closingParenOnSameLine: true)
testFormatting(for: input, rule: FormatRules.indent, options: options)
testFormatting(for: input, rule: FormatRules.indent, options: options, exclude: ["braces"])
}

func testSingleIndentTrailingClosureBodyOfShortMethod() {
Expand Down
2 changes: 2 additions & 0 deletions Tests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -968,6 +968,8 @@ extension RulesTests {
("testKnRNoMangleClosureReturningClosure", testKnRNoMangleClosureReturningClosure),
("testKnRNoMangleClosureReturningClosure2", testKnRNoMangleClosureReturningClosure2),
("testKnRNoMangleCommentBeforeClosure", testKnRNoMangleCommentBeforeClosure),
("testKnRNoUnwrapClosureIfWidthExceeded", testKnRNoUnwrapClosureIfWidthExceeded),
("testKnRUnwrapClosure", testKnRUnwrapClosure),
("testLabeledTupleYodaCondition", testLabeledTupleYodaCondition),
("testLabelsAreNotArguments", testLabelsAreNotArguments),
("testLeadingColonFollowedByCommentMovedToPreviousLine", testLeadingColonFollowedByCommentMovedToPreviousLine),
Expand Down

0 comments on commit e0dd5c7

Please sign in to comment.