Skip to content

Commit 97674b5

Browse files
authored
Merge pull request swiftlang#8 from allevato/sr-11107
Indent line comments correctly when they precede a continuation line.
2 parents 8c1b960 + 7737a05 commit 97674b5

File tree

4 files changed

+15
-3
lines changed

4 files changed

+15
-3
lines changed

Sources/SwiftFormatPrettyPrint/PrettyPrint.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,6 @@ public class PrettyPrinter {
313313
spaceRemaining -= text.count
314314

315315
case .comment(let comment, let wasEndOfLine):
316-
currentLineIsContinuation = false
317316
lastBreak = false
318317

319318
write(comment.print(indent: currentIndentation))

Sources/SwiftFormatPrettyPrint/TokenStreamCreator.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,13 +881,12 @@ private final class TokenStreamCreator: SyntaxVisitor {
881881
}
882882

883883
func visit(_ node: CodeBlockItemListSyntax) -> SyntaxVisitorContinueKind {
884-
insertTokens(.break(.reset, size: 0), .newline, betweenElementsOf: node)
885884
return .visitChildren
886885
}
887886

888887
func visit(_ node: CodeBlockItemSyntax) -> SyntaxVisitorContinueKind {
889888
before(node.firstToken, tokens: .open)
890-
after(node.lastToken, tokens: .close)
889+
after(node.lastToken, tokens: .close, .break(.reset, size: 0))
891890
return .visitChildren
892891
}
893892

Tests/SwiftFormatPrettyPrintTests/CommentTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,4 +416,17 @@ public class CommentTests: PrettyPrintTestCase {
416416

417417
assertPrettyPrintEqual(input: input, expected: expected, linelength: 100)
418418
}
419+
420+
public func testCommentOnContinuationLine() {
421+
let input =
422+
"""
423+
func foo() {
424+
return true
425+
// comment
426+
&& false
427+
}
428+
"""
429+
430+
assertPrettyPrintEqual(input: input, expected: input + "\n", linelength: 60)
431+
}
419432
}

Tests/SwiftFormatPrettyPrintTests/XCTestManifests.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ extension CommentTests {
103103
// to regenerate.
104104
static let __allTests__CommentTests = [
105105
("testBlockComments", testBlockComments),
106+
("testCommentOnContinuationLine", testCommentOnContinuationLine),
106107
("testContainerLineComments", testContainerLineComments),
107108
("testDocumentationBlockComments", testDocumentationBlockComments),
108109
("testDocumentationComments", testDocumentationComments),

0 commit comments

Comments
 (0)