java 制表符 宽度不够_有没有办法强制使用制表符而不是Java中的空格?

使用空格而不是缩进的缩进是首选的,因为它提供了所有编辑器/查看器之间的布局一致性.

但是,如果你仍然想要它,你可以随时自己检查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插件也很难.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值