Skip to content

Fix CodeGenerationFormat to correctly indent LabeledExprListSyntax in method calls that start on a new line #2851

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

Conversation

roopekv
Copy link
Contributor

@roopekv roopekv commented Sep 13, 2024

This fixes a case where CodeGenerationFormat would incorrectly indent the arguments (LabeledExprListSyntax) and the right parenthesis of method calls that start on a new line when formatting children to be separated by a newline.

@roopekv
Copy link
Contributor Author

roopekv commented Sep 19, 2024

This fixes the remaining issue discussed in #2833 with @ahoppen and should be mergeable without conflicts.

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

Nice, just a syntactic comment. The approach looks good to me

Comment on lines 99 to 107
let inMethodCallThatStartsOnNewline =
if let functionCallExpr = node.parent?.as(FunctionCallExprSyntax.self),
let memberAccessExpr = functionCallExpr.calledExpression.as(MemberAccessExprSyntax.self),
startsOnNewline(memberAccessExpr.period)
{
true
} else {
false
}
Copy link
Member

Choose a reason for hiding this comment

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

If you mad startsOnNewline an extension on SyntaxProtocol this could be done without an if expression:

node.parent?.as(FunctionCallExprSyntax.self).calledExpression.as(MemberAccessExprSyntax.self)?.startsOnNewline ?? false

Why does this only apply if the called expression is a MemberAccessExprSyntax? Shouldn’t the same be true for top level function calls as well?

Copy link
Contributor Author

@roopekv roopekv Sep 20, 2024

Choose a reason for hiding this comment

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

If you mad startsOnNewline an extension on SyntaxProtocol this could be done without an if expression:

node.parent?.as(FunctionCallExprSyntax.self).calledExpression.as(MemberAccessExprSyntax.self)?.startsOnNewline ?? false

Done 👍

Why does this only apply if the called expression is a MemberAccessExprSyntax? Shouldn’t the same be true for top level function calls as well?

If you mean top level function calls by specifying the module, they also parse into MemberAccessExprSyntax. Example:

Swift
  .print(
    "1",
    "2",
    separator: ",",
    terminator: ";"
  )
image

Copy link
Member

Choose a reason for hiding this comment

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

I mean if you have something like

print(
  "1", 
  "2"
)

Or do we format that correctly already?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Or do we format that correctly already?

Yes we do.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, great. Thanks for confirming

@roopekv
Copy link
Contributor Author

roopekv commented Sep 20, 2024

Should I squash the commits to remove the use of the if expression etc. @ahoppen ?

@ahoppen
Copy link
Member

ahoppen commented Sep 23, 2024

Yes, that would be good

…` in method calls that start on a new line
@roopekv roopekv force-pushed the fix-code-gen-newline-method-call-indentation branch from 9676296 to 0dcb6df Compare September 23, 2024 15:42
@roopekv
Copy link
Contributor Author

roopekv commented Sep 23, 2024

Done 👍

@ahoppen
Copy link
Member

ahoppen commented Sep 23, 2024

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Sep 23, 2024

@swift-ci Please test

@ahoppen
Copy link
Member

ahoppen commented Sep 23, 2024

@swift-ci Please test Windows

1 similar comment
@ahoppen
Copy link
Member

ahoppen commented Sep 26, 2024

@swift-ci Please test Windows

@ahoppen ahoppen merged commit 0ecc929 into swiftlang:main Sep 27, 2024
3 checks passed
@roopekv roopekv deleted the fix-code-gen-newline-method-call-indentation branch September 28, 2024 17:22
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.

2 participants