如何使用 CheckStyle 要求制表符缩进?
How to require tab-indentation with CheckStyle?
在 CheckStyle 中有一个模块 (File Tab Character
) 检查源代码中是否没有制表符。他们的理由是:
- Developers should not need to configure the tab width of their text editors in order to be able to read source code.
- From the Apache jakarta coding standards: In a distributed development environment, when the commit messages get sent to a mailing list, they are almost impossible to read if you use tabs.
为确保空格数正确,还有一个附加模块 (Indentation
)。
我更喜欢使用制表符进行缩进,并希望将此要求添加到我的 CheckStyle 文件中。我的理由:
- Developers should have the opportunity to configure the space used for indentation
- Tabs are a logical and configurable unit for indentation,
n spaces
is just an arbitrary number of spaces.
不幸的是,我找不到使用 CheckStyle 执行此操作的方法。
没有现成的检查可以执行此操作,但您可以相应地配置 RegexpSinglelineJava 检查。以下配置进入 checkstyle.xml 下的 TreeWalker
:
<module name="RegexpSinglelineJava">
<property name="format" value="^\t* "/>
<property name="message" value="Indent must use tab characters"/>
<property name="ignoreComments" value="true"/>
</module>
它检查缩进中是否有 space。设置 ignoreComments
消除了 Javadoc 注释经常在星号前至少有一个 space 的问题。
我们还可以配置RegexpSinglelineJava在格式字段中使用以下正则表达式进行检查-
<property name="format" value="^[\t]*[" "]+([\t]*|[" "]+)[a-zA-Z0-9]"/>
这会检查第一个单词之前的所有空格
在 CheckStyle 中有一个模块 (File Tab Character
) 检查源代码中是否没有制表符。他们的理由是:
- Developers should not need to configure the tab width of their text editors in order to be able to read source code.
- From the Apache jakarta coding standards: In a distributed development environment, when the commit messages get sent to a mailing list, they are almost impossible to read if you use tabs.
为确保空格数正确,还有一个附加模块 (Indentation
)。
我更喜欢使用制表符进行缩进,并希望将此要求添加到我的 CheckStyle 文件中。我的理由:
- Developers should have the opportunity to configure the space used for indentation
- Tabs are a logical and configurable unit for indentation,
n spaces
is just an arbitrary number of spaces.
不幸的是,我找不到使用 CheckStyle 执行此操作的方法。
没有现成的检查可以执行此操作,但您可以相应地配置 RegexpSinglelineJava 检查。以下配置进入 checkstyle.xml 下的 TreeWalker
:
<module name="RegexpSinglelineJava">
<property name="format" value="^\t* "/>
<property name="message" value="Indent must use tab characters"/>
<property name="ignoreComments" value="true"/>
</module>
它检查缩进中是否有 space。设置 ignoreComments
消除了 Javadoc 注释经常在星号前至少有一个 space 的问题。
我们还可以配置RegexpSinglelineJava在格式字段中使用以下正则表达式进行检查-
<property name="format" value="^[\t]*[" "]+([\t]*|[" "]+)[a-zA-Z0-9]"/>
这会检查第一个单词之前的所有空格