Skip to content

Commit 896472d

Browse files
committed
Ensure newline after trailing line comments to prevent formatting issues
1 parent e1cd715 commit 896472d

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
@@ -644,4 +644,42 @@ final class AttributeTests: PrettyPrintTestCase {
644644

645645
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
646646
}
647+
648+
func testAttributesWithComment() {
649+
let input =
650+
"""
651+
@foo // comment
652+
@bar
653+
import Baz
654+
655+
"""
656+
let expected =
657+
"""
658+
@foo // comment
659+
@bar import Baz
660+
661+
"""
662+
663+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
664+
}
665+
666+
func testAttributesWithLineAndBlockComments() {
667+
let input =
668+
"""
669+
@foo // comment
670+
@bar /* comment */
671+
@zoo // comment
672+
import Baz
673+
674+
"""
675+
let expected =
676+
"""
677+
@foo // comment
678+
@bar /* comment */ @zoo // comment
679+
import Baz
680+
681+
"""
682+
683+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
684+
}
647685
}

0 commit comments

Comments
 (0)