CheckStyle offers to check for consistent use of spaces, but sadly lacks the opposite idea: Force source code to use tabs. Is there some way to add this functionality? It does not have to be CheckStyle, other tools are welcome as well.
Same as this question but for Java.
EDIT
I don't need a code beautifier, since the normal state of the codebase will be all tabs. I just need a tool which can report the presence of alternate indentation. That way I can set up a new continuous build configuration which will fail when spaces are introduced.
The tab character can be inserted by holding the Alt and pressing 0 and 9 together.
Java: 4 spaces, tabs must be set at 8 spaces. Both are acceptable. Data from Github suggests that around 10-33% of the Java repositories prefer tabs, and the majority use spaces in different formations, preferring 4 spaces over 2.
They're better than spaces because they turn “number of spaces per indent” into text text editor setting. One tab == one indent == one keypress in any editor.
Although Checkstyle has no builtin check for this, you can enforce tabs-only indentation using the RegexpSinglelineJava
check. Note that this only checks which character is used for indentation, not for the correct level of indentation.
Shamelessly stolen from the Hibernate OGM source:
<module name="RegexpSinglelineJava">
<property name="format" value="^\t* +\t*\S"/>
<property name="message" value="Line has leading space characters; indentation should be performed with tabs only."/>
<property name="ignoreComments" value="true"/>
</module>
Using spaces instead of tabs to indent is preferred because it offers consistency of layout across all editors/viewers. But if you still want it, you can always make your own custom check for checkstyle or a custom maven plugin /ant task. Logic shouldnt be difficult to implement either - all you have to check whether leading space on any line is greater than the tab length.
Edit: including an ant example. Its two weeks now since you posted and you're still not happy, and I had some free time :) So I cooked up a little ant custom task solution for you.
The Ant task
public class SpaceDetectorTask extends Task {
public static final String REGEX = "^[ ]+";
public static final Pattern p = Pattern.compile(REGEX);
private FileSet fileSet;
private boolean failOnDetection;
// Usual getters/setters
public void addFileSet(FileSet fileSet) {
this.fileSet = fileSet;
}
public void execute() {
DirectoryScanner ds = fileSet.getDirectoryScanner();
String[] files = ds.getIncludedFiles();
for (int x = 0; x <= files.length -1; x++) {
process(ds.getBasedir(), files[x]);
}
}
public void process(File dir, String file) {
try {
BufferedReader reader = new BufferedReader(new FileReader(new File(dir, file)));
String line;
int linecount = 0;
System.out.println("File: " + file);
boolean ignore = false;
while((line = reader.readLine()) != null) {
linecount++;
// exclude comment blocks
if (line.contains("/**") || line.contains("*/")) {
ignore = !ignore;
continue;
}
if (!ignore) {
if (p.matcher(line).find()) {
int spcCount = line.length() - (line.replaceAll(REGEX, "")).length();
if (spcCount >= 4) { // break whenever 4 leading spaces are detected. Configure as you need.
String msg = "File: "+ file + " is using spaces as indentation.";
if (failOnDetection) {
throw new BuildException(msg);
} else {
getProject().log(msg);
}
}
}
reader.close();
}
}
} catch (IOException e) {
if (failOnDetection) {
throw new BuildException(e);
} else {
getProject().log("File: " + file + "\n" + e.getMessage());
}
}
}
In ant build.xml
Declare it
<taskdef name="detect-spaces"
classname="com.blah.blah.build.tools.SpaceDetectorTask">
<classpath>
<pathelement path="${dir.classes}"/>
<fileset dir="C:/apache-ant-1.7.1/lib">
<include name="**/*.jar"/>
</fileset>
</classpath>
</taskdef>
use it
<target name="rules.spaces">
<detect-spaces
failOnDetection="true">
<fileset dir="${dir.src.java}">
<include name="**/*.java"/>
</fileset>
</detect-spaces>
</target>
Writing up a maven/checkstyle plugin shoulnt be difficult either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With