Skip to content

Commit

Permalink
Merge pull request swiftlang#544 from kimberninger/macro_expression_w…
Browse files Browse the repository at this point in the history
…hite_space

Insert white space before trailing closure of a macro expression
  • Loading branch information
allevato committed Jun 29, 2023
1 parent 93aade8 commit dc4c972
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 2 deletions.
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(
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)
}
}

0 comments on commit dc4c972

Please sign in to comment.