Skip to content

Disallow discretionary newlines before trailing closures. #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
(node.trailingClosure != nil && !isCompactSingleFunctionCallArgument(arguments))
|| mustBreakBeforeClosingDelimiter(of: node, argumentListPath: \.argumentList)

before(node.trailingClosure?.leftBrace, tokens: .break(.same))
before(
node.trailingClosure?.leftBrace,
tokens: .break(.same, newlines: .elective(ignoresDiscretionary: true)))

arrangeFunctionCallArgumentList(
arguments,
Expand Down Expand Up @@ -1048,7 +1050,9 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
node.trailingClosure != nil
|| mustBreakBeforeClosingDelimiter(of: node, argumentListPath: \.argumentList)

before(node.trailingClosure?.leftBrace, tokens: .space)
before(
node.trailingClosure?.leftBrace,
tokens: .break(.same, newlines: .elective(ignoresDiscretionary: true)))

arrangeFunctionCallArgumentList(
arguments,
Expand Down
49 changes: 49 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/FunctionCallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,55 @@ final class FunctionCallTests: PrettyPrintTestCase {
assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
}

func testDiscretionaryLineBreakBeforeTrailingClosure() {
let input =
"""
foo(a, b, c)
{
blah()
}
foo(
a, b, c
)
{
blah()
}
foo(arg1, arg2, arg3, arg4, arg5, arg6, arg7)
{
blah()
}
foo(ab, arg1, arg2) {
blah()
}
"""

let expected =
"""
foo(a, b, c) {
blah()
}
foo(
a, b, c
) {
blah()
}
foo(
arg1, arg2, arg3,
arg4, arg5, arg6,
arg7
) {
blah()
}
foo(ab, arg1, arg2)
{
blah()
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
}

func testGroupsTrailingComma() {
let input =
"""
Expand Down
49 changes: 49 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/SubscriptExprTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,53 @@ final class SubscriptExprTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected, linelength: 70)
}

func testDiscretionaryLineBreakBeforeTrailingClosure() {
let input =
"""
foo[a, b, c]
{
blah()
}
foo[
a, b, c
]
{
blah()
}
foo[arg1, arg2, arg3, arg4, arg5, arg6, arg7]
{
blah()
}
foo[ab, arg1, arg2] {
blah()
}
"""

let expected =
"""
foo[a, b, c] {
blah()
}
foo[
a, b, c
] {
blah()
}
foo[
arg1, arg2, arg3,
arg4, arg5, arg6,
arg7
] {
blah()
}
foo[ab, arg1, arg2]
{
blah()
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 20)
}
}
2 changes: 2 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ extension FunctionCallTests {
("testBasicFunctionCalls_packArguments", testBasicFunctionCalls_packArguments),
("testDiscretionaryLineBreakAfterColon", testDiscretionaryLineBreakAfterColon),
("testDiscretionaryLineBreakBeforeClosingParenthesis", testDiscretionaryLineBreakBeforeClosingParenthesis),
("testDiscretionaryLineBreakBeforeTrailingClosure", testDiscretionaryLineBreakBeforeTrailingClosure),
("testDiscretionaryLineBreaksAreSelfCorrecting", testDiscretionaryLineBreaksAreSelfCorrecting),
("testGroupsTrailingComma", testGroupsTrailingComma),
("testNestedFunctionCallExprSequences", testNestedFunctionCallExprSequences),
Expand Down Expand Up @@ -697,6 +698,7 @@ extension SubscriptExprTests {
static let __allTests__SubscriptExprTests = [
("testBasicSubscriptGetters", testBasicSubscriptGetters),
("testBasicSubscriptSetters", testBasicSubscriptSetters),
("testDiscretionaryLineBreakBeforeTrailingClosure", testDiscretionaryLineBreakBeforeTrailingClosure),
("testGroupsTrailingComma", testGroupsTrailingComma),
("testSubscriptGettersWithTrailingClosures", testSubscriptGettersWithTrailingClosures),
("testSubscriptSettersWithTrailingClosures", testSubscriptSettersWithTrailingClosures),
Expand Down