astyle 配置代码格式化(Source Insight / VSCode)

代码格式化工具:官网   下载地址

配置 Source Insight

添加 Astyle 命令

Tools --> Custom Commands -->Add

 在 Run 栏填入格式化命令:

"D:\Program Files\astyle-3.5.2-x64\astyle.exe" -A3 -t -xV -w -Y -m0 -p -H -U --squeeze-lines=1 --squeeze-ws -k3 -W3 -n %f

 添加 Astyle 快捷键

Tools --> Custom Commands --> Keys --> Assign New Key

 vscode 配置

安装 astyle

配置文件

在设置中搜索 @ext:chiehyu.vscode-astyle

然后编辑配置文件加入如下内容:

"astyle.cmd_options":[
        /*"-A3 -t -xV -Y -m0 -p -H -U --squeeze-lines=1 --squeeze-ws -k3 -W3 -n %f",*/
        "--style=kr",
        "--indent=tab",
        "--attach-closing-while",
        "--indent-col1-comments",
        "--min-conditional-indent=0",
        "--pad-oper",
        "--pad-header",
        "--unpad-paren",
        "--align-pointer=name",
        "--align-reference=name",
        "--suffix=none",
        "--squeeze-lines=1",
        "--squeeze-ws",
    ],
    "[c]": {
        "editor.defaultFormatter": "chiehyu.vscode-astyle"
    }

Astyle 参数说明

--style 设置代码风格

所有风格见:Artistic Style
 

Allman 风格

--style=allman / --style=bsd / --style=break / -A1

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

gnu 风格

--style=gnu / -A7

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

Linux 风格

--style=linux / --style=knf / -A8

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

google 风格

--style=google / -A14

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

 代码宽度设置

--max-code-length=#   / -xC#(# 有效值 50 - 200)
--break-after-logical     / -xL

将

if (thisVariable1 == thatVariable1 || thisVariable2 == thatVariable2 || thisVariable3 == thatVariable3)
    bar();

格式化为

if (thisVariable1 == thatVariable1
        || thisVariable2 == thatVariable2
        || thisVariable3 == thatVariable3)
    bar();

加上 break-after-logical 后格式化为

if (thisVariable1 == thatVariable1 ||
        thisVariable2 == thatVariable2 ||
        thisVariable3 == thatVariable3)
    bar();

缩进设置

--indent=spaces / --indent=spaces=# / -s#

with indent=spaces=3

每次缩进使用 # 个空格进行缩进。2 <= # <=20。不设置 # 时默认为 4

void Foo() {
...if (isBar1
.........&& isBar2)
......bar();
}

--indent=tab / --indent=tab=# / -t / -t#

缩进使用制表符进行缩进,使用空格进行连续行对齐。这可以确保无论查看者的标签大小如何,代码都能正确显示。将每个缩进视为 # 个空格

with indent=tab:

void Foo() {
>   if (isBar1
>   ........&& isBar2)    // indent of this line can be changed with min-conditional-indent
>   >   bar();
}
with style=linux, indent=tab=8:

void Foo()
{
>       if (isBar1
>       ....&& isBar2)    // indent of this line can NOT be changed with style=linux
>       >       bar();
}

--indent=force-tab / --indent=force-tab=# / -T / -T#

如果可能的话,全部使用制表符缩进。如果连续行不是偶数个制表符,则会在末尾添加空格。将每个制表符视为 # 个空格

indent=force-tab:

void Foo() {
>   if (isBar1
>   >   >   && isBar2)    // indent of this line can be changed with min-conditional-indent
>   >   bar();
}

do - while 设置

--attach-closing-while / -xV

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

格式化为:

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

 switch 缩进

--indent-switches / -S

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

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

格式化为:

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

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

case 缩进

--indent-cases / -K

将

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

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

格式化为:

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

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

命名空间缩进

--indent-namespaces / -N

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

格式化为

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

标签缩进

--indent-labels / -L

将

void Foo() {
    while (isFoo) {
        if (isFoo)
            goto error;
        ...
error:
        ...
        }
}

格式化为 (error 标签缩进,默认向左):

void Foo() {
    while (isFoo) {
        if (isFoo)
            goto error;
        ...
    error:
        ...
        }
}

预处理块缩进

--indent-preproc-block / -xW

将

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

格式化为

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

将预处理器条件语句缩进到与源代码相同的级别

--indent-preproc-cond / -xw 

将

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

格式化为

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

宏定义块缩进

--indent-preproc-define / -w

将

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

格式化为

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

注释缩进

--indent-col1-comments / -Y

将

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

格式化为

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

多行缩进

--min-conditional-indent=# / -m#

