Skip to content

Commit 6cf4ea0

Browse files
committed
Merge adjacent .line and .docLine comments into a single element.
1 parent bc7c751 commit 6cf4ea0

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

Sources/SwiftFormat/PrettyPrint/Comment.swift

Lines changed: 4 additions & 3 deletions
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

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,12 +3386,31 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
33863386
private func appendToken(_ token: Token) {
33873387
if let last = tokens.last {
33883388
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
3389+
case (.break(.same, _, .soft(let count, _)), .comment(let c2, _))
3390+
where count == 1 && (c2.kind == .docLine || c2.kind == .line):
3391+
// we are search for the pattern of [line comment] - [soft break 1] - [line comment]
3392+
// where the comment type is the same; these can be merged into a single comment
3393+
if let nextToLast = tokens.dropLast().last,
3394+
case let .comment(c1, false) = nextToLast,
3395+
c1.kind == c2.kind
3396+
{
3397+
var mergedComment = c1
3398+
mergedComment.addText(c2.text)
3399+
tokens.removeLast() // remove the soft break
3400+
// replace the original comment with the merged one
3401+
tokens[tokens.count - 1] = .comment(mergedComment, wasEndOfLine: false)
3402+
3403+
// need to fix lastBreakIndex because we just removed the last break
3404+
lastBreakIndex = tokens.lastIndex(where: {
3405+
switch $0 {
3406+
case .break: return true
3407+
default: return false
3408+
}
3409+
})
3410+
canMergeNewlinesIntoLastBreak = false
3411+
3412+
return
3413+
}
33953414

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

0 commit comments

Comments
 (0)