Source Insight没有格式化代码的快捷键,看到各种缩进不统一的代码实在眼疼
发现可以SI里可以自定义指令来使用外部工具来格式化代码,非常方便
这里使用的是Astyle,一个开源的工具。可以使用AStyle对Java文件进行格式化
下载链接 https://sourceforge.net/projects/astyle/
解压到任意盘符,我解压在E盘,最后Astyle的exe路径为E:\AStyle\bin\AStyle.exe
一般格式化java文件的指令是
AStyle.exe –style=java –indent=spaces=4 -p -f -v %f(文件名)
点击Tools -> Custom Commands -> Add..
将命令取名为formatter -> 点击ok
在Run里填入“E:\AStyle\bin\AStyle.exe” –style=java –indent=spaces=4 -p -f -v %f
点击右下角的Keys… -> Assign New Key… -> 然后选择一个快捷键,例如Ctrl+Alt+L
点击OK完成,以后在查看代码的界面,按Ctrl+Alt+L就可以进行格式化了
这里是基于Source Insight 4.0版本的,其他版本可能有些选项不一样
以下是使用到的Astyle的参数介绍
–style=java / –style=attach / -A2
Java style uses attached braces.
java样式,将左花括号放在上一条语句后面
int Foo(bool isBar) {
if (isBar) {
bar();
return 1;
} else
return 0;
}
–indent=spaces / –indent=spaces=# / -s#
If no indentation option is set, the default option of 4 spaces will be used (e.g. -s4 –indent=spaces=4).
缩进默认是4个空格
–verbose / -v
Verbose display mode. Display optional information, such as release number, date, option file locations, and statistical data.
详细显示模式,提示格式化后的统计数据
1 格式化 0 未改变 0.27 秒 9,628 行
–pad-oper / -p
Insert space padding around operators. This will also pad commas. Any end of line comments will remain in the original column,
if possible. Note that there is no option to unpad. Once padded, they stay padded.
在操作符两边添加空格
if (foo==2)
a=bar((b-c)*a,d--);
becomes:
if (foo == 2)
a = bar((b - c) * a, d--);
–break-blocks / -f
Pad empty lines around header blocks (e.g. ‘if’, ‘for’, ‘while’…).
在一些代码块前后加上空行
isFoo = true;
if (isFoo) {
bar();
} else {
anotherBar();
}
isBar = false;
becomes:
isFoo = true;
if (isFoo) {
bar();
} else {
anotherBar();
}
isBar = false;
更多参数请查阅http://astyle.sourceforge.net/astyle.html#_Quick_Start
本文介绍如何在SourceInsight中使用Astyle工具自定义快捷键格式化Java代码。通过设置可快速调整代码风格,包括缩进、空格等。
2846

被折叠的 条评论
为什么被折叠?



