Skip to content
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

Insert white space before trailing closure of a macro expression #544

Merged
merged 2 commits into from
Jun 20, 2023
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
16 changes: 14 additions & 2 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1296,11 +1296,23 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: MacroExpansionExprSyntax) -> SyntaxVisitorContinueKind {
let arguments = node.argumentList

// If there is a trailing closure, force the right parenthesis down to the next line so it
// stays with the open curly brace.
let breakBeforeRightParen =
(node.trailingClosure != nil && !isCompactSingleFunctionCallArgument(arguments))
|| mustBreakBeforeClosingDelimiter(of: node, argumentListPath: \.argumentList)

before(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before this, we need to pick up the same logic we're doing here: https://github.com/apple/swift-format/blob/8793d965ecd830bae26d86c6cddca607a3a7fa93/Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift#L981-L985

And then pass that value into forcesBreakBeforeRightDelimiter below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback! I have pushed an update addressing the suggestion and adding another test case similar to the corresponding one in FunctionCallTests.swift.

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

arrangeFunctionCallArgumentList(
node.argumentList,
arguments,
leftDelimiter: node.leftParen,
rightDelimiter: node.rightParen,
forcesBreakBeforeRightDelimiter: false)
forcesBreakBeforeRightDelimiter: breakBeforeRightParen)
return .visitChildren
}

Expand Down
117 changes: 117 additions & 0 deletions Tests/SwiftFormatPrettyPrintTests/MacroCallTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import SwiftFormatConfiguration

final class MacroCallTests: PrettyPrintTestCase {
func testNoWhiteSpaceAfterMacroWithoutTrailingClosure() {
let input =
"""
func myFunction() {
print("Currently running \\(#function)")
}

"""

let expected =
"""
func myFunction() {
print("Currently running \\(#function)")
}

"""

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

func testKeepWhiteSpaceBeforeTrailingClosure() {
let input =
"""
#Preview {}
#Preview("MyPreview") {
MyView()
}
let p = #Predicate<Int> { $0 == 0 }
"""

let expected =
"""
#Preview {}
#Preview("MyPreview") {
MyView()
}
let p = #Predicate<Int> { $0 == 0 }

"""

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

func testInsertWhiteSpaceBeforeTrailingClosure() {
let input =
"""
#Preview{}
#Preview("MyPreview"){
MyView()
}
let p = #Predicate<Int>{ $0 == 0 }
"""

let expected =
"""
#Preview {}
#Preview("MyPreview") {
MyView()
}
let p = #Predicate<Int> { $0 == 0 }

"""

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

func testDiscretionaryLineBreakBeforeTrailingClosure() {
let input =
"""
#Preview("MyPreview")
{
MyView()
}
#Preview(
"MyPreview", traits: .landscapeLeft
)
{
MyView()
}
#Preview("MyPreview", traits: .landscapeLeft, .sizeThatFitsLayout)
{
MyView()
}
#Preview("MyPreview", traits: .landscapeLeft) {
MyView()
}
"""

let expected =
"""
#Preview("MyPreview") {
MyView()
}
#Preview(
"MyPreview", traits: .landscapeLeft
) {
MyView()
}
#Preview(
"MyPreview", traits: .landscapeLeft,
.sizeThatFitsLayout
) {
MyView()
}
#Preview("MyPreview", traits: .landscapeLeft)
{
MyView()
}

"""

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