介绍一个 C/C++ 、C#、JAVA 代码美化工具

来源:互联网 发布:域名怎么防腾讯拦截 编辑:程序博客网 时间:2024/05/17 10:25

给大伙介绍一个开源工具 AStyle,支持C/C++、C#、JAVA等语言,按照不同的风格格式化代码,也可以称为美化代码。
用这个工具美化出来的代码与我自已的风格几乎一样,我很喜欢这个小工具,也可以很方便的集成到VS里面去。

用法:

AStyle.exe --style=ansi -t -M80 -k1 -p -j -H -c -w -n -U -K -Y -xW test.cpp


--style=ansi ansi C风格代码

void Foo(bool isFoo)
{
    if (isFoo)
    {
        bar();
    }
    else
    {
        anotherBar();
    }
}

--indent=tab / --indent=tab=# /-t / -t#  缩进用tab,不用空格,#为数字,默认为4

 

--indent-preproc-block /-xW
Indent preprocessor blocks at bracket level zero, and immediately within a namespace. There are restrictions on what will be indented. Blocks within methods, classes, arrays, etc, will not be indented. Blocks containing brackets or multi-line define statements will not be indented. Without this option the preprocessor block is not indented.

#ifdef _WIN32#include <windows.h>#ifndef NO_EXPORT#define EXPORT#endif#endif

becomes:

#ifdef _WIN32    #include <windows.h>    #ifndef NO_EXPORT        #define EXPORT    #endif#endif

--indent-preproc-define /-w
Indent multi-line preprocessor definitions ending with a backslash. Should be used with --convert-tabs for proper results. Does a pretty good job, but cannot perform miracles in obfuscated preprocessor definitions. Without this option the preprocessor statements remain unchanged.

#define Is_Bar(arg,a,b) \(Is_Foo((arg), (a)) \|| Is_Foo((arg), (b)))

becomes:

#define Is_Bar(arg,a,b) \    (Is_Foo((arg), (a)) \     || Is_Foo((arg), (b)))


--indent-col1-comments /-Y
Indent C++ comments beginning in column one. By default C++ comments beginning in column one are assumed to be commented‑out code and not indented. This option will allow the comments to be indented with the code.

void Foo()\n"{// comment    if (isFoo)        bar();}

becomes:

void Foo()\n"{    // comment    if (isFoo)        bar();}

--keep-one-line-blocks /-O 
Don't break one-line blocks.

if (isFoo){ isFoo = false; cout << isFoo << endl; }

remains unchanged.

 


-p  在运算符号左右加上空格

if (foo==2)
    a=bar((b-c)*a,d--);
becomes:

if (foo == 2)
     a = bar((b - c) * a, d--);
 

-j   给每个if增加大括号(#add如何将{换行而不是加在行尾?--style=ansi已经做到了)

if (isFoo)
    isFoo = false;
becomes:

if (isFoo) {
    isFoo = false;
}
 

-H 在c/c++ 关键字的后面增加一个空格

if(isFoo(a, b))
    bar(a, b);

becomes:

if (isFoo(a, b))
    bar(a, b);

 -c

把TAB字符替换成空格为什么要把TAB替换成空格?主要是因为各种不同的编辑器对TAB的解释不一样造成的。

有的编辑器把TAB解释成4个空格,有的是8个,还有的是2个。这样会破坏原有的代码的对齐的结构,

因此最好把TAB替换成空格,这样不管在任何编辑器下面代码都是以对齐的风格来显示。

-k1(-k2, -k3) --align-pointer=type

char *foo1;
char &foo2;

becomes (with align-pointer=type):

char* foo1;
char& foo2;

-M# 对定义的参数和变量进行对齐

fooArray[] = { red,
         green,
         blue };

fooFunction(barArg1,
         barArg2,
         barArg3);
becomes (with larger value):

fooArray[] = { red,
               green,
               blue };

fooFunction(barArg1,
            barArg2,
            barArg3);


-U

if ( isFoo( a, b ) )    bar ( a, b );

becomes (with no padding option requested):

if(isFoo(a, b))    bar(a, b);

 -n 不生成备份文件,即默认的 .orig文件。

 -N  namespace 缩进


看到上面这么多的美化代码的参数,真的很hi。

--style=ansi -t -M80 -k1 -p -j -H -c -w -n -U -K -Y -xW $(ItemDir)$(ItemFileName)$(ItemExt)

(#add 适合自己的.cpp)

-O -t -M80 -k1 -p -j -H -c -w -n -U -K -Y -xW $(ItemDir)$(ItemFileName)$(ItemExt)

(#add 适合自己的.h,区别是大括号不换行)


如何集成到VS当中呢?

1. 把AStyle.exe 拷到 “C:\Program Files\Microsoft Visual Studio 9.0\Common7\Tools\”目录下

2. Tools –> External Tools

如图增加一个外部工具配置,初始目录:如果$(ProjectDir)不行就试 $(TargetDir)

0927_1

 

在Tools菜单下面会多出一个Beautify点击它就可以来美化当前的文件 如下图:

 

0927_2

 

总体来说这个工具美化的效果还是很不错的,喜欢的人大家都可以试试。

官方网站: http://astyle.sourceforge.net/

官方下载:http://nchc.dl.sourceforge.net/project/astyle/astyle/astyle%202.02/AStyle_2.02_windows.zip


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

(3) 处理多个文件--有限个
astyle --style=java Test.java T.java


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



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

for /R %%f in (*.cpp;*.c;*.h) do astyle --style=ansi "%%f" //批处理中要这么写%%才能表示一个%

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

       astyle --style=ansi "%f"


 Example to format a single file:

astyle  --style=allman  /home/user/project/foo.cpp

  Example to format all .cpp and .h files recursively:

astyle  --style=allman --recursive  /home/user/project/*.cpp  /home/user/project/*.h