Skip to content

Commit b065fa3

Browse files
committed
Improvements based on feedback and further testing.
1 parent 6cf4ea0 commit b065fa3

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3384,10 +3384,14 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
33843384
/// This function also handles collapsing neighboring tokens in situations where that is
33853385
/// desired, like merging adjacent comments and newlines.
33863386
private func appendToken(_ token: Token) {
3387+
func breakAllowsCommentMerge(_ breakKind: BreakKind) -> Bool {
3388+
return breakKind == .same || breakKind == .continue || breakKind == .contextual
3389+
}
3390+
33873391
if let last = tokens.last {
33883392
switch (last, token) {
3389-
case (.break(.same, _, .soft(let count, _)), .comment(let c2, _))
3390-
where count == 1 && (c2.kind == .docLine || c2.kind == .line):
3393+
case (.break(let breakKind, _, .soft(1, _)), .comment(let c2, _))
3394+
where breakAllowsCommentMerge(breakKind) && (c2.kind == .docLine || c2.kind == .line):
33913395
// we are search for the pattern of [line comment] - [soft break 1] - [line comment]
33923396
// where the comment type is the same; these can be merged into a single comment
33933397
if let nextToLast = tokens.dropLast().last,

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ final class CommentTests: PrettyPrintTestCase {
8383
func testLineComments() {
8484
let input =
8585
"""
86+
// Line Comment0
87+
8688
// Line Comment1
8789
// Line Comment2
8890
let a = 123
@@ -93,6 +95,7 @@ final class CommentTests: PrettyPrintTestCase {
9395
// Comment 4
9496
9597
let reallyLongVariableName = 123 // This comment should not wrap
98+
// and should not combine with this comment
9699
97100
func MyFun() {
98101
// just a comment
@@ -135,6 +138,8 @@ final class CommentTests: PrettyPrintTestCase {
135138

136139
let expected =
137140
"""
141+
// Line Comment0
142+
138143
// Line Comment1
139144
// Line Comment2
140145
let a = 123
@@ -145,6 +150,7 @@ final class CommentTests: PrettyPrintTestCase {
145150
// Comment 4
146151
147152
let reallyLongVariableName = 123 // This comment should not wrap
153+
// and should not combine with this comment
148154
149155
func MyFun() {
150156
// just a comment
@@ -208,6 +214,13 @@ final class CommentTests: PrettyPrintTestCase {
208214
let c = [123, 456 // small comment
209215
]
210216
217+
// Multiline comment
218+
let d = [123,
219+
// comment line 1
220+
// comment line 2
221+
456
222+
]
223+
211224
/* Array comment */
212225
let a = [456, /* small comment */
213226
789]
@@ -236,6 +249,14 @@ final class CommentTests: PrettyPrintTestCase {
236249
123, 456, // small comment
237250
]
238251
252+
// Multiline comment
253+
let d = [
254+
123,
255+
// comment line 1
256+
// comment line 2
257+
456,
258+
]
259+
239260
/* Array comment */
240261
let a = [
241262
456, /* small comment */
@@ -760,4 +781,55 @@ final class CommentTests: PrettyPrintTestCase {
760781
]
761782
)
762783
}
784+
785+
func testLineWithDocLineComment() {
786+
// none of these should be merged if/when there is comment formatting
787+
let input =
788+
"""
789+
/// Doc line comment
790+
// Line comment
791+
/// Doc line comment
792+
// Line comment
793+
794+
// Another line comment
795+
796+
"""
797+
assertPrettyPrintEqual(input: input, expected: input, linelength: 80)
798+
}
799+
800+
func testNonmergeableComments() {
801+
// none of these should be merged if/when there is comment formatting
802+
let input =
803+
"""
804+
let x = 1 // end of line comment
805+
//
806+
807+
let y = // eol comment
808+
1 // another
809+
+ 2 // and another
810+
811+
"""
812+
813+
assertPrettyPrintEqual(input: input, expected: input, linelength: 80)
814+
}
815+
816+
func testMergeableComments() {
817+
// these examples should be merged and formatted if/when there is comment formatting
818+
let input =
819+
"""
820+
let z =
821+
// one comment
822+
// and another comment
823+
1 + 2
824+
825+
let w = [1, 2, 3]
826+
.foo()
827+
// this comment
828+
// could be merged with this one
829+
.bar()
830+
831+
"""
832+
833+
assertPrettyPrintEqual(input: input, expected: input, linelength: 80)
834+
}
763835
}

0 commit comments

Comments
 (0)