Skip to content

Commit babff46

Browse files
authored
Merge pull request #742 from ahoppen/6.0/merge-main-2024-05-14
Merge `main` into `release/6.0`
2 parents 9e47d59 + 7467d35 commit babff46

File tree

3 files changed

+105
-9
lines changed

3 files changed

+105
-9
lines changed

Sources/SwiftFormat/PrettyPrint/Comment.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct Comment {
6464

6565
switch kind {
6666
case .line, .docLine:
67-
self.text = [text.trimmingTrailingWhitespace()]
67+
self.text = [text]
6868
self.text[0].removeFirst(kind.prefixLength)
6969
self.length = self.text.reduce(0, { $0 + $1.count + kind.prefixLength + 1 })
7070

@@ -88,8 +88,9 @@ struct Comment {
8888
func print(indent: [Indent]) -> String {
8989
switch self.kind {
9090
case .line, .docLine:
91-
let separator = "\n" + kind.prefix
92-
return kind.prefix + self.text.joined(separator: separator)
91+
let separator = "\n" + indent.indentation() + kind.prefix
92+
let trimmedLines = self.text.map { $0.trimmingTrailingWhitespace() }
93+
return kind.prefix + trimmedLines.joined(separator: separator)
9394
case .block, .docBlock:
9495
let separator = "\n"
9596
return kind.prefix + self.text.joined(separator: separator) + "*/"

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

+29-6
Original file line numberDiff line numberDiff line change
@@ -3384,14 +3384,37 @@ 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 (.comment(let c1, _), .comment(let c2, _))
3390-
where c1.kind == .docLine && c2.kind == .docLine:
3391-
var newComment = c1
3392-
newComment.addText(c2.text)
3393-
tokens[tokens.count - 1] = .comment(newComment, wasEndOfLine: false)
3394-
return
3393+
case (.break(let breakKind, _, .soft(1, _)), .comment(let c2, _))
3394+
where breakAllowsCommentMerge(breakKind) && (c2.kind == .docLine || c2.kind == .line):
3395+
// we are search for the pattern of [line comment] - [soft break 1] - [line comment]
3396+
// where the comment type is the same; these can be merged into a single comment
3397+
if let nextToLast = tokens.dropLast().last,
3398+
case let .comment(c1, false) = nextToLast,
3399+
c1.kind == c2.kind
3400+
{
3401+
var mergedComment = c1
3402+
mergedComment.addText(c2.text)
3403+
tokens.removeLast() // remove the soft break
3404+
// replace the original comment with the merged one
3405+
tokens[tokens.count - 1] = .comment(mergedComment, wasEndOfLine: false)
3406+
3407+
// need to fix lastBreakIndex because we just removed the last break
3408+
lastBreakIndex = tokens.lastIndex(where: {
3409+
switch $0 {
3410+
case .break: return true
3411+
default: return false
3412+
}
3413+
})
3414+
canMergeNewlinesIntoLastBreak = false
3415+
3416+
return
3417+
}
33953418

33963419
// If we see a pair of spaces where one or both are flexible, combine them into a new token
33973420
// with the maximum of their counts.

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

+72
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)