1

I started to playing with OpenRewrite and what I need is to use OpenRewrite to change the formatting of curly braces (for all the Java classes and interfaces).

So from this:

public class Foo
{
    // something here
}

to have this:

public class Foo { // <-- left curly bracket on the same line as a class name
    // something here
}

I was trying to achieve this with simple existing org.openrewrite.java.format.AutoFormat recipe, but it seems this one completely ignores the curly braces. Then I tried to write some custom recipe and I was playing with method autoFormat. Something like this:

 @Override
 public J.ClassDeclaration visitClassDeclaration(final J.ClassDeclaration classDec, final ExecutionContext executionContext) {
     return autoFormat(classDec, executionContext);
 }

but it seems that autoFormat just uses AutoFormatVisitor and it seems to me that this one is also used by existing org.openrewrite.java.format.AutoFormat recipe - so it doesn't have the desired effect.

Could you please point me to some idea how this can be solved by OpenRewrite?

UPDATE I tried to apply default IntelliJ style.

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>4.44.0</version>
    <configuration>
        <activeRecipes>
            <recipe>org.openrewrite.java.format.AutoFormat</recipe>
        </activeRecipes>
        <activeStyles>
            <style>org.openrewrite.java.style.IntelliJ</style>
        </activeStyles>
    </configuration>
</plugin> 

but it still didn't change the curly brackets.

PetrS
  • 1,110
  • 14
  • 38

2 Answers2

1

You likely want to look into styles: https://docs.openrewrite.org/concepts-explanations/styles

We support a range of well known styles already, such as for instance: https://github.com/openrewrite/rewrite/blob/main/rewrite-java/src/main/java/org/openrewrite/java/style/IntelliJ.java

The Maven and Gradle plugins support passing in styles: https://docs.openrewrite.org/reference/rewrite-maven-plugin#plugin-configuration

./mvnw rewrite:run -Drewrite.activeStyles=an.example.Style0

This style should then be picked up by the code change or formatter recipes you apply.

Tim
  • 19,793
  • 8
  • 70
  • 95
  • I've already tried the style, see the updated question. – PetrS May 19 '23 at 15:23
  • I also tried to do it via checkstyle - I mean I created a simple checkstyle.xml with desired curly bracket formatting and followed this: https://docs.openrewrite.org/running-recipes/popular-recipe-guides/automatically-fix-checkstyle-violations But still no luck. – PetrS May 19 '23 at 15:25
  • Tyler helped to provide some context that I was missing in https://stackoverflow.com/a/76293036/53444 ; As also seen on the comment below his answer: feel free to open up a bug/feature request. – Tim May 21 '23 at 09:16
1

So, the style classes in OpenRewrite are meant to closely support the configuration you see in Intellij:

enter image description here

However, in the case of WrappingAndBracesStyle there is currently a very minimally implementation. To add full support, this style class must be filled out (using the Intellij dialog as a guide) and then the WrappingAndBracesVisitor needs to be implemented to use the styles to manipulate the whitespace of the various tree elements. Note that indention is handled by a different style/visitor and this visitor should focus specifically on removing/adding new lines.

Tyler Van Gorder
  • 453
  • 6
  • 14
  • Thank you for chiming in here Tyler! Guess that makes this a bug/feature request, with no current solution. @PetrS feel free to open up an issue over on https://github.com/openrewrite/rewrite/issues – Tim May 21 '23 at 09:14