Skip to content

Commit 0e99d9d

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

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ public class PrettyPrinter {
146146
/// enabled (see ``isBreakingSuppressed``).
147147
private var allowSuppressedDiscretionaryBreaks = false
148148

149+
/// Overrides break suppression for line or doc comments, or similar cases where a line break is required.
150+
/// Reset after handling the break.
151+
private var shouldOverrideBreakSuppression = false
152+
149153
/// The computed indentation level, as a number of spaces, based on the state of any unclosed
150154
/// delimiters and whether or not the current line is a continuation line.
151155
private var currentIndentation: [Indent] {
@@ -426,7 +430,8 @@ public class PrettyPrinter {
426430
case .soft(_, let discretionary):
427431
// A discretionary newline (i.e. from the source) should create a line break even if the
428432
// rules for breaking are disabled.
429-
overrideBreakingSuppressed = discretionary && allowSuppressedDiscretionaryBreaks
433+
overrideBreakingSuppressed =
434+
shouldOverrideBreakSuppression || (discretionary && allowSuppressedDiscretionaryBreaks)
430435
mustBreak = true
431436
case .hard:
432437
// A hard newline must always create a line break, regardless of the context.
@@ -455,6 +460,7 @@ public class PrettyPrinter {
455460
outputBuffer.enqueueSpaces(size)
456461
lastBreak = false
457462
}
463+
shouldOverrideBreakSuppression = false
458464

459465
// Print out the number of spaces according to the size, and adjust spaceRemaining.
460466
case .space(let size, _):
@@ -472,7 +478,7 @@ public class PrettyPrinter {
472478

473479
case .comment(let comment, let wasEndOfLine):
474480
lastBreak = false
475-
481+
shouldOverrideBreakSuppression = comment.kind == .docLine || comment.kind == .line
476482
if wasEndOfLine {
477483
if !(canFit(comment.length) || isBreakingSuppressed) {
478484
diagnose(.moveEndOfLineComment, category: .endOfLineComment)

Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,42 @@ final class AttributeTests: PrettyPrintTestCase {
623623

624624
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
625625
}
626+
627+
func testAttributesWithComment() {
628+
let input =
629+
"""
630+
@foo // comment
631+
@bar
632+
import Baz
633+
634+
"""
635+
let expected =
636+
"""
637+
@foo // comment
638+
@bar import Baz
639+
640+
"""
641+
642+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
643+
}
644+
645+
func testAttributesWithLineAndBlockComments() {
646+
let input =
647+
"""
648+
@foo // comment
649+
@bar /* comment */
650+
@zoo // comment
651+
import Baz
652+
653+
"""
654+
let expected =
655+
"""
656+
@foo // comment
657+
@bar /* comment */ @zoo // comment
658+
import Baz
659+
660+
"""
661+
662+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
663+
}
626664
}

0 commit comments

Comments
 (0)