如何统一
使用统一的astyle配置:
astyle --style=ansi --indent=spaces=4 -k1 -W1 -p -j -H -c -U -w -S -M -z2 -n
下面详细解释一下各个参数的含义:
1、指针对齐,我们选择k1。
--align-pointer=type OR -k1 比如char* p
--align-pointer=middle OR -k2 比如char * p
--align-pointer=name OR -k3 比如char *p
2、引用对齐,我们选择W1。
--align-reference=none OR -W0
--align-reference=type OR -W1 比如int& p
--align-reference=middle OR -W2 比如int & p
--align-reference=name OR -W3 比如int &p
3、-p 在操作符两边插入空格,如=、+、-等。
4、-j 给每个”if”、”for”、“while”增加大括号。
5、-H Insert space padding after paren headers (e.g. ‘if’, ‘for’…)。
6、-c Convert tabs to the appropriate number of spaces。
7、-U 移除括号两边不必要的空格。
如:System.out.println( 1 );
处理后变成System.out.println(1);
8、-w 格式化多行的宏定义。
9、-S switch中case关键字,一个tab的缩进。效果如下:
switch (nMsgID)
{
case MSG_FROM_DEVICE_REPORT_STATUS:
{
GetMsgControlInstance()->OnDeviceReportStatusMsg(pNormalMsg, conn);
}
break;
}
10、-M: 对定义的参数和变量进行对齐。
11、-z2 换行符是Linux风格的。
12、-n :不备份源文件。
下载路径
linux版链接:https://pan.baidu.com/s/1s37HZ6ZhxDv9G1vloXb13w
提取码:8u1n
windows链接:https://pan.baidu.com/s/1Iob3TD98yv9OSPWVVcv-Qg
提取码:2ayi
如何使用
命令行方式
下载解压后,将Astyle/bin的路径加入到windows的path环境变量中。
使用批量脚本将代码格式化:
for /R %f in (*.cpp;*.h;*.c;*.cc) do astyle --style=ansi --indent=spaces=4 -k1 -W1 -p -j -H -c -U -w -S -M -z2 -n "%f"
加入到VS2008,VS2005
- 工具——>外部工具——>添加
- 标题:astyle
- 命令:AStyle.exe (填好astyle.exe的路径)
- 参数:–style=ansi –indent=spaces=4 -k1 -W1 -p -j -H -c -U -w -S -M -z2 -n $(ItemDir)$(ItemFileName)$(ItemExt)
- 初始目录:$(TargetDir)
- 勾上“使用初始目录”
- 点击确定完成,以后就可以在工具菜单中找到“astyle“这一项了,点击它,就可以对当前文件进行格式化操作。
加入到SourceInsight
- Options–>Custom Command–>Add
- Command:astyle
- Run “D:\soft\astyle\astyle.exe” –style=ansi –indent=spaces=4 -k1 -W1 -p -j -H -c -U -w -S -M -z2 -n %f(填好astyle.exe的路径)
- Output:不选.
- Control: 选择pause when done和exit to window.
- source links in output:file, then line
- –>menu
- add to work menu.
- 点击确定完成。以后就可以在Work菜单中找到“astyle“这一项了,点击它,就可以对当前文件进行格式化操作。