Skip to content

Commit 49b3bbd

Browse files
authored
Merge pull request #1791 from gibachan/basic-format-indentation
Fix the indentation for nodes that have a parent node
2 parents cbac4b7 + 9a14f2d commit 49b3bbd

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

Sources/SwiftBasicFormat/BasicFormat.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ open class BasicFormat: SyntaxRewriter {
293293
defer {
294294
self.previousToken = token
295295
}
296+
let isInitialToken = self.previousToken == nil
296297
let previousToken = self.previousToken ?? token.previousToken(viewMode: viewMode)
297298
let nextToken = token.nextToken(viewMode: viewMode)
298299

@@ -393,7 +394,7 @@ open class BasicFormat: SyntaxRewriter {
393394
}
394395
}
395396

396-
if leadingTrivia.indentation(isOnNewline: previousTokenWillEndWithNewline) == [] {
397+
if leadingTrivia.indentation(isOnNewline: isInitialToken || previousTokenWillEndWithNewline) == [] {
397398
// If the token starts on a new line and does not have indentation, this
398399
// is the last non-indented token. Store its indentation level
399400
anchorPoints[token] = currentIndentationLevel

Tests/SwiftBasicFormatTest/BasicFormatTests.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,4 +341,43 @@ final class BasicFormatTest: XCTestCase {
341341
"""
342342
)
343343
}
344+
345+
func testSubTreeNode() {
346+
let decl: DeclSyntax = """
347+
func test() {
348+
print(1)
349+
}
350+
"""
351+
let body = decl.cast(FunctionDeclSyntax.self).body!
352+
353+
assertFormatted(
354+
source: body.formatted().description,
355+
expected: """
356+
{
357+
print(1)
358+
}
359+
"""
360+
)
361+
}
362+
363+
func testSubTreeNodeWithIndentedParentNode() {
364+
let decl: DeclSyntax = """
365+
struct X {
366+
func test() {
367+
print(1)
368+
}
369+
}
370+
"""
371+
372+
let body = decl.cast(StructDeclSyntax.self).memberBlock.members.first!.decl.cast(FunctionDeclSyntax.self).body!
373+
374+
assertFormatted(
375+
source: body.formatted().description,
376+
expected: """
377+
{
378+
print(1)
379+
}
380+
"""
381+
)
382+
}
344383
}

0 commit comments

Comments
 (0)