Skip to content

Commit 28395f9

Browse files
authored
Merge pull request swiftlang#184 from dabelknap/fix-trailing-comments
Fix an edge case for trailing comments
2 parents 8e87fda + a735961 commit 28395f9

File tree

3 files changed

+81
-19
lines changed

3 files changed

+81
-19
lines changed

Sources/SwiftFormatCore/Trivia+Convenience.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,19 @@ extension Trivia {
3535
return count
3636
}
3737

38+
public var numberOfComments: Int {
39+
var count = 0
40+
for piece in self {
41+
switch piece {
42+
case .lineComment, .docLineComment, .blockComment, .docBlockComment:
43+
count += 1
44+
default:
45+
continue
46+
}
47+
}
48+
return count
49+
}
50+
3851
public var hasSpaces: Bool {
3952
for piece in self {
4053
if case .tabs = piece { return true }

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,11 @@ private final class TokenStreamCreator: SyntaxVisitor {
2121
private var beforeMap = [TokenSyntax: [Token]]()
2222
private var afterMap = [TokenSyntax: [[Token]]]()
2323
private let config: Configuration
24+
private let maxlinelength: Int
2425

2526
init(configuration: Configuration) {
2627
self.config = configuration
28+
self.maxlinelength = config.lineLength
2729
}
2830

2931
func makeStream(from node: Syntax) -> [Token] {
@@ -516,13 +518,14 @@ private final class TokenStreamCreator: SyntaxVisitor {
516518
}
517519

518520
override func visit(_ node: CodeBlockSyntax) {
519-
insertToken(.newline, betweenChildrenOf: node.statements)
521+
insertToken(.break(size: maxlinelength), betweenChildrenOf: node.statements)
520522
super.visit(node)
521523
}
522524

523525
override func visit(_ node: CodeBlockItemListSyntax) {
524-
if node.parent is AccessorBlockSyntax || node.parent is ClosureExprSyntax || node.parent is IfConfigClauseSyntax, node.count > 0 {
525-
insertToken(.newline, betweenChildrenOf: node)
526+
if node.parent is AccessorBlockSyntax || node.parent is ClosureExprSyntax ||
527+
node.parent is IfConfigClauseSyntax, node.count > 0 {
528+
insertToken(.break(size: maxlinelength), betweenChildrenOf: node)
526529
}
527530
super.visit(node)
528531
}
@@ -535,7 +538,7 @@ private final class TokenStreamCreator: SyntaxVisitor {
535538
node.parent?.parent is AccessorBlockSyntax ||
536539
node.parent?.parent is IfConfigClauseSyntax
537540
) {
538-
after(node.lastToken, tokens: .close, .newline)
541+
after(node.lastToken, tokens: .close, .break(size: maxlinelength))
539542
} else {
540543
after(node.lastToken, tokens: .close)
541544
}
@@ -1386,16 +1389,11 @@ private final class TokenStreamCreator: SyntaxVisitor {
13861389
appendToken(.break(size: 2, offset: 0))
13871390
appendToken(.comment(commentToken, wasEndOfLine: true))
13881391

1389-
if isInContainer(token) {
1390-
appendToken(.newline)
1391-
}
1392-
}
1393-
1394-
private func isInContainer(_ token: TokenSyntax) -> Bool {
1395-
if token.parent is ArrayElementSyntax || token.parent is DictionaryElementSyntax || token.parent is TupleElementSyntax {
1396-
return true
1392+
if nextToken != nil, ["}", ")"].contains(nextToken?.withoutTrivia().text), trivia.numberOfComments == 1 {
1393+
appendToken(.break(size: maxlinelength, offset: -2))
1394+
} else {
1395+
appendToken(.break(size: maxlinelength))
13971396
}
1398-
return false
13991397
}
14001398

14011399
private func extractLeadingTrivia(_ token: TokenSyntax) {
@@ -1411,16 +1409,16 @@ private final class TokenStreamCreator: SyntaxVisitor {
14111409
(["{", "in"].contains { $0 == previousToken.withoutTrivia().text }) {
14121410
// do nothing
14131411
} else {
1414-
appendToken(.newline)
1412+
appendToken(.break(size: maxlinelength))
14151413
}
14161414
}
14171415

14181416
appendToken(.comment(Comment(kind: .line, text: text), wasEndOfLine: false))
14191417

14201418
if token.withoutTrivia().text == "}" {
1421-
appendToken(.newline(offset: -2))
1419+
appendToken(.break(size: maxlinelength, offset: -2))
14221420
} else {
1423-
appendToken(.newline)
1421+
appendToken(.break(size: maxlinelength))
14241422
}
14251423
}
14261424

@@ -1431,16 +1429,16 @@ private final class TokenStreamCreator: SyntaxVisitor {
14311429
previousToken.withoutTrivia().text == "{" {
14321430
// do nothing
14331431
} else {
1434-
appendToken(.newline)
1432+
appendToken(.break(size: maxlinelength))
14351433
}
14361434
}
14371435

14381436
appendToken(.comment(Comment(kind: .block, text: text), wasEndOfLine: false))
14391437

14401438
if token.withoutTrivia().text == "}" {
1441-
appendToken(.newline(offset: -2))
1439+
appendToken(.break(size: maxlinelength, offset: -2))
14421440
} else {
1443-
appendToken(.newline)
1441+
appendToken(.break(size: maxlinelength))
14441442
}
14451443
}
14461444

Tests/SwiftFormatPrettyPrintTests/CommentTests.swift

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,22 @@ public class CommentTests: PrettyPrintTestCase {
109109
// Comment 5
110110
}
111111
112+
let a = myfun(var1: 123 // Cmt 7
113+
)
114+
115+
guard condition else { return // Cmt 6
116+
}
117+
118+
switch myvar {
119+
case .one, .two, // three
120+
.four:
121+
dostuff()
122+
default: ()
123+
}
124+
125+
let a = 123 + // comment
126+
b + c
127+
112128
let d = 123
113129
// Trailing Comment
114130
"""
@@ -143,6 +159,27 @@ public class CommentTests: PrettyPrintTestCase {
143159
// Comment 5
144160
}
145161
162+
let a = myfun(
163+
var1: 123 // Cmt 7
164+
)
165+
166+
guard condition else {
167+
return // Cmt 6
168+
}
169+
170+
switch myvar {
171+
case .one,
172+
.two, // three
173+
.four:
174+
dostuff()
175+
default:
176+
()
177+
}
178+
179+
let a =
180+
123 + // comment
181+
b + c
182+
146183
let d = 123
147184
// Trailing Comment
148185
@@ -299,6 +336,12 @@ public class CommentTests: PrettyPrintTestCase {
299336
300337
let reallyLongVariableName = 123 /* This comment should not wrap */
301338
339+
let a = myfun(var1: 123 /* Cmt 5 */
340+
)
341+
342+
guard condition else { return /* Cmt 6 */
343+
}
344+
302345
let d = 123
303346
/* Trailing Comment */
304347
"""
@@ -316,6 +359,14 @@ public class CommentTests: PrettyPrintTestCase {
316359
317360
let reallyLongVariableName = 123 /* This comment should not wrap */
318361
362+
let a = myfun(
363+
var1: 123 /* Cmt 5 */
364+
)
365+
366+
guard condition else {
367+
return /* Cmt 6 */
368+
}
369+
319370
let d = 123
320371
/* Trailing Comment */
321372

0 commit comments

Comments
 (0)