Skip to content

Commit 57b5bb6

Browse files
authored
Merge pull request swiftlang#142 from dabelknap/fix-accessor
Fix accessor wrapping containing only statements
2 parents f6d3649 + b58512c commit 57b5bb6

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -406,11 +406,21 @@ private final class TokenStreamCreator: SyntaxVisitor {
406406
super.visit(node)
407407
}
408408

409+
override func visit(_ node: CodeBlockItemListSyntax) {
410+
if node.parent is AccessorBlockSyntax, node.count > 0 {
411+
for i in 0..<(node.count - 1) {
412+
after(node[i].lastToken, tokens: .newline)
413+
}
414+
}
415+
super.visit(node)
416+
}
417+
409418
override func visit(_ node: CodeBlockItemSyntax) {
410419
before(node.firstToken, tokens: .open)
411420
if !(node.parent?.parent is CodeBlockSyntax ||
412421
node.parent?.parent is SwitchCaseSyntax ||
413-
node.parent?.parent is ClosureExprSyntax
422+
node.parent?.parent is ClosureExprSyntax ||
423+
node.parent?.parent is AccessorBlockSyntax
414424
) {
415425
after(node.lastToken, tokens: .close, .newline)
416426
} else {
@@ -694,7 +704,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
694704

695705
override func visit(_ node: ReturnStmtSyntax) {
696706
before(node.firstToken, tokens: .open)
697-
after(node.returnKeyword, tokens: .break)
707+
before(node.expression?.firstToken, tokens: .break(offset: 2))
698708
after(node.lastToken, tokens: .close)
699709
super.visit(node)
700710
}

Tests/SwiftFormatPrettyPrintTests/AccessorTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ public class AccessorTests: PrettyPrintTestCase {
3030
}
3131
}
3232
}
33+
struct MyStruct {
34+
var memberValue: Int
35+
var SomeValue: Int {
36+
return 123
37+
}
38+
var AnotherValue: Double {
39+
let out = 1.23
40+
return out
41+
}
42+
}
3343
"""
3444

3545
let expected =
@@ -63,6 +73,14 @@ public class AccessorTests: PrettyPrintTestCase {
6373
}
6474
}
6575
}
76+
struct MyStruct {
77+
var memberValue: Int
78+
var SomeValue: Int { return 123 }
79+
var AnotherValue: Double {
80+
let out = 1.23
81+
return out
82+
}
83+
}
6684
6785
"""
6886

0 commit comments

Comments
 (0)