给大伙介绍一个开源工具 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 /