Skip to content

Commit 7fa247f

Browse files
committed
Ensure newline after trailing line comments to prevent formatting issues
1 parent e70ab31 commit 7fa247f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

+3
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,9 @@ public class PrettyPrinter {
479479
}
480480
}
481481
outputBuffer.write(comment.print(indent: currentIndentation))
482+
if wasEndOfLine, (comment.kind == .line || comment.kind == .docLine) {
483+
outputBuffer.writeNewlines(.hard, shouldIndentBlankLines: configuration.indentBlankLines)
484+
}
482485

483486
case .verbatim(let verbatim):
484487
outputBuffer.writeVerbatim(verbatim.print(indent: currentIndentation), length)

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

+39
Original file line numberDiff line numberDiff line change
@@ -1107,4 +1107,43 @@ final class CommentTests: PrettyPrintTestCase {
11071107

11081108
assertPrettyPrintEqual(input: input, expected: input, linelength: 80)
11091109
}
1110+
1111+
func testCommentWithAttributes() {
1112+
let input =
1113+
"""
1114+
@foo // comment
1115+
@bar
1116+
import Baz
1117+
1118+
"""
1119+
let expected =
1120+
"""
1121+
@foo // comment
1122+
@bar import Baz
1123+
1124+
"""
1125+
1126+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
1127+
}
1128+
1129+
func testLineAndBlockCommentsWithAttributes() {
1130+
let input =
1131+
"""
1132+
@foo // comment
1133+
@bar /* comment */
1134+
@zoo // comment
1135+
import Baz
1136+
1137+
"""
1138+
let expected =
1139+
"""
1140+
@foo // comment
1141+
@bar /* comment */ @zoo // comment
1142+
import Baz
1143+
1144+
"""
1145+
1146+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 80)
1147+
}
1148+
11101149
}

0 commit comments

Comments
 (0)