OpenRewrite - 更改大括号格式的方法

问题描述 投票:0回答:2

我开始玩 OpenRewrite,我需要的是使用 OpenRewrite 更改花括号的格式(对于所有 Java 类和接口)。

所以从这个:

public class Foo
{
    // something here
}

拥有这个:

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

我试图用简单的现有

org.openrewrite.java.format.AutoFormat
食谱来实现这一点,但似乎这个完全忽略了花括号。 然后我尝试写一些自定义食谱,我正在玩方法
autoFormat
。像这样的东西:

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

但似乎

autoFormat
只是使用了
AutoFormatVisitor
在我看来,这个也被现有的
org.openrewrite.java.format.AutoFormat
配方使用 - 所以它没有达到预期的效果。

你能告诉我一些关于如何通过 OpenRewrite 解决这个问题的想法吗?

更新 我尝试应用默认的 IntelliJ 样式。

<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> 

但它仍然没有改变花括号。

java format curly-braces openrewrite
2个回答
0
投票

您可能想研究样式: https://docs.openrewrite.org/concepts-explanations/styles

我们已经支持一系列众所周知的样式,例如: https://github.com/openrewrite/rewrite/blob/main/rewrite-java/src/main/java/org/openrewrite/java/style/IntelliJ.java

Maven 和 Gradle 插件支持传入样式: https://docs.openrewrite.org/reference/rewrite-maven-plugin#plugin-configuration

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

然后您应用的代码更改或格式化程序配方应该采用这种风格。


0
投票

因此,OpenRewrite 中的样式类旨在密切支持您在 Intellij 中看到的配置:

但是,在 WrappingAndBracesStyle 的情况下,目前有一个非常简单的实现。要添加完整支持,必须填写此样式类(使用 Intellij 对话框作为指南),然后需要实现 WrappingAndBracesVisitor 以使用样式来操作各种树元素的空白。请注意,缩进由不同的样式/访问者处理,该访问者应特别关注删除/添加新行。

© www.soinside.com 2019 - 2024. All rights reserved.