Skip to content

Commit bfb8947

Browse files
authored
Merge pull request swiftlang#105 from dabelknap/function-decl
Add wrapping for `where` clauses and generic parameters to function declarations
2 parents 278fe58 + 2750608 commit bfb8947

File tree

3 files changed

+113
-3
lines changed

3 files changed

+113
-3
lines changed

Sources/PrettyPrint/PrettyPrint.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public class PrettyPrinter {
9797
/// current line it is printing on. If a token exceeds the remaning space, we break to a new line,
9898
/// and apply the appropriate level of indentation.
9999
private func printToken(token: Token, length: Int) {
100+
assert(length >= 0, "Token lengths must be positive")
100101
switch token {
101102

102103
case .open(let breaktype, let offset):

Sources/PrettyPrint/TokenStreamCreator.swift

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
115115
override func visit(_ node: FunctionParameterSyntax) {
116116
before(node.firstToken, tokens: .open)
117117
after(node.colon, tokens: .break)
118+
before(node.secondName, tokens: .break)
118119

119120
if let trailingComma = node.trailingCommaWorkaround {
120121
after(trailingComma, tokens: .close, .break)
@@ -281,8 +282,8 @@ private final class TokenStreamCreator: SyntaxVisitor {
281282
}
282283

283284
override func visit(_ node: GenericParameterClauseSyntax) {
284-
after(node.leftAngleBracket, tokens: .open(.consistent, 2), .break(size: 0))
285-
before(node.rightAngleBracket, tokens: .break(size: 0), .close)
285+
after(node.leftAngleBracket, tokens: .break(size: 0, offset: 2), .open(.consistent, 0))
286+
before(node.rightAngleBracket, tokens: .break(size: 0, offset: -2), .close)
286287
super.visit(node)
287288
}
288289

@@ -466,8 +467,13 @@ private final class TokenStreamCreator: SyntaxVisitor {
466467
after(node.modifiers?.lastToken, tokens: .break)
467468
after(node.funcKeyword, tokens: .break)
468469

470+
before(node.genericWhereClause?.firstToken, tokens: .break, .open(.consistent, 0))
471+
after(node.genericWhereClause?.lastToken, tokens: .break, .close)
472+
469473
if let body = node.body {
470-
before(body.leftBrace, tokens: .break)
474+
if node.genericWhereClause == nil {
475+
before(body.leftBrace, tokens: .break)
476+
}
471477
after(body.leftBrace, tokens: .newline(offset: 2), .open(.consistent, 0))
472478
before(body.rightBrace, tokens: .newline(offset: -2), .close)
473479
}
@@ -657,7 +663,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
657663
}
658664

659665
override func visit(_ node: GenericParameterSyntax) {
666+
before(node.firstToken, tokens: .open)
660667
after(node.colon, tokens: .break)
668+
after(node.lastToken, tokens: .close)
661669
super.visit(node)
662670
}
663671

@@ -723,7 +731,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
723731
}
724732

725733
override func visit(_ node: GenericWhereClauseSyntax) {
734+
before(node.whereKeyword, tokens: .open(.consistent, 2))
726735
after(node.whereKeyword, tokens: .break)
736+
after(node.lastToken, tokens: .close)
727737
super.visit(node)
728738
}
729739

@@ -744,6 +754,10 @@ private final class TokenStreamCreator: SyntaxVisitor {
744754
}
745755

746756
override func visit(_ node: SameTypeRequirementSyntax) {
757+
before(node.firstToken, tokens: .open)
758+
before(node.equalityToken, tokens: .break)
759+
after(node.equalityToken, tokens: .break)
760+
after(node.lastToken, tokens: .close)
747761
super.visit(node)
748762
}
749763

@@ -800,6 +814,9 @@ private final class TokenStreamCreator: SyntaxVisitor {
800814
}
801815

802816
override func visit(_ node: ConformanceRequirementSyntax) {
817+
before(node.firstToken, tokens: .open)
818+
after(node.colon, tokens: .break)
819+
after(node.lastToken, tokens: .close)
803820
super.visit(node)
804821
}
805822

Tests/PrettyPrinterTests/FunctionDeclTests.swift

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public class FunctionDeclTests: PrettyPrintTestCase {
6767
func myFun<S, T>(var1: S, var2: T) {
6868
print("Hello World")
6969
}
70+
71+
func longerNameFun<ReallyLongTypeName: Conform, TypeName>(var1: ReallyLongTypeNAme, var2: TypeName) {
72+
let a = 123
73+
}
7074
"""
7175

7276
let expected =
@@ -75,8 +79,96 @@ public class FunctionDeclTests: PrettyPrintTestCase {
7579
print("Hello World")
7680
}
7781
82+
func longerNameFun<
83+
ReallyLongTypeName: Conform,
84+
TypeName
85+
>(
86+
var1: ReallyLongTypeNAme,
87+
var2: TypeName
88+
) {
89+
let a = 123
90+
}
91+
7892
"""
7993

8094
assertPrettyPrintEqual(input: input, expected: expected, linelength: 40)
8195
}
96+
97+
public func testFunctionWhereClause() {
98+
let input =
99+
"""
100+
public func index<Elements: Collection, Element>(
101+
of element: Element,
102+
in collection: Elements
103+
) -> Elements.Index? where Elements.Element == Element {
104+
let a = 123
105+
let b = "abc"
106+
}
107+
108+
public func index<Elements: Collection, Element>(
109+
of element: Element,
110+
in collection: Elements
111+
) -> Elements.Index? where Elements.Element == Element, Element: Equatable {
112+
let a = 123
113+
let b = "abc"
114+
}
115+
"""
116+
117+
let expected =
118+
"""
119+
public func index<Elements: Collection, Element>(
120+
of element: Element,
121+
in collection: Elements
122+
) -> Elements.Index? where Elements.Element == Element {
123+
let a = 123
124+
let b = "abc"
125+
}
126+
127+
public func index<Elements: Collection, Element>(
128+
of element: Element,
129+
in collection: Elements
130+
) -> Elements.Index?
131+
where
132+
Elements.Element == Element,
133+
Element: Equatable
134+
{
135+
let a = 123
136+
let b = "abc"
137+
}
138+
139+
"""
140+
141+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
142+
}
143+
144+
public func testFunctionFullWrap() {
145+
let input =
146+
"""
147+
public func index<Elements: Collection, Element>(of element: Element, in collection: Elements) -> Elements.Index? where Elements.Element == Element, Element: Equatable {
148+
let a = 123
149+
let b = "abc"
150+
}
151+
"""
152+
153+
let expected =
154+
"""
155+
public func index<
156+
Elements: Collection,
157+
Element
158+
>(
159+
of element: Element,
160+
in collection: Elements
161+
) -> Elements.Index?
162+
where
163+
Elements.Element == Element,
164+
Element: Equatable
165+
{
166+
let a = 123
167+
let b = "abc"
168+
}
169+
170+
"""
171+
172+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 30)
173+
}
82174
}

0 commit comments

Comments
 (0)