Skip to content

Wrap keypath literals appropriately. #545

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
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
42 changes: 24 additions & 18 deletions Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1760,6 +1760,30 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: KeyPathExprSyntax) -> SyntaxVisitorContinueKind {
before(node.backslash, tokens: .open)
after(node.lastToken(viewMode: .sourceAccurate), tokens: .close)
return .visitChildren
}

override func visit(_ node: KeyPathComponentSyntax) -> SyntaxVisitorContinueKind {
// If this is the first component (immediately after the backslash), allow a break after the
// slash only if a typename follows it. Do not break in the middle of `\.`.
var breakBeforePeriod = true
if let keyPathComponents = node.parent?.as(KeyPathComponentListSyntax.self),
let keyPathExpr = keyPathComponents.parent?.as(KeyPathExprSyntax.self),
node == keyPathExpr.components.first, keyPathExpr.root == nil
{
breakBeforePeriod = false
}
if breakBeforePeriod {
before(node.period, tokens: .break(.continue, size: 0))
}
return .visitChildren
}

override func visit(_ node: KeyPathSubscriptComponentSyntax) -> SyntaxVisitorContinueKind {
after(node.leftBracket, tokens: .break(.open, size: 0), .open)
before(node.rightBracket, tokens: .break(.close, size: 0), .close)
return .visitChildren
}

Expand Down Expand Up @@ -1847,24 +1871,6 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
}

override func visit(_ node: InfixOperatorExprSyntax) -> SyntaxVisitorContinueKind {
// FIXME: This is a workaround/hack for https://github.com/apple/swift-syntax/issues/928. For
// keypaths like `\.?.foo`, they get represented (after folding) as an infix operator expression
// with an empty keypath, followed by the "binary operator" `.?.`, followed by other
// expressions. We can detect this and treat the whole thing as a verbatim node, which mimics
// what we do today for keypaths (i.e., nothing).
if let keyPathExpr = node.leftOperand.as(KeyPathExprSyntax.self),
keyPathExpr.components.isEmpty
{
// If there were spaces in the trailing trivia of the previous token, they would have been
// ignored (since this expression would be expected to insert its own preceding breaks).
// Preserve that whitespace verbatim for now.
if let previousToken = node.firstToken(viewMode: .sourceAccurate)?.previousToken(viewMode: .sourceAccurate) {
appendTrailingTrivia(previousToken, forced: true)
}
verbatimToken(Syntax(node), indentingBehavior: .none)
return .skipChildren
}

let binOp = node.operatorOperand
if binOp.is(ArrowExprSyntax.self) {
// `ArrowExprSyntax` nodes occur when a function type is written in an expression context;
Expand Down
78 changes: 68 additions & 10 deletions Tests/SwiftFormatPrettyPrintTests/KeyPathExprTests.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// TODO: Add more tests and figure out how we want to wrap keypaths. Right now, they just get
// printed without breaks.
final class KeyPathExprTests: PrettyPrintTestCase {
func testSimple() {
let input =
Expand Down Expand Up @@ -47,15 +45,31 @@ final class KeyPathExprTests: PrettyPrintTestCase {
let z = a.map(\.foo!.bar)
"""#

let expected =
let expected80 =
#"""
let x = \.foo?
let y = \.foo!.bar
let z = a.map(\.foo!.bar)

"""#

assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
assertPrettyPrintEqual(input: input, expected: expected80, linelength: 80)

let expected11 =
#"""
let x =
\.foo?
let y =
\.foo!
.bar
let z =
a.map(
\.foo!
.bar)

"""#

assertPrettyPrintEqual(input: input, expected: expected11, linelength: 11)
}

func testSubscript() {
Expand All @@ -80,19 +94,63 @@ final class KeyPathExprTests: PrettyPrintTestCase {
func testImplicitSelfUnwrap() {
let input =
#"""
//let x = \.?.foo
//let y = \.?.foo.bar
let x = \.?.foo
let y = \.?.foo.bar
let z = a.map(\.?.foo.bar)
"""#

let expected =
let expected80 =
#"""
//let x = \.?.foo
//let y = \.?.foo.bar
let x = \.?.foo
let y = \.?.foo.bar
let z = a.map(\.?.foo.bar)

"""#

assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
assertPrettyPrintEqual(input: input, expected: expected80, linelength: 80)

let expected11 =
#"""
let x =
\.?.foo
let y =
\.?.foo
.bar
let z =
a.map(
\.?.foo
.bar)

"""#

assertPrettyPrintEqual(input: input, expected: expected11, linelength: 11)
}

func testWrapping() {
let input =
#"""
let x = \ReallyLongType.reallyLongProperty.anotherLongProperty
let x = \.reeeeallyLongProperty.anotherLongProperty
let x = \.longProperty.a.b.c[really + long + expression].anotherLongProperty
"""#

let expected =
#"""
let x =
\ReallyLongType
.reallyLongProperty
.anotherLongProperty
let x =
\.reeeeallyLongProperty
.anotherLongProperty
let x =
\.longProperty.a.b.c[
really + long
+ expression
].anotherLongProperty

"""#

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