Skip to content

Commit e6a95c3

Browse files
authored
Merge pull request swiftlang#181 from dylansturg/class_is_too_long
Rearrange breaks so class decls won't overflow line length.
2 parents 684d3a9 + 67d63cc commit e6a95c3

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,10 +1991,11 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
19911991
}
19921992

19931993
override func visit(_ node: TypeInheritanceClauseSyntax) -> SyntaxVisitorContinueKind {
1994-
after(node.colon, tokens: .break(.open, size: 1))
1995-
before(node.inheritedTypeCollection.firstToken, tokens: .open)
1996-
after(node.inheritedTypeCollection.lastToken, tokens: .close)
1997-
after(node.lastToken, tokens: .break(.close, size: 0))
1994+
// Normally, the open-break is placed before the open token. In this case, it's intentionally
1995+
// ordered differently so that the inheritance list can start on the current line and only
1996+
// breaks if the first item in the list would overflow the column limit.
1997+
before(node.inheritedTypeCollection.firstToken, tokens: .open, .break(.open, size: 1))
1998+
after(node.inheritedTypeCollection.lastToken, tokens: .break(.close, size: 0), .close)
19981999
return .visitChildren
19992000
}
20002001

Tests/SwiftFormatPrettyPrintTests/ClassDeclTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,15 @@ final class ClassDeclTests: PrettyPrintTestCase {
140140
let A: Int
141141
let B: Bool
142142
}
143+
class MyClass:
144+
SuperOne, SuperTwo, SuperThree {
145+
let A: Int
146+
let B: Bool
147+
}
148+
class MyClassWhoseNameIsVeryLong: SuperOne, SuperTwo, SuperThree {
149+
let A: Int
150+
let B: Bool
151+
}
143152
"""
144153

145154
let expected =
@@ -158,6 +167,18 @@ final class ClassDeclTests: PrettyPrintTestCase {
158167
let A: Int
159168
let B: Bool
160169
}
170+
class MyClass:
171+
SuperOne, SuperTwo, SuperThree
172+
{
173+
let A: Int
174+
let B: Bool
175+
}
176+
class MyClassWhoseNameIsVeryLong:
177+
SuperOne, SuperTwo, SuperThree
178+
{
179+
let A: Int
180+
let B: Bool
181+
}
161182
162183
"""
163184

Tests/SwiftFormatPrettyPrintTests/ExtensionDeclTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ final class ExtensionDeclTests: PrettyPrintTestCase {
316316
let expected =
317317

318318
"""
319-
public extension MyContainer: MyContainerProtocolOne,
320-
MyContainerProtocolTwo,
319+
public extension MyContainer:
320+
MyContainerProtocolOne, MyContainerProtocolTwo,
321321
SomeoneElsesContainerProtocol,
322322
SomeFrameworkContainerProtocol
323323
where
@@ -346,8 +346,8 @@ final class ExtensionDeclTests: PrettyPrintTestCase {
346346
let expected =
347347

348348
"""
349-
public extension MyContainer: MyContainerProtocolOne,
350-
MyContainerProtocolTwo,
349+
public extension MyContainer:
350+
MyContainerProtocolOne, MyContainerProtocolTwo,
351351
SomeoneElsesContainerProtocol,
352352
SomeFrameworkContainerProtocol
353353
where

0 commit comments

Comments
 (0)