File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed
Tests/SwiftBasicFormatTest Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -293,6 +293,7 @@ open class BasicFormat: SyntaxRewriter {
293
293
defer {
294
294
self . previousToken = token
295
295
}
296
+ let isInitialToken = self . previousToken == nil
296
297
let previousToken = self . previousToken ?? token. previousToken ( viewMode: viewMode)
297
298
let nextToken = token. nextToken ( viewMode: viewMode)
298
299
@@ -393,7 +394,7 @@ open class BasicFormat: SyntaxRewriter {
393
394
}
394
395
}
395
396
396
- if leadingTrivia. indentation ( isOnNewline: previousTokenWillEndWithNewline) == [ ] {
397
+ if leadingTrivia. indentation ( isOnNewline: isInitialToken || previousTokenWillEndWithNewline) == [ ] {
397
398
// If the token starts on a new line and does not have indentation, this
398
399
// is the last non-indented token. Store its indentation level
399
400
anchorPoints [ token] = currentIndentationLevel
Original file line number Diff line number Diff line change @@ -341,4 +341,43 @@ final class BasicFormatTest: XCTestCase {
341
341
"""
342
342
)
343
343
}
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
+ }
344
383
}
You can’t perform that action at this time.
0 commit comments