AStyle说明

集成进source insight:
1:下载AStyle(开放源码),见附件;
2:此软件为绿色软件无需安装,压缩后,在SourceInsight菜单栏里,Options-->Custom Commands界面上选择:Add,在弹出对话框写入 Astyle,
3:找到你的解压路径下的bin目录下的astyle.exe,如果你的路径为C:\Program Files\astyle\bin\astyle.exe,
在run中添加"C:\Program Files\astyle\bin\astyle.exe" --style=ansi %f --indent=spaces=4,其中,如果astyle.exe所在路径中有空格,必须用""括起来,参数--style=ansi 代表ansi C 格式(如果你需要格式化java代码,这个地方改为:--style=java),--indent=spaces=4,是使用4空格缩进风格,"%f"是指作用于当前文件,这个必须的.其它命令参数后面提到.
4:此外,在此界面上还可以为这个命令设置快捷键,点击"keys",添加你觉得方便的按钮;
5:也可以添加到工具栏中,在SourceInsight菜单栏里,Options-->Menu Assignments界面上,将这个命令名称为Astyle添加到某工具栏下,我是依然放在了Option下,在左面的Command列表里找到我们刚才添加的"Astyle",在右面的Menu中选择你要加到那个菜单下,这里我加到"Option"下,可以在"Menu Contents"选择适当位置,点击"insert"即可;



astyle基本功能介绍:
astyle是一个命令行工具,命令语法很简单:
astyle [options] < original > Beautified
astyle [options] Foo.cpp Bar.cpp [...]

例如:

astyle --style=ansi foo.cpp

上面的命令将美化foo.cpp文件,更改其风格为ANSI,并将原始文件备份到https://www.360docs.net/doc/cf15902321.html,in。所以,你可以安全的使用该软件而不必担心会将代码改得无法回头。

具体的来说,astyle包含了以下几种预定义风格,只需在参数中简单指定即可使用:

--style=ansi:ANSI 风格格式和缩进

namespace foospace
{
 int Foo()
 {
if (isBar)
{
bar();
return 1;
}
else
return 0;
 }
}

--style=kr :Kernighan&Ritchie 风格格式和缩进

namespace foospace {
 int Foo() {
if (isBar) {
bar();
return 1;
} else
return 0;
 }
}

--style=linux :Linux 风格格式和缩进

namespace foospace
{
 int Foo()
 {
if (isBar) {
bar();
return 1;
} else
return 0;
 }
}

--style=gnu :GNU 风格格式和缩进

namespace foospace
{
 int Foo()
 {
if (isBar)
{
bar();
return 1;
}
else
return 0;
 }
}

--style=java :Java 风格格式和缩进

class foospace {
 int Foo() {
if (isBar) {
bar();
return 1;
} else
return 0;
 }
}


1.常用功能
(1) 单个文件--缺省美化
astyle --style=ansi Form1.cs
处理前的代码


private void Form1_Load(object sender, EventArgs e)
{
int s;
for (int i=0;i<10;i++){
for (int j=0;j<10; j++){
s = s+j+i;}
}
}
处理后:
private void Form1_Load(object sender, EventArgs e)
{
int s;
for (int i=0;i<10;i++)
{
for (int j=0;j<10; j++)
{
s = s+j+i;
}
}
}

(2) 单个文件--更改缩进2个空格
astyle --style=ansi --indent=spaces=2 Form1.cs
缺省缩进一个TAB,也可以显式说明使用Tab,如下:
astyle --style=ansi --indent=tab Form1.cs

(3) 处理多个文件--有限个
astyle --style=ansi Form1.cs Form2.cs

(4) 批量处理多个文件--无限个
for /R .\ %f in (*.cs) do astyle --style=ansi "%f"
说明:/R表明遍历一个目录树,后面紧跟的路径是根,缺省为当前目录。
本例中,根为.\表示当前目录,命令等价于:
for /R %f in (*.cs) do astyle --style=ansi "%f"
作用是从(目录树根)当前目录开始,查找所有java文件,包含子目录中的文件;然后交给astyle处理。
当然,目录树根也可以使用绝对路径,下面的命令查找C盘所有的java文件并处理。
for /R c:\ %f in (*.cs) do astyle --style=ansi "%f"

2. 其他比较有用的开关:
(1) -f
在两行不相关的代码之间插入空行,如import和public class之间、public class和成员之间等;
(2) -p
在操作符两边插入空格,如=、+、-等。
如:int a=10*60;
处理后变成int a = 10 * 60;
(3) -P
在括号两边插入空格。另,-d只在括号外面插入空格,-D只在里面插入。
如:MessageBox.Show ("aaa");
处理后变成MessageBox.Show ( "aaa" );
(4) -U
移除括号两边不必要的空格。
如:MessageBox.Show ( "aaa" );
处理后变成MessageBox.Show ("aaa");
(5) -V
将Tab替换为空格。

下面再介绍独门绝技:批量格式化!

有时候你会有很多文件需要格式化成统一风格,难道一个个点击菜单?不!那样太累了。

在Windows中,我们可以用命令行来解决问题。这里用到一个超级命令 for

我来写个范例,大家就知道该怎么处理了。

for /R %f in (*.cpp;*.c;*.h) do astyle --style=ansi "%f"

该命令在当前目录中寻找文件名匹配模式 *.cpp;*.c;*.h 的所有文件(不同模式可用英文逗号隔开),并且对每个文件%f执行操作:

astyle --style=ansi "%f"

相关文档
最新文档