Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to require tab-indentation with CheckStyle?

In CheckStyle there is a module (File Tab Character) that checks that there are not tab-characters in the source code. Their rationale is:

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

To ensure there is the correct number of spaces, there is an additional module (Indentation).

I prefer using tabs for indentation and want to add this requirement to my CheckStyle-file. My rationale:

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

Unfortunately I could not find a way to do this with CheckStyle.

like image 317
Edward Avatar asked Feb 11 '15 16:02

Edward


1 Answers

There is no ready-made check which does this, but you can configure the RegexpSinglelineJava check accordingly. The following configuration goes into your checkstyle.xml under TreeWalker:

<module name="RegexpSinglelineJava">
    <property name="format" value="^\t* "/>
    <property name="message" value="Indent must use tab characters"/>
    <property name="ignoreComments" value="true"/>
</module>

It checks if there are spaces in the indent. Setting ignoreComments eliminates the problem that Javadoc comments often have at least one space before the asterisk.

like image 125
barfuin Avatar answered Sep 28 '22 05:09

barfuin