Skip to content

Fix the indentation for nodes that have a parent node #1791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Sources/SwiftBasicFormat/BasicFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ open class BasicFormat: SyntaxRewriter {
defer {
self.previousToken = token
}
let isInitialToken = self.previousToken == nil
let previousToken = self.previousToken ?? token.previousToken(viewMode: viewMode)
let nextToken = token.nextToken(viewMode: viewMode)

Expand Down Expand Up @@ -393,7 +394,7 @@ open class BasicFormat: SyntaxRewriter {
}
}

if leadingTrivia.indentation(isOnNewline: previousTokenWillEndWithNewline) == [] {
if leadingTrivia.indentation(isOnNewline: isInitialToken || previousTokenWillEndWithNewline) == [] {
Copy link
Contributor Author

@gibachan gibachan Jun 15, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the formatter takes a node that has a parent node, I think the initial node should be treated as starting a new line, even if it has a parent node, in order to accurately detect anchor points for indentation.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, very nice find. I was not expecting the issue in this area.

This raises a more fundamental question: What does it even mean to format a CodeBlockSyntax item standalone while it still has a parent that’s a FunctionDeclSyntax? I’m not sure if there really is a correct answer so maybe calling formatted should detach the syntax node from the tree, in which case we can always assume that we are formatting an entire tree and not just a subtree. What do you think?

CC @bnbarham: Do you have any opinions on my thoughts above?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, @bnbarham and I talked about this a little bit and I thought about this a little bit more and I think that your approach of setting the anchor point is the right one.

Another test case that I would like to add is the following. Ideally, we would indent the opening { by four spaces as well (as inferred from the indentation of func, similar to what we do in https://github.com/apple/swift-syntax/pull/1698/files#diff-32042eea35aac988fe01a592484fb9cbbff328267babb16f5eb8af29f8f9c288R64-R77).

let decl: DeclSyntax = """
struct X {
    func test() {
        print(1)
    }
}
"""

let body = decl.cast(StructDeclSyntax.self).memberBlock.members.first!.decl.cast(FunctionDeclSyntax.self).body!

assertFormatted(
  source: body.formatted().description,
  expected: """
  {
          print(1)
      }
  """
)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your consideration! I agree with you and have included your test case for now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thank you. Do you want to work on adding 4 spaces in front of {? If not, I’ll file an issue for it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! I'd love to work on it 😄

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s great. I’m looking forward to your PR.

// If the token starts on a new line and does not have indentation, this
// is the last non-indented token. Store its indentation level
anchorPoints[token] = currentIndentationLevel
Expand Down
39 changes: 39 additions & 0 deletions Tests/SwiftBasicFormatTest/BasicFormatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,43 @@ final class BasicFormatTest: XCTestCase {
"""
)
}

func testSubTreeNode() {
let decl: DeclSyntax = """
func test() {
print(1)
}
"""
let body = decl.cast(FunctionDeclSyntax.self).body!

assertFormatted(
source: body.formatted().description,
expected: """
{
print(1)
}
"""
)
}

func testSubTreeNodeWithIndentedParentNode() {
let decl: DeclSyntax = """
struct X {
func test() {
print(1)
}
}
"""

let body = decl.cast(StructDeclSyntax.self).memberBlock.members.first!.decl.cast(FunctionDeclSyntax.self).body!

assertFormatted(
source: body.formatted().description,
expected: """
{
print(1)
}
"""
)
}
}