Skip to content

Commit 00d014c

Browse files
committed
Respect indentation in the line for sub tree in BasicFormat
1 parent e6192b2 commit 00d014c

File tree

5 files changed

+55
-27
lines changed

5 files changed

+55
-27
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,15 @@ open class BasicFormat: SyntaxRewriter {
413413
anchorPoints[token] = currentIndentationLevel
414414
}
415415

416+
// Add an indentation in the line for the initial token,
417+
// if the token is present and its leadingTrivia does not contain the indentation.
418+
if isInitialToken && token.presence == .present {
419+
let indentationOfLine = token.indentationOfLine
420+
if token.leadingTrivia.pieces.suffix(indentationOfLine.pieces.count) != indentationOfLine.pieces {
421+
leadingTrivia += token.indentationOfLine
422+
}
423+
}
424+
416425
// Add a trailing space to the token unless
417426
// - it already ends with a whitespace or
418427
// - the next token will start starts with a newline after the rewrite
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
public extension TokenSyntax {
16+
/// The indentation of this token
17+
///
18+
/// In contrast to `indentation`, this does not walk to previous tokens to
19+
/// find the indentation of the line this token occurs on.
20+
private var indentation: Trivia? {
21+
let previous = self.previousToken(viewMode: .sourceAccurate)
22+
return ((previous?.trailingTrivia ?? []) + leadingTrivia).indentation(isOnNewline: false)
23+
}
24+
25+
/// Returns the indentation of the line this token occurs on
26+
var indentationOfLine: Trivia {
27+
var token: TokenSyntax = self
28+
if let indentation = token.indentation {
29+
return indentation
30+
}
31+
while let previous = token.previousToken(viewMode: .sourceAccurate) {
32+
token = previous
33+
if let indentation = token.indentation {
34+
return indentation
35+
}
36+
}
37+
38+
return []
39+
}
40+
}

Sources/SwiftParserDiagnostics/MissingNodesError.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,11 @@ fileprivate enum NodesDescriptionPart {
3636
switch self {
3737
case .tokensWithDefaultText(var tokens):
3838
if format {
39-
tokens = tokens.map({ NoNewlinesFormat(viewMode: .all).visit($0) })
39+
tokens = tokens.enumerated()
40+
.map { index, token in
41+
// We detach the nodes from the second one onwards, considering the line indentation only for the first node
42+
NoNewlinesFormat(viewMode: .all).visit(index == 0 ? token : token.detached)
43+
}
4044
}
4145
if !tokens.isEmpty {
4246
tokens[0] = tokens[0].with(\.leadingTrivia, [])

Sources/SwiftParserDiagnostics/ParseDiagnosticsGenerator.swift

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,31 +51,6 @@ fileprivate func getTokens(between first: TokenSyntax, and second: TokenSyntax)
5151
}
5252

5353
fileprivate extension TokenSyntax {
54-
/// The indentation of this token
55-
///
56-
/// In contrast to `indentation`, this does not walk to previous tokens to
57-
/// find the indentation of the line this token occurs on.
58-
private var indentation: Trivia? {
59-
let previous = self.previousToken(viewMode: .sourceAccurate)
60-
return ((previous?.trailingTrivia ?? []) + leadingTrivia).indentation(isOnNewline: false)
61-
}
62-
63-
/// Returns the indentation of the line this token occurs on
64-
var indentationOfLine: Trivia {
65-
var token: TokenSyntax = self
66-
if let indentation = token.indentation {
67-
return indentation
68-
}
69-
while let previous = token.previousToken(viewMode: .sourceAccurate) {
70-
token = previous
71-
if let indentation = token.indentation {
72-
return indentation
73-
}
74-
}
75-
76-
return []
77-
}
78-
7954
/// Assuming this token is a `poundAvailableKeyword` or `poundUnavailableKeyword`
8055
/// returns the opposite keyword.
8156
var negatedAvailabilityKeyword: TokenSyntax {

Tests/SwiftBasicFormatTest/BasicFormatTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ final class BasicFormatTest: XCTestCase {
374374
assertFormatted(
375375
tree: body,
376376
expected: """
377-
{
377+
{
378378
print(1)
379379
}
380380
"""

0 commit comments

Comments
 (0)