Skip to content

Commit 174c1ae

Browse files
authored
Merge pull request #1867 from gibachan/parent-indentation
Respect indentation in the line for sub-tree in BasicFormat
2 parents 0d813c3 + 30bfccd commit 174c1ae

File tree

6 files changed

+68
-28
lines changed

6 files changed

+68
-28
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,18 @@ open class BasicFormat: SyntaxRewriter {
150150

151151
// MARK: - Customization points
152152

153+
/// If we are formatting a subtree and the line that the initial token occurs on is indented,
154+
/// use that line indentation for the first token in the subtree to format.
155+
///
156+
/// For example, when formatting only the code block in the following,
157+
/// then the opening `{` should be indented by four spaces.
158+
/// ```
159+
/// func test() {
160+
/// print(1)
161+
/// }
162+
/// ```
163+
open var inferInitialTokenIndentaiton: Bool { true }
164+
153165
/// Whether a leading newline on `token` should be added.
154166
open func requiresIndent(_ node: some SyntaxProtocol) -> Bool {
155167
return node.requiresIndent
@@ -413,6 +425,16 @@ open class BasicFormat: SyntaxRewriter {
413425
anchorPoints[token] = currentIndentationLevel
414426
}
415427

428+
if inferInitialTokenIndentaiton
429+
&& isInitialToken
430+
&& token.presence == .present
431+
{
432+
let indentationOfLine = token.indentationOfLine
433+
if token.leadingTrivia.pieces.suffix(indentationOfLine.pieces.count) != indentationOfLine.pieces {
434+
leadingTrivia += indentationOfLine
435+
}
436+
}
437+
416438
// Add a trailing space to the token unless
417439
// - it already ends with a whitespace or
418440
// - the next token will start starts with a newline after the rewrite

Sources/SwiftBasicFormat/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
add_swift_host_library(SwiftBasicFormat
1010
BasicFormat.swift
1111
generated/BasicFormat+Extensions.swift
12+
Syntax+Extensions.swift
1213
SyntaxProtocol+Formatted.swift
1314
Trivia+FormatExtensions.swift
1415
)
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+
extension TokenSyntax {
16+
/// The indentation of this token
17+
///
18+
/// In contrast to `indentationOfLine`, 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+
public 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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ fileprivate func findCommonAncestor(_ nodes: [Syntax]) -> Syntax? {
2222
}
2323

2424
class NoNewlinesFormat: BasicFormat {
25+
override var inferInitialTokenIndentaiton: Bool { false }
26+
2527
override func requiresNewline(between first: TokenSyntax?, and second: TokenSyntax?) -> Bool {
2628
return false
2729
}

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ final class BasicFormatTest: XCTestCase {
351351
let body = decl.cast(FunctionDeclSyntax.self).body!
352352

353353
assertFormatted(
354-
source: body.formatted().description,
354+
tree: body,
355355
expected: """
356356
{
357357
print(1)
@@ -372,9 +372,9 @@ final class BasicFormatTest: XCTestCase {
372372
let body = decl.cast(StructDeclSyntax.self).memberBlock.members.first!.decl.cast(FunctionDeclSyntax.self).body!
373373

374374
assertFormatted(
375-
source: body.formatted().description,
375+
tree: body,
376376
expected: """
377-
{
377+
{
378378
print(1)
379379
}
380380
"""

0 commit comments

Comments
 (0)