Skip to content

Commit e261c72

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

File tree

2 files changed

+103
-4
lines changed

2 files changed

+103
-4
lines changed

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3038,6 +3038,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
30383038

30393039
if wasLineComment && !hasAppendedTrailingComment {
30403040
trailingCommentTokens.forEach(appendToken)
3041+
hasAppendedTrailingComment = true
3042+
}
3043+
if hasAppendedTrailingComment {
3044+
appendNewlines(.hard)
30413045
}
30423046
}
30433047

@@ -3493,7 +3497,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
34933497
generateEnableFormattingIfNecessary(position..<position + piece.sourceLength)
34943498
appendToken(.comment(Comment(kind: .line, leadingIndent: leadingIndent, text: text), wasEndOfLine: false))
34953499
generateDisableFormattingIfNecessary(position + piece.sourceLength)
3496-
appendNewlines(.soft)
3500+
appendNewlines(.hard)
34973501
isStartOfFile = false
34983502
}
34993503
requiresNextNewline = true
@@ -3522,7 +3526,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
35223526
generateEnableFormattingIfNecessary(position..<position + piece.sourceLength)
35233527
appendToken(.comment(Comment(kind: .docLine, leadingIndent: leadingIndent, text: text), wasEndOfLine: false))
35243528
generateDisableFormattingIfNecessary(position + piece.sourceLength)
3525-
appendNewlines(.soft)
3529+
appendNewlines(.hard)
35263530
isStartOfFile = false
35273531
requiresNextNewline = true
35283532
leadingIndent = nil
@@ -3531,7 +3535,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
35313535
generateEnableFormattingIfNecessary(position..<position + piece.sourceLength)
35323536
appendToken(.comment(Comment(kind: .docBlock, leadingIndent: leadingIndent, text: text), wasEndOfLine: false))
35333537
generateDisableFormattingIfNecessary(position + piece.sourceLength)
3534-
appendNewlines(.soft)
3538+
appendNewlines(.hard)
35353539
isStartOfFile = false
35363540
requiresNextNewline = false
35373541
leadingIndent = nil
@@ -3651,7 +3655,15 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
36513655
fatalError("Found non-break token at lastBreakIndex. TokenStreamCreator is invalid.")
36523656
}
36533657

3654-
guard !canMergeNewlinesIntoLastBreak else {
3658+
let hasExistingHardNewline: Bool
3659+
switch existingNewlines {
3660+
case .elective, .soft, .escaped:
3661+
hasExistingHardNewline = false
3662+
case .hard:
3663+
hasExistingHardNewline = true
3664+
}
3665+
3666+
guard hasExistingHardNewline || !canMergeNewlinesIntoLastBreak else {
36553667
tokens[lastBreakIndex] = .break(kind, size: size, newlines: existingNewlines + newlines)
36563668
return
36573669
}

Tests/SwiftFormatTests/PrettyPrint/AttributeTests.swift

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -623,4 +623,91 @@ 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 testAttributesConsecutiveComments() {
646+
let input =
647+
"""
648+
@foo // comment
649+
// comment
650+
@bar
651+
import Baz
652+
653+
"""
654+
let expected =
655+
"""
656+
@foo // comment
657+
// comment
658+
@bar import Baz
659+
660+
"""
661+
662+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
663+
}
664+
665+
func testAttributesConsecutiveCommentsWithSomeNewlines() {
666+
let input =
667+
"""
668+
@foo // comment
669+
670+
671+
672+
// comment
673+
674+
675+
676+
@bar
677+
let a = 1
678+
679+
"""
680+
let expected =
681+
"""
682+
@foo // comment
683+
684+
// comment
685+
686+
@bar
687+
let a = 1
688+
689+
"""
690+
691+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
692+
}
693+
694+
func testAttributesWithLineAndBlockComments() {
695+
let input =
696+
"""
697+
@foo // comment
698+
@bar /* comment */
699+
@zoo // comment
700+
import Baz
701+
702+
"""
703+
let expected =
704+
"""
705+
@foo // comment
706+
@bar /* comment */ @zoo // comment
707+
import Baz
708+
709+
"""
710+
711+
assertPrettyPrintEqual(input: input, expected: expected, linelength: 45)
712+
}
626713
}

0 commit comments

Comments
 (0)