Skip to content

Commit 2a85d12

Browse files
authored
Merge pull request #642 from dylansturg/even_longer_imports
Ignore too long end of line comments when they're wrapped in `printCo…
2 parents b4b7ce1 + f01e044 commit 2a85d12

File tree

3 files changed

+57
-26
lines changed

3 files changed

+57
-26
lines changed

Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ public class PrettyPrinter {
515515

516516
write(comment.print(indent: currentIndentation))
517517
if wasEndOfLine {
518-
if comment.length > spaceRemaining {
518+
if comment.length > spaceRemaining && !isBreakingSuppressed {
519519
diagnose(.moveEndOfLineComment, category: .endOfLineComment)
520520
}
521521
} else {

Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift

+5-4
Original file line numberDiff line numberDiff line change
@@ -2746,9 +2746,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
27462746
/// will stay inside the group.
27472747
///
27482748
/// * If the trailing comment is a line comment, we first append any enqueued after-tokens
2749-
/// that are *not* breaks or newlines, then we append the comment, and then the remaining
2750-
/// after-tokens. Due to visitation ordering, this ensures that a trailing line comment is
2751-
/// not incorrectly inserted into the token stream *after* a break or newline.
2749+
/// that are *not* related to breaks or newlines (e.g. includes print control tokens), then
2750+
/// we append the comment, and then the remaining after-tokens. Due to visitation ordering,
2751+
/// this ensures that a trailing line comment is not incorrectly inserted into the token stream
2752+
/// *after* a break or newline.
27522753
private func appendAfterTokensAndTrailingComments(_ token: TokenSyntax) {
27532754
let (wasLineComment, trailingCommentTokens) = afterTokensForTrailingComment(token)
27542755
let afterGroups = afterMap.removeValue(forKey: token) ?? []
@@ -2763,7 +2764,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
27632764
var shouldExtractTrailingComment = false
27642765
if wasLineComment && !hasAppendedTrailingComment {
27652766
switch afterToken {
2766-
case .break: shouldExtractTrailingComment = true
2767+
case .break, .printerControl: shouldExtractTrailingComment = true
27672768
default: break
27682769
}
27692770
}

Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift

+51-21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import _SwiftFormatTestSupport
2+
13
final class CommentTests: PrettyPrintTestCase {
24
func testDocumentationComments() {
35
let input =
@@ -408,7 +410,7 @@ final class CommentTests: PrettyPrintTestCase {
408410
case quux
409411
}
410412
"""
411-
413+
412414
let expected =
413415
"""
414416
struct Foo {
@@ -424,7 +426,7 @@ final class CommentTests: PrettyPrintTestCase {
424426
}
425427
426428
"""
427-
429+
428430
assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
429431
}
430432

@@ -594,25 +596,25 @@ final class CommentTests: PrettyPrintTestCase {
594596

595597
func testCommentsInIfStatements() {
596598
let input =
597-
"""
598-
if foo.bar && false && // comment about foo.bar
599-
baz && // comment about baz
600-
// comment about next
601-
next
602-
&& // other is important
603-
// second line about other
604-
other &&
605-
// comment about final on a new line
606-
final
607-
{
608-
}
609-
if foo.bar && foo.baz
610-
&& // comment about the next line
611-
// another comment line
612-
next.line
613-
{
614-
}
615-
"""
599+
"""
600+
if foo.bar && false && // comment about foo.bar
601+
baz && // comment about baz
602+
// comment about next
603+
next
604+
&& // other is important
605+
// second line about other
606+
other &&
607+
// comment about final on a new line
608+
final
609+
{
610+
}
611+
if foo.bar && foo.baz
612+
&& // comment about the next line
613+
// another comment line
614+
next.line
615+
{
616+
}
617+
"""
616618

617619
let expected =
618620
"""
@@ -682,4 +684,32 @@ final class CommentTests: PrettyPrintTestCase {
682684

683685
assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
684686
}
687+
688+
func testDiagnoseMoveEndOfLineComment() {
689+
assertPrettyPrintEqual(
690+
input: """
691+
import veryveryverylongmodulenameherebecauseitistypical // special sentinel comment
692+
693+
func fooBarBazRunningOutOfIdeas() { 1️⃣// comment that needs to move
694+
if foo { // comment is fine
695+
}
696+
}
697+
698+
""",
699+
expected: """
700+
import veryveryverylongmodulenameherebecauseitistypical // special sentinel comment
701+
702+
func fooBarBazRunningOutOfIdeas() { // comment that needs to move
703+
if foo { // comment is fine
704+
}
705+
}
706+
707+
""",
708+
linelength: 45,
709+
whitespaceOnly: true,
710+
findings: [
711+
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length"),
712+
]
713+
)
714+
}
685715
}

0 commit comments

Comments
 (0)