File tree Expand file tree Collapse file tree 2 files changed +37
-4
lines changed
Sources/SwiftFormatPrettyPrint
Tests/SwiftFormatPrettyPrintTests Expand file tree Collapse file tree 2 files changed +37
-4
lines changed Original file line number Diff line number Diff line change @@ -533,11 +533,19 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
533
533
override func visit( _ node: RepeatWhileStmtSyntax ) -> SyntaxVisitorContinueKind {
534
534
arrangeBracesAndContents ( of: node. body, contentsKeyPath: \. statements)
535
535
536
- let whilePrecedingBreak = config. lineBreakBeforeControlFlowKeywords
537
- ? Token . break ( . same) : Token . space
538
- before ( node. whileKeyword, tokens: whilePrecedingBreak)
536
+ if config. lineBreakBeforeControlFlowKeywords {
537
+ before ( node. whileKeyword, tokens: . break( . same) , . open)
538
+ after ( node. condition. lastToken, tokens: . close)
539
+ } else {
540
+ // The length of the condition needs to force the breaks around the braces of the repeat
541
+ // stmt's body, so that there's always a break before the right brace when the while &
542
+ // condition is too long to be on one line.
543
+ before ( node. whileKeyword, tokens: . space)
544
+ // The `open` token occurs after the ending tokens for the braced `body` node.
545
+ before ( node. body. rightBrace, tokens: . open)
546
+ after ( node. condition. lastToken, tokens: . close)
547
+ }
539
548
after ( node. whileKeyword, tokens: . space)
540
-
541
549
return . visitChildren
542
550
}
543
551
Original file line number Diff line number Diff line change @@ -10,6 +10,10 @@ final class RepeatStmtTests: PrettyPrintTestCase {
10
10
while x
11
11
repeat { foo() }
12
12
while longcondition
13
+ repeat { f() }
14
+ while long.condition
15
+ repeat { f() } while long.condition
16
+ repeat { f() } while long.condition.that.ison.many.lines
13
17
repeat {
14
18
let a = 123
15
19
var b = " abc "
@@ -29,6 +33,16 @@ final class RepeatStmtTests: PrettyPrintTestCase {
29
33
repeat {
30
34
foo()
31
35
} while longcondition
36
+ repeat {
37
+ f()
38
+ } while long.condition
39
+ repeat {
40
+ f()
41
+ } while long.condition
42
+ repeat {
43
+ f()
44
+ } while long.condition
45
+ .that.ison.many.lines
32
46
repeat {
33
47
let a = 123
34
48
var b = " abc "
@@ -50,6 +64,10 @@ final class RepeatStmtTests: PrettyPrintTestCase {
50
64
repeat {} while x
51
65
repeat { f() } while x
52
66
repeat { foo() } while longcondition
67
+ repeat { f() }
68
+ while long.condition
69
+ repeat { f() } while long.condition
70
+ repeat { f() } while long.condition.that.ison.many.lines
53
71
repeat {
54
72
let a = 123
55
73
var b = " abc "
@@ -68,6 +86,13 @@ final class RepeatStmtTests: PrettyPrintTestCase {
68
86
repeat { f() } while x
69
87
repeat { foo() }
70
88
while longcondition
89
+ repeat { f() }
90
+ while long.condition
91
+ repeat { f() }
92
+ while long.condition
93
+ repeat { f() }
94
+ while long.condition.that
95
+ .ison.many.lines
71
96
repeat {
72
97
let a = 123
73
98
var b = " abc "
You can’t perform that action at this time.
0 commit comments