File tree Expand file tree Collapse file tree 3 files changed +59
-0
lines changed
Sources/SwiftFormatPrettyPrint
Tests/SwiftFormatPrettyPrintTests Expand file tree Collapse file tree 3 files changed +59
-0
lines changed Original file line number Diff line number Diff line change @@ -444,6 +444,12 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
444
444
// MARK: - Control flow statement nodes
445
445
446
446
override func visit( _ node: IfStmtSyntax ) -> SyntaxVisitorContinueKind {
447
+ // There may be a consistent breaking group around this node, see `CodeBlockItemSyntax`. This
448
+ // group is necessary so that breaks around and inside of the conditions aren't forced to break
449
+ // when the if-stmt spans multiple lines.
450
+ before ( node. conditions. firstToken, tokens: . open)
451
+ after ( node. conditions. lastToken, tokens: . close)
452
+
447
453
after ( node. labelColon, tokens: . space)
448
454
after ( node. ifKeyword, tokens: . space)
449
455
@@ -1287,6 +1293,13 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
1287
1293
appendFormatterIgnored ( node: Syntax ( node) )
1288
1294
return . skipChildren
1289
1295
}
1296
+
1297
+ // This group applies to a top-level if-stmt so that all of the bodies will have the same
1298
+ // breaking behavior.
1299
+ if let ifStmt = node. item. as ( IfStmtSyntax . self) , ifStmt. elseBody != nil {
1300
+ before ( ifStmt. firstToken, tokens: . open( . consistent) )
1301
+ after ( ifStmt. lastToken, tokens: . close)
1302
+ }
1290
1303
return . visitChildren
1291
1304
}
1292
1305
Original file line number Diff line number Diff line change @@ -515,4 +515,49 @@ final class IfStmtTests: PrettyPrintTestCase {
515
515
516
516
assertPrettyPrintEqual ( input: input, expected: expected, linelength: 50 )
517
517
}
518
+
519
+ func testMultipleIfStmts( ) {
520
+ let input =
521
+ """
522
+ if foo && bar { baz() } else if bar { baz() } else if foo { baz() } else { blargh() }
523
+ if foo && bar && quxxe { baz() } else if bar { baz() } else if foo { baz() } else if quxxe { baz() } else { blargh() }
524
+ if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz { foo() } else { bar() }
525
+ if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz && someOtherCondition { foo() } else { bar() }
526
+ if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz && someOtherCondition { foo() }
527
+ """
528
+
529
+ let expected =
530
+ """
531
+ if foo && bar { baz() } else if bar { baz() } else if foo { baz() } else { blargh() }
532
+ if foo && bar && quxxe {
533
+ baz()
534
+ } else if bar {
535
+ baz()
536
+ } else if foo {
537
+ baz()
538
+ } else if quxxe {
539
+ baz()
540
+ } else {
541
+ blargh()
542
+ }
543
+ if let foo = getmyfoo(), let bar = getmybar(), foo.baz && bar.baz {
544
+ foo()
545
+ } else {
546
+ bar()
547
+ }
548
+ if let foo = getmyfoo(), let bar = getmybar(),
549
+ foo.baz && bar.baz && someOtherCondition
550
+ {
551
+ foo()
552
+ } else {
553
+ bar()
554
+ }
555
+ if let foo = getmyfoo(), let bar = getmybar(),
556
+ foo.baz && bar.baz && someOtherCondition
557
+ { foo() }
558
+
559
+ """
560
+
561
+ assertPrettyPrintEqual ( input: input, expected: expected, linelength: 85 )
562
+ }
518
563
}
Original file line number Diff line number Diff line change @@ -410,6 +410,7 @@ extension IfStmtTests {
410
410
( " testIfStatement " , testIfStatement) ,
411
411
( " testLabeledIfStmt " , testLabeledIfStmt) ,
412
412
( " testMatchingPatternConditions " , testMatchingPatternConditions) ,
413
+ ( " testMultipleIfStmts " , testMultipleIfStmts) ,
413
414
( " testOptionalBindingConditions " , testOptionalBindingConditions) ,
414
415
( " testParenthesizedClauses " , testParenthesizedClauses) ,
415
416
]
You can’t perform that action at this time.
0 commit comments