Skip to content

Commit 6b5238c

Browse files
authored
Merge pull request swiftlang#157 from dabelknap/assorted-fixes
Add Arrow Expressions
2 parents 93b237a + 001ac2c commit 6b5238c

File tree

2 files changed

+20
-34
lines changed

2 files changed

+20
-34
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,6 @@ import SwiftFormatConfiguration
1414
import SwiftFormatCore
1515
import SwiftSyntax
1616

17-
private class FindChildScope: SyntaxVisitor {
18-
var found = false
19-
override func visit(_ node: CodeBlockSyntax) {
20-
found = true
21-
}
22-
override func visit(_ node: SwitchStmtSyntax) {
23-
found = true
24-
}
25-
func findChildScope(in items: CodeBlockItemListSyntax) -> Bool {
26-
for child in items {
27-
visit(child)
28-
if found { return true }
29-
}
30-
return false
31-
}
32-
}
33-
3417
private let rangeOperators: Set = ["...", "..<"]
3518

3619
private final class TokenStreamCreator: SyntaxVisitor {
@@ -412,15 +395,6 @@ private final class TokenStreamCreator: SyntaxVisitor {
412395
super.visit(node)
413396
}
414397

415-
func shouldAddOpenCloseNewlines(_ node: Syntax) -> Bool {
416-
if node is AccessorListSyntax { return true }
417-
guard let list = node as? CodeBlockItemListSyntax else {
418-
return false
419-
}
420-
if list.count > 1 { return true }
421-
return FindChildScope().findChildScope(in: list)
422-
}
423-
424398
override func visit(_ node: CodeBlockSyntax) {
425399
insertToken(.newline, betweenChildrenOf: node.statements)
426400
super.visit(node)
@@ -585,6 +559,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
585559
}
586560

587561
override func visit(_ node: ArrowExprSyntax) {
562+
before(node.throwsToken, tokens: .break)
563+
before(node.arrowToken, tokens: .break)
564+
after(node.arrowToken, tokens: .break)
588565
super.visit(node)
589566
}
590567

@@ -1400,14 +1377,6 @@ private final class TokenStreamCreator: SyntaxVisitor {
14001377
}
14011378
tokens.append(token)
14021379
}
1403-
1404-
private func shouldAddNewlineBefore(_ token: TokenSyntax?) -> Bool {
1405-
guard let token = token, let before = beforeMap[token] else { return false }
1406-
for item in before {
1407-
if case .newlines = item { return false }
1408-
}
1409-
return true
1410-
}
14111380
}
14121381

14131382
extension Syntax {

Tests/SwiftFormatPrettyPrintTests/ArrayDeclTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,21 @@ public class ArrayDeclTests: PrettyPrintTestCase {
3939

4040
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
4141
}
42+
43+
public func testArrayOfFunctions() {
44+
let input =
45+
"""
46+
let A = [(Int, Double) -> Bool]()
47+
let A = [(Int, Double) throws -> Bool]()
48+
"""
49+
50+
let expected =
51+
"""
52+
let A = [(Int, Double) -> Bool]()
53+
let A = [(Int, Double) throws -> Bool]()
54+
55+
"""
56+
57+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
58+
}
4259
}

0 commit comments

Comments
 (0)