Skip to content

Commit 74c8bbe

Browse files
authored
Merge pull request swiftlang#150 from google/ternary-operator
Wrap ternary expressions.
2 parents 67e8257 + 372df84 commit 74c8bbe

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

Diff for: Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

+6
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,12 @@ private final class TokenStreamCreator: SyntaxVisitor {
765765
}
766766

767767
override func visit(_ node: TernaryExprSyntax) {
768+
before(node.conditionExpression.firstToken, tokens: .open(.inconsistent, 2))
769+
before(node.questionMark, tokens: .break)
770+
after(node.questionMark, tokens: .space)
771+
before(node.colonMark, tokens: .break)
772+
after(node.colonMark, tokens: .space)
773+
after(node.secondChoice.lastToken, tokens: .close)
768774
super.visit(node)
769775
}
770776

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class TernaryExprTests: PrettyPrintTestCase {
2+
public func testTernaryExprs() {
3+
let input =
4+
"""
5+
let x = a ? b : c
6+
let y = a ?b:c
7+
let z = a
8+
? b
9+
: c
10+
let reallyLongName = a ? longTruePart : longFalsePart
11+
let reallyLongName = reallyLongCondition ? reallyLongTruePart : reallyLongFalsePart
12+
let reallyLongName = reallyLongCondition ? reallyReallyReallyLongTruePart : reallyLongFalsePart
13+
"""
14+
15+
let expected =
16+
"""
17+
let x = a ? b : c
18+
let y = a ? b : c
19+
let z = a ? b : c
20+
let reallyLongName =
21+
a ? longTruePart : longFalsePart
22+
let reallyLongName =
23+
reallyLongCondition ? reallyLongTruePart
24+
: reallyLongFalsePart
25+
let reallyLongName =
26+
reallyLongCondition
27+
? reallyReallyReallyLongTruePart
28+
: reallyLongFalsePart
29+
30+
"""
31+
32+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
33+
}
34+
}

0 commit comments

Comments
 (0)