Skip to content

Ignore too long end of line comments when they're wrapped in `printCo… #642

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/PrettyPrint/PrettyPrint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public class PrettyPrinter {

write(comment.print(indent: currentIndentation))
if wasEndOfLine {
if comment.length > spaceRemaining {
if comment.length > spaceRemaining && !isBreakingSuppressed {
diagnose(.moveEndOfLineComment, category: .endOfLineComment)
}
} else {
Expand Down
9 changes: 5 additions & 4 deletions Sources/SwiftFormat/PrettyPrint/TokenStreamCreator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2742,9 +2742,10 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
/// will stay inside the group.
///
/// * If the trailing comment is a line comment, we first append any enqueued after-tokens
/// that are *not* breaks or newlines, then we append the comment, and then the remaining
/// after-tokens. Due to visitation ordering, this ensures that a trailing line comment is
/// not incorrectly inserted into the token stream *after* a break or newline.
/// that are *not* related to breaks or newlines (e.g. includes print control tokens), then
/// we append the comment, and then the remaining after-tokens. Due to visitation ordering,
/// this ensures that a trailing line comment is not incorrectly inserted into the token stream
/// *after* a break or newline.
private func appendAfterTokensAndTrailingComments(_ token: TokenSyntax) {
let (wasLineComment, trailingCommentTokens) = afterTokensForTrailingComment(token)
let afterGroups = afterMap.removeValue(forKey: token) ?? []
Expand All @@ -2759,7 +2760,7 @@ fileprivate final class TokenStreamCreator: SyntaxVisitor {
var shouldExtractTrailingComment = false
if wasLineComment && !hasAppendedTrailingComment {
switch afterToken {
case .break: shouldExtractTrailingComment = true
case .break, .printerControl: shouldExtractTrailingComment = true
default: break
}
}
Expand Down
72 changes: 51 additions & 21 deletions Tests/SwiftFormatTests/PrettyPrint/CommentTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import _SwiftFormatTestSupport

final class CommentTests: PrettyPrintTestCase {
func testDocumentationComments() {
let input =
Expand Down Expand Up @@ -408,7 +410,7 @@ final class CommentTests: PrettyPrintTestCase {
case quux
}
"""

let expected =
"""
struct Foo {
Expand All @@ -424,7 +426,7 @@ final class CommentTests: PrettyPrintTestCase {
}

"""

assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
}

Expand Down Expand Up @@ -594,25 +596,25 @@ final class CommentTests: PrettyPrintTestCase {

func testCommentsInIfStatements() {
let input =
"""
if foo.bar && false && // comment about foo.bar
baz && // comment about baz
// comment about next
next
&& // other is important
// second line about other
other &&
// comment about final on a new line
final
{
}
if foo.bar && foo.baz
&& // comment about the next line
// another comment line
next.line
{
}
"""
"""
if foo.bar && false && // comment about foo.bar
baz && // comment about baz
// comment about next
next
&& // other is important
// second line about other
other &&
// comment about final on a new line
final
{
}
if foo.bar && foo.baz
&& // comment about the next line
// another comment line
next.line
{
}
"""

let expected =
"""
Expand Down Expand Up @@ -682,4 +684,32 @@ final class CommentTests: PrettyPrintTestCase {

assertPrettyPrintEqual(input: input, expected: expected, linelength: 60)
}

func testDiagnoseMoveEndOfLineComment() {
assertPrettyPrintEqual(
input: """
import veryveryverylongmodulenameherebecauseitistypical // special sentinel comment

func fooBarBazRunningOutOfIdeas() { 1️⃣// comment that needs to move
if foo { // comment is fine
}
}

""",
expected: """
import veryveryverylongmodulenameherebecauseitistypical // special sentinel comment

func fooBarBazRunningOutOfIdeas() { // comment that needs to move
if foo { // comment is fine
}
}

""",
linelength: 45,
whitespaceOnly: true,
findings: [
FindingSpec("1️⃣", message: "move end-of-line comment that exceeds the line length"),
]
)
}
}