Astyle格式化代码-最好的选项

这里的源代码本来是一片,后来变成了一大片,经过几代人的手之后,成了原始森林,各种风格,各种旁逸斜出.不敢直视.一个switch语句几千万行,一眼望不到边.今天突然有种冲动,想整一下.然后就找到了Astyle(Artistic Style) 
查看了官方文档 http://astyle.sourceforge.net/astyle.html
配置了最佳的教科书格式
 

astyle *.c *.cpp *.h --recursive  --style=bsd  --convert-tabs --indent=spaces=8 --attach-closing-while  --indent-switches  --indent-namespaces --indent-continuation=4 --indent-preproc-block --indent-preproc-define --indent-preproc-cond --indent-col1-comments --pad-oper   --pad-paren-in   --unpad-paren  --delete-empty-lines --align-pointer=name    --align-reference=name --break-elseifs  --add-braces 

--indent-switches 缩进case标签
 

switch (foo)
{
case 1:
    a += 1;
    break;

case 2:
{
    a += 2;
    break;
}
}

becomes:

switch (foo)
{
    case 1:
        a += 1;
        break;

    case 2:
    {
        a += 2;
        break;
    }
}

--indent=spaces=8  缩进8个空格

void Foo() 
{
........bar();
}

 

--indent-namespaces 缩进命名空间块

namespace foospace
{
class Foo
{
    public:
        Foo();
        virtual ~Foo();
};
}

becomes:

namespace foospace
{
    class Foo
    {
        public:
            Foo();
            virtual ~Foo();
    };
}

--indent-continuation=4 等号=或(结尾后续本语句符号插入空格,默认为1,可取1~4

isLongVariable =
    foo1 ||
    foo2;

isLongFunction(
    bar1,
    bar2);

becomes (with indent-continuation=3):

isLongVariable =
            foo1 ||
            foo2;

isLongFunction(
            bar1,
            bar2);


--style=bsd 大括号独占一行,上下对齐

 

 

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

 

 

--attach-closing-while (while紧贴)

do
{
    bar();
    ++x;
}
while x == 1;

becomes:

do
{
    bar();
    ++x;
} while x == 1;

 

--indent-preproc-block 缩进#开头的处理语句

#ifdef _WIN32
#include 
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif

becomes:

#ifdef _WIN32
    #include 
    #ifndef NO_EXPORT
        #define EXPORT
    #endif
#endif

--indent-preproc-cond  预处理语句也缩进
 

        isFoo = true;
#ifdef UNICODE
        text = wideBuff;
#else
        text = buff;
#endif

becomes:

        isFoo = true;
        #ifdef UNICODE
        text = wideBuff;
        #else
        text = buff;
        #endif

--indent-col1-comments 注释也缩进

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

becomes:

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

 

--pad-oper 操作符间插入空格
 

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

becomes:

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

 

--pad-comma 逗号间插入空格(--pad-oper中已有此效果)
 

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

becomes:

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

 

--pad-paren-in 括号里内插入空格
 

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

becomes:

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

--unpad-paren 紧凑括号内外

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

becomes (with no padding option requested):

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

 

--delete-empty-lines 清除函数间的空行
 

void Foo()
{

    foo1 = 1;

    foo2 = 2;

}

becomes:

void Foo()
{
    foo1 = 1;
    foo2 = 2;
}

  指针符号紧贴哪

char* foo1;
char & foo2;
string ^s1;

becomes (with --align-pointer=type):

char* foo1;
char& foo2;
string^ s1;
char* foo1;
char & foo2;
string ^s1;

becomes (with --align-pointer=middle):

char * foo1;
char & foo2;
string ^ s1;
char* foo1;
char & foo2;
string ^s1;

becomes (with --align-pointer=name):

char *foo1;
char &foo2;
string ^s1;

//引用符号紧贴哪
char &foo1;

becomes (with --align-reference=type):

char& foo1;
char& foo2;

becomes (with --align-reference=middle):

char & foo2;
char& foo3;

becomes (with --align-reference=name):

char &foo3;

 

char &foo1;

becomes (with --align-reference=type):

char& foo1;
char& foo2;

becomes (with --align-reference=middle):

char & foo2;
char& foo3;

becomes (with --align-reference=name):

char &foo3;

 

--attach-return-type-decl 返回类型紧贴符号名
 

void
Foo(bool isFoo);

becomes:

void Foo(bool isFoo);

--add-braces 在'if', 'for', 'while'等句块中只有一行也加入大括号

if (isFoo)
    isFoo = false;

becomes:

if (isFoo) {
    isFoo = false;
}

 

--convert-tabs 将TAB符转化成空格,由转化参数指定,引号内的不转化
 

--recursive 遍历目录,文件名要指定为带通配符(*)的名字,含有空格的文件名要加引号
 

    

你可能感兴趣的:(文本的力量)