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 {
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
Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments