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

Enhance autoformat logic for Groovy files #4009

Merged
merged 2 commits into from
Feb 16, 2024
Merged

Conversation

shanman190
Copy link
Contributor

@shanman190 shanman190 commented Feb 16, 2024

What's changed?

Bringing back some auto formatting rules to the G type. This focuses on tabs VS spaces, normalizing EOL characters, blank lines, and removing trailing whitespace.

What's your motivation?

Historically, Groovy auto formatting was a bear and would pretty often result in files becoming invalid. As a result, it was disabled fully in the org.openrewrite.java.format.AutoFormatVisitor, so that the Java auto formatting rules would never be applied to any non-J type.

Anything in particular you'd like reviewers to focus on?

While re-introducing auto format support, I found two bugs. One of which is found fixed within this PR, but the other has been noted with a pair of disabled tests.

  • If a visitor used with withClasses or withImports on an instance of G, this would result in the G.CompilationUnit being recreated due to a bug in the logic. Specifically, getting the classes or imports will always return a new list which means that they will never have instance identity equality. So instead, we should verify that the elements themselves are unchanged, then take the appropriate actions.
  • GroovyParser appears to be unable to parse the implicit-public constructor. This additionally may extend into constructors with default arguments, so there is a second test case to cover that specific case.

Have you considered any alternatives or workarounds?

N/A

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've read and applied the recipe conventions and best practices
  • I've used the IntelliJ IDEA auto-formatter on affected files

@shanman190 shanman190 added bug Something isn't working enhancement New feature or request labels Feb 16, 2024
@shanman190 shanman190 self-assigned this Feb 16, 2024
@shanman190
Copy link
Contributor Author

I'll figure out what is wrong with auto formatting the rewrite-gradle tests tomorrow and get that fixed.

@shanman190 shanman190 merged commit 2055f3f into main Feb 16, 2024
1 check passed
@shanman190 shanman190 deleted the feature/groovy-autoformat branch February 16, 2024 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request parser-groovy
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

2 participants