Skip to content

Commit 20162cd

Browse files
dylansturgallevato
authored andcommitted
Rearrange breaks so class decls won't overflow line length.
The inheritance list for type and extension decls is wrapped with open/close tokens, but the relevant open-break token was nested inside of a different group. The previous token layout created the correct indentation of the tokens in the inheritance list, but meant the break between the colon and first token in the inheritance list could never fire because it was followed by a close token. This resulted in type and extension decls that overflow the column limit when the type name + `:` + first token in the inhertance list together was longer than the column limit. I fixed this by moving the open break inside of the open/close tokens surrounding the inheritance list. Normally, the open break comes before the open but that would force a break before the first token in the inheritance list if the entire list doesn't fit. That behavior wouldn't be consistent with existing behavior. Instead, placing the open break inside of the open only breaks if the first token in the inheritance list is too long.
1 parent ed5f980 commit 20162cd

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)