Skip to content

Fix indentation issue if trailing trivia contains a newline at the end of an indentation scope #2428

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
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
15 changes: 13 additions & 2 deletions Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -594,8 +594,19 @@ open class BasicFormat: SyntaxRewriter {
trailingTriviaIndentation = anchorPointIndentation
}

leadingTrivia = leadingTrivia.indented(indentation: leadingTriviaIndentation, isOnNewline: previousTokenIsStringLiteralEndingInNewline)
trailingTrivia = trailingTrivia.indented(indentation: trailingTriviaIndentation, isOnNewline: false)
leadingTrivia = leadingTrivia.indented(
indentation: leadingTriviaIndentation,
isOnNewline: previousTokenIsStringLiteralEndingInNewline || previousTokenWillEndWithNewline
)
trailingTrivia = trailingTrivia.indented(
indentation: trailingTriviaIndentation,
isOnNewline: false,
// Don't add indentation to the last newline.
// Its indentation will be added to the next token's leading trivia, which
// might have a different indentation scope than this token (in particular
// if this token is a closing brace).
addIndentationAfterLastNewline: false
)

leadingTrivia = leadingTrivia.trimmingTrailingWhitespaceBeforeNewline(isBeforeNewline: leadingTriviaIsFollowedByNewline)
trailingTrivia = trailingTrivia.trimmingTrailingWhitespaceBeforeNewline(isBeforeNewline: nextTokenWillStartWithNewline)
Expand Down
23 changes: 18 additions & 5 deletions Sources/SwiftBasicFormat/Trivia+FormatExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,20 @@ extension Trivia {
}

/// Adds `indentation` after every newline in this trivia.
func indented(indentation: Trivia, isOnNewline: Bool) -> Trivia {
///
/// - Parameters:
/// - indentation: The amount of indentation to add.
/// - isOnNewline: Whether this token starts on a new line.
/// This causes the indentation to get added at the start of the trivia.
/// - addIndentationAfterLastNewline: Whether to add indentation after newline
/// if the newline is the last piece of trivia. Not doing this makes sense
/// if the indentation will get added to the next token's leading trivia
/// via `isOnNewline`.
func indented(
indentation: Trivia,
isOnNewline: Bool,
addIndentationAfterLastNewline: Bool = true
) -> Trivia {
guard !isEmpty else {
if isOnNewline {
return indentation
Expand All @@ -72,13 +85,13 @@ extension Trivia {

var indentedPieces: [TriviaPiece] = []
if isOnNewline {
indentedPieces.append(contentsOf: indentation)
indentedPieces += indentation
}

for piece in pieces {
for (index, piece) in pieces.enumerated() {
indentedPieces.append(piece)
if piece.isNewline {
indentedPieces.append(contentsOf: indentation)
if piece.isNewline && !(index == pieces.count - 1 && !addIndentationAfterLastNewline) {
indentedPieces += indentation
}
}

Expand Down
43 changes: 43 additions & 0 deletions Tests/SwiftBasicFormatTest/BasicFormatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -566,4 +566,47 @@ final class BasicFormatTest: XCTestCase {
"""
assertFormatted(source: source, expected: source)
}

func testNewlineInTrailingTriviaAtEndOfIndentationScope() throws {
assertFormatted(
tree: try FunctionDeclSyntax("func test()") {
CodeBlockItemSyntax("Task {\n}\n")
},
expected: """
func test() {
Task {
}
}
"""
)

assertFormatted(
tree: try FunctionDeclSyntax("func test()") {
CodeBlockItemSyntax("Task {\n}\n\n")
},
expected: """
func test() {
Task {
}
}
"""
)

assertFormatted(
tree: try FunctionDeclSyntax("func bar()") {
try FunctionDeclSyntax("func test()") {
CodeBlockItemSyntax("Task {\n}\n")
}
},
expected: """
func bar() {
func test() {
Task {
}
}
}
"""
)
}

}