-
Notifications
You must be signed in to change notification settings - Fork 439
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
Conversation
@@ -393,7 +394,7 @@ open class BasicFormat: SyntaxRewriter { | |||
} | |||
} | |||
|
|||
if leadingTrivia.indentation(isOnNewline: previousTokenWillEndWithNewline) == [] { | |||
if leadingTrivia.indentation(isOnNewline: isInitialToken || previousTokenWillEndWithNewline) == [] { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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)
}
"""
)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 😄
There was a problem hiding this comment.
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.
func test() { | ||
print(1) | ||
} |
There was a problem hiding this comment.
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:
func test() { | |
print(1) | |
} | |
func test() { | |
print(1) | |
} |
There was a problem hiding this comment.
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) == [] { |
There was a problem hiding this comment.
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?
b7c2726
to
f1f90f2
Compare
@swift-ci Please test |
The indentation doesn’t seem to match |
Add a test case
Head branch was pushed to by a user without write access
f1f90f2
to
9a14f2d
Compare
Ah, I did it and squashed the commits. Thanks! |
@swift-ci Please test |
@swift-ci Please test Windows |
@swift-ci please test windows |
Resolves #1744