Skip to content
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

Add automatic colons or paranthesis to nodes when needed #2112

Merged
merged 1 commit into from
Aug 31, 2023

Conversation

natikgadzhi
Copy link
Contributor

This pull request adds a convenience init to EnumCaseParameterSyntax so that when you're initializing it with a firstName, it adds colon automatically.

It's the first step in #1984.

Changes

How to review this

  • I love tinkering with this, but I do feel like Alice in Wonderland. @ahoppen, did I capture your idea well, does this approach seem right?
  • I've used let firstName: TokenSyntax? = firstName to trick Swift into using the right overload with an optional firstName so that the underlying generated init will be called, instead of copying it over.
  • While this PR might be self-sufficient, we should probably add the rest of the todos to this as well?

TODO

Copying over from the issue:
And the same strategy can also be applied

  • Other nodes that have an optional colon
  • ClosureCaptureSyntax.equal
  • Automatically add parentheses for the following nodes if arguments are present:
    • AttributeSyntax
    • MacroExpansionDeclSyntax
    • ClosureCaptureModifierSyntax
    • FunctionCallExprSyntax
    • FreestandingMacroExpansionSyntax

Copy link
Contributor

@Matejkob Matejkob left a comment

Choose a reason for hiding this comment

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

Great work on this! I took the liberty of adding a few questions and suggestions—I hope that's alright with you.

I saw in the description that you're still considering whether this will be the final PR or if additional initializers are coming. If you're leaning towards making this the final one, don't forget to run swift-format after your latest changes.

Sources/SwiftSyntax/Convenience.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/Convenience.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/Convenience.swift Outdated Show resolved Hide resolved
Sources/SwiftSyntax/Convenience.swift Outdated Show resolved Hide resolved
@natikgadzhi
Copy link
Contributor Author

natikgadzhi commented Aug 30, 2023

@Matejkob HUGE thank you for the review!

  • Committed your suggestions
  • Formatted with swift-format from brew.
  • I think swift-format@main is broken, filing an issue and will do some yak shaving tomorrow.

Formatting aside, I will try and work on a few more items on the list above. I'll try to keep this PR small — if you'd prefer to merge this work in small chunks, I would love that as well.

@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

  • I think swift-format@main is broken, filing an issue and will do some yak shaving tomorrow.

Which error are you seeing? Running ./format.py should work.

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.

Thank you. That looks very good to me. Just some minor comments. And thanks for the review @Matejkob.

Sources/SwiftSyntax/Convenience.swift Outdated Show resolved Hide resolved
@@ -45,3 +45,57 @@ extension MemberAccessExprSyntax {
)
}
}

extension EnumCaseParameterSyntax {
Copy link
Member

Choose a reason for hiding this comment

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

Could you order these extensions alphabetically and add the following section at the end of the file, similar to what we have in other parts of the codebase.

//==========================================================================//
// IMPORTANT: If you are tempted to add an extension here, please insert    //
// it in alphabetical order above                                           //
//==========================================================================//

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think that writing a script that does this automatically for an arbitrary file would be a nice exercise and an example of how to use SwiftSyntax!

How would you feel about having an additional script that ./format.py would invoke? It could live as an executable target in swift-format as well, perhaps?

Or.. maybe that could be a part of swift-format that would only trigger on files with paths matching a setting? I.e.

{
  // ...
  "rules" : {
    "AllPublicDeclarationsHaveDocumentation" : false,
    "AlwaysUseLowerCamelCase" : true,
    
    // What if a rule would support not only a Boolean value, but an array of paths for which the value is true (or different from a default value)
    "AlphabetizeDeclarations": [ "Sources/SwiftSyntax/generated/Convenience.swift", "**/Convenience.swift"]
    // ...
  },
  // ... 
}

I could start with a little experiment as an example to learn to use SwiftSyntax, and if I can get a prototype, I could write a proposal on Swift Forums to make the settings support path arrays?

Copy link
Contributor

Choose a reason for hiding this comment

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

We discussed a similar idea here. At the time, we concluded that it wouldn't be a trivial task, so the subject was put on the back burner. However, the fact that the idea has resurfaced suggests there might be a real need for such a tool, and it could be beneficial to revisit the discussion.

How about we move this discussion to a separate issue? @natikgadzhi, would you like to do the honors and open one? ☺️

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 have an idea of how to implement it, I’d be happy to take it. But as I mentioned in the other discussion, I think it’s far from a trivial task, if not even (nearly) impossible.

And so far, we have been good with these comments so at least as far as I’m concerned there’s no immediate need for action.

@natikgadzhi
Copy link
Contributor Author

Which error are you seeing? Running ./format.py should work.

I cooked SwiftPM wrong, and it couldn't reset Package.resolved, so it tried to build a fresh swift-format with older swift-syntax. Fixed now.

@Matejkob @ahoppen thank you both for reviewing, and for your feedback! I'm learning the pieces, and it starts to make more sense with every hour 🙃 I worked through the feedback and suggestions.

Since we're not squashing commits, I've amended the original one, so it looks clean in the repo history. I will add the next items on the list as separate commits.

I have a suggestion: @ahoppen, @Matejkob, how do you feel about splitting the work into three pull requests:

  1. This one, that will include the other nodes that have an optional colon (um, I will search through the codebase and try to actually use my brain to come up with a list, but may need help).
  2. Another small pull request that will add an init for ClosureCaptureSyntax that adds equal token. It should be small and easy.
  3. The third pull request that will include parentheses for a bunch of nodes.

That way, I can make progress on them and ship them one by one faster than holding a bigger PR. Hopefully, it will be easier to review as well.

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. Thanks a lot. Do you also want to cover the other cases mentioned in the issue in follow-up PRs?

@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

@swift-ci Please test

@ahoppen ahoppen enabled auto-merge August 30, 2023 22:08
@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

I have a suggestion: @ahoppen, @Matejkob, how do you feel about splitting the work into three pull requests:

  1. This one, that will include the other nodes that have an optional colon (um, I will search through the codebase and try to actually use my brain to come up with a list, but may need help).
  2. Another small pull request that will add an init for ClosureCaptureSyntax that adds equal token. It should be small and easy.
  3. The third pull request that will include parentheses for a bunch of nodes.

That way, I can make progress on them and ship them one by one faster than holding a bigger PR. Hopefully, it will be easier to review as well.

I’m also happy to merge this one as-is as a proof of concept and then make any similar changes to other nodes in follow-up PRs. But you can also keep amending this PR, whatever you prefer.

@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

@swift-ci Please test Windows

@natikgadzhi
Copy link
Contributor Author

Nice. Thanks a lot. Do you also want to cover the other cases mentioned in the issue in follow-up PRs?

Yep, makes sense — happy to work on the rest of it.

@ahoppen
Copy link
Member

ahoppen commented Aug 30, 2023

Great. Thanks

@ahoppen ahoppen merged commit 28e00f3 into swiftlang:main Aug 31, 2023
natikgadzhi added a commit to natikgadzhi/swift-syntax that referenced this pull request Aug 31, 2023
Provides release notes entries for:

- `ClosureCaptureSyntax.init` swiftlang#2127
- `EnumCaseParameterSyntax.init`
  swiftlang#2112
natikgadzhi added a commit to natikgadzhi/swift-syntax that referenced this pull request Sep 1, 2023
Provides release notes entries for:

- `ClosureCaptureSyntax.init` swiftlang#2127
- `EnumCaseParameterSyntax.init`
  swiftlang#2112

Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com>
natikgadzhi added a commit to natikgadzhi/swift-syntax that referenced this pull request Sep 1, 2023
Provides release notes entries for:

- `ClosureCaptureSyntax.init` swiftlang#2127
- `EnumCaseParameterSyntax.init`
  swiftlang#2112

Co-authored-by: Mateusz Bąk <bakmatthew@icloud.com>
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.

3 participants