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

Conversation

gibachan
Copy link
Contributor

Resolves #1744

@@ -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.

@gibachan gibachan marked this pull request as ready for review June 15, 2023 12:45
@gibachan gibachan requested a review from ahoppen as a code owner June 15, 2023 12:45
Comment on lines 347 to 349
func test() {
print(1)
}
Copy link
Member

Choose a reason for hiding this comment

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

I just realized that there is a simpler and more intuitive example for this behavior:

Suggested change
func test() {
print(1)
}
func test() {
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.

Fixed it 👍 9d40557

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

if leadingTrivia.indentation(isOnNewline: previousTokenWillEndWithNewline) == [] {
if leadingTrivia.indentation(isOnNewline: isInitialToken || previousTokenWillEndWithNewline) == [] {
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?

@gibachan gibachan force-pushed the basic-format-indentation branch from b7c2726 to f1f90f2 Compare June 15, 2023 23:33
@ahoppen
Copy link
Member

ahoppen commented Jun 16, 2023

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge June 16, 2023 10:50
@ahoppen
Copy link
Member

ahoppen commented Jun 16, 2023

The indentation doesn’t seem to match swift-format. Could you run ./format.py?

auto-merge was automatically disabled June 16, 2023 19:08

Head branch was pushed to by a user without write access

@gibachan gibachan force-pushed the basic-format-indentation branch from f1f90f2 to 9a14f2d Compare June 16, 2023 19:08
@gibachan
Copy link
Contributor Author

Ah, I did it and squashed the commits. Thanks!

@ahoppen
Copy link
Member

ahoppen commented Jun 19, 2023

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge June 19, 2023 07:22
@ahoppen
Copy link
Member

ahoppen commented Jun 19, 2023

@swift-ci Please test Windows

@kimdv
Copy link
Contributor

kimdv commented Jun 19, 2023

@swift-ci please test windows

@ahoppen ahoppen merged commit 49b3bbd into swiftlang:main Jun 19, 2023
@gibachan gibachan deleted the basic-format-indentation branch June 20, 2023 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extraneous indentation if the formatted node has parents
3 participants