使用空格而不是缩进的缩进是首选的,因为它提供了所有编辑器/查看器之间的布局一致性.
但是,如果你仍然想要它,你可以随时自己检查checkstyle或一个自定义的maven插件/蚂蚁任务.
逻辑不应该很难实现 – 所有你必须检查任何行上的前导空间是否大于标签长度.
编辑:包括一个蚂蚁的例子.
自从你发布以来,现在已经有两个星期了,你还不开心,我有空闲时间:)
所以我为你制定了一个蚂蚁定制任务解决方案.
蚂蚁任务
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());
}
}
}
在ant build.xml中
>先编译任务
>声明它
classname="com.blah.blah.build.tools.SpaceDetectorTask">
>使用它
failOnDetection="true">
编写一个maven / checkstyle插件也很难.