Checkstyle - 每行一个声明与一个块对齐

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

我正在尝试配置 Eclipse Checkstyle 但我找不到两件事的选项:

在方法中我想为每个声明换行:

public int calc (int x, 
                 int y,
                 int z) {
}

而不是:

public int calc (int x, int y, int z) {
}

声明应该像这样包装

private var                a;
private int []             b = null;
private ArrayList<Integer> c = new ArrayList<Integer> ();

而不是:

private var a;
private int [] b = null;
private ArrayList<Integer> c = new ArrayList<Integer> ();

我已经尝试过“MultipleVariableDeclarations”和 OneStatementPerLine,但它们仅在方法内部起作用,不适用于方法的参数。

我希望有人能帮助我。

java eclipse checkstyle
2个回答
2
投票

我认为没有内置的 Checkstyle 规则。内置规则旨在保持一种通用且既定的风格。就像 @yegor256 已经说过的,你的不是。您可以浏览此站点 以查找符合您要求的任何支票。我找不到任何。作为最后的选择,您始终可以写自己的支票

代码格式化程序的提示:转到

Window
->
Preferences
Save Actions
的文件管理器。在那里你可以定义你的代码应该总是在保存时格式化。也许你不需要像 Checkstyle 这样的工具来警告你未格式化的代码。


0
投票

没有内置的 Checkstyle 规则。但是,对于方法参数/参数对齐,您可以将 lib 与对 checkstyle 的额外检查一起使用 - check-tfij-style.

1。将 lib 添加到您的

maven-checkstyle-plugin
(也适用于 Gradle)作为依赖项,例如:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-checkstyle-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <configLocation>src/main/resources/checkstyle.xml</configLocation>
        <encoding>UTF-8</encoding>
        <consoleOutput>true</consoleOutput>
        <failsOnError>true</failsOnError>
        <linkXRef>false</linkXRef>
    </configuration>
    <executions>
        <execution>
            <id>validate</id>
            <phase>validate</phase>
            <goals>
                <goal>check</goal>
            </goals>
        </execution>
    </executions>
    <dependencies>
        <dependency>
            <groupId>pl.tfij</groupId>
            <artifactId>check-tfij-style</artifactId>
            <version>1.3.0</version>
        </dependency>
    </dependencies>
</plugin>

2。配置您的 checkstyle 检查,例如:

  <module name="TreeWalker">
    <module name="MethodParameterAlignment"/>
    <module name="MethodParameterLinesCheck">
      <property name="allowSingleLine" value="false"/>
    </module>
  </module>

通知

lib 包含更多有用的检查。阅读自述文件以获取更多详细信息 - check-tfij-style.

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