0 - 无最小缩进。这些行将与前一行的 paren 对齐
1 - 至少缩进一个额外的缩进
2 - 至少缩进另外两个缩进
3 - 至少缩进二分之一的额外缩进

例如 --min-conditional-indent=0 / -m0

将

if (a < b
        || c > d)
{
    foo++;
}

格式化为

if (a < b
    || c > d)
{
    foo++;
}

连续行缩进

--max-continuation-indent=# / -M#

# 表示列数,不得少于 40 或 大于 120。默认为 40

将

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

fooFunction(barArg1,
         barArg2,
         barArg3);

格式化为

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

fooFunction(barArg1,
            barArg2,
            barArg3);

空行设置

--break-blocks / -f

在 if for while .. 等代码块前后加上空行

将

isFoo = true;
if (isFoo) {
    bar();
} else {
    anotherBar();
}
isBar = false;

格式化为

isFoo = true;

if (isFoo) {
    bar();
} else {
    anotherBar();
}

isBar = false;

--break-blocks=all / -F

在 if for while .. 等代码块前后加上空行,同时在 'else', 'catch' 前加上空行

将

isFoo = true;
if (isFoo) {
    bar();
} else {
    anotherBar();
}
isBar = false;

格式化为

isFoo = true;

if (isFoo) {
    bar();

} else {
    anotherBar();
}

isBar = false;

运算符前后加空格

--pad-oper / -p

将

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

格式化为

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

逗号后加空格

--pad-comma / -xg (设置了 --pad-oper / -p 就不需要此设置)

将

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

格式化为

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

在 ! 后加空格

--pad-negation

将

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

格式化为

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

在 ! 前加上空格

--pad-negation=before

将

if (!isFoo(a,b))
    bar(a,b);
 
格式化为

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

 关键字/函数名和括号间加空格

--pad-header / -H

将

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

格式化为

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

删除多余空格

--unpad-paren / -U

将

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

格式化为

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

 --squeeze-ws

将

void       Foo    ()
{
    foo1   =   1;
}
    
格式化为

void Foo ()
{
    foo1 = 1;
}

删除函数或方法中的空行

--delete-empty-lines / -xe

将

void Foo()
{

    foo1 = 1;

    foo2 = 2;

}

格式化为

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

删除超过给定数字的多余空行

--squeeze-lines=#

将

void Foo()
{
    foo1 = 1;
}




void Foo2()
{
    foo1 = 1;
}
    
格式化为

void Foo()
{
    foo1 = 1;
}


void Foo2()
{
    foo1 = 1;
}

设置指针或引用运算符的位置

--align-pointer=type   / -k1
--align-pointer=middle / -k2
--align-pointer=name   / -k3

align-pointer=type

将

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

格式化为

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

/
align-pointer=middle

将

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

格式化为

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

/
align-pointer=name

将

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

格式化为

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

引用与指针分开对齐

--align-reference=none   / -W0
--align-reference=type   / -W1
--align-reference=middle / -W2
--align-reference=name   / -W3


align-reference=type
将

char &foo1;

格式化为

char& foo1;

///
align-reference=middle

将

char& foo2;

格式化为

char & foo2;

///
align-reference=name

将

char& foo3;

格式化为

char &foo3;

else 块设置

--break-closing-braces / -y (与 --style=java, --style=kr, --style=stroustrup, --style=linux, or --style=1tbs 一起使用时有效)

将

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

格式化为

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

else if 块设置

--break-elseifs / -e

将

if (isFoo) {
    bar();
}
else if (isFoo1()) {
    bar1();
}
else if (isFoo2()) {
    bar2();
}

格式化为

if (isFoo) {
    bar();
}
else
    if (isFoo1()) {
        bar1();
    }
    else
        if (isFoo2()) {
            bar2();
        }

单行代码添加大括号

--add-braces / -j

将

if (isFoo)
    isFoo = false;

格式化为

if (isFoo) {
    isFoo = false;
}

单行代码删除大括号

--remove-braces / -xj

将

if (isFoo)
{
    isFoo = false;
}

格式化为

if (isFoo)
    isFoo = false;

返回类型单独一行

--break-return-type     / -xB
--break-return-type-decl  / -xD

将

void Foo(bool isFoo);

格式化为

void
Foo(bool isFoo);

返回类型和函数名在一行

--attach-return-type    / -xf
--attach-return-type-decl / -xh

将

void
Foo(bool isFoo);

格式化为

void Foo(bool isFoo);

删除注释块中多余的 *

--remove-comment-prefix / -xp

将

/*
 * comment line 1
 * comment line 2
 */

格式化为

/*
    comment line 1
    comment line 2
*/

其他选项

备份文件后缀

--suffix=####

不备份原始文件

--suffix=none / -n

排除文件夹

--exclude=####

  • 23
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值