Astyle Linux编码风格配置

本文介绍如何安装Astyle代码格式化工具,并将其嵌入到SourceInsight 4.0环境中。此外,还详细说明了Astyle针对Linux编码风格的各种配置选项,包括大括号风格、缩进风格、操作符间空格等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、安装Astyle

1.1 下载Astyle

下载地址:  Artistic Style download | SourceForge.net

1.2 安装Astyle

Astyle是免安装的,只需将下载下来的zip文件解压到想安装的目录即可,如: F:\software\AStyle

二、将Astyle嵌入到Source Insight 4.0

1) 点击Source Insight菜单栏Tools–>Custom Commands...

2) 点击Add...添加新的命令,在弹出对话框写入命令名如Astyle

3) 在Run输入框中添加相应的命令。

"F:\software\AStyle\bin\Astyle.exe" --style=linux -t4 -xg -p -H -Y -k3 --suffix=.bak %f

 

4) 给Astyle添加快捷键

4.1) 1) 点击Source Insight菜单栏Options–>Key Assignments...

4.2) 在KeyAssignment Command输入框住输入Astyle找到刚刚添加的Astyle命令,然后点击Assign New Key...,然后在键盘上输入想设置的按键,如Ctrl+Alt+F,设置好后点击OK。后面使用Ctrl+Alt+F组合键就可以快速格式化代码。

三、Astyle Linux编码风格配置

Astyle官方文档详细介绍了Astyle选项参数的使用方法,详情参考官方文档: Artistic Style

1. 大括号风格

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

Linux style uses linux braces. Opening braces are broken from namespace, class, and function definitions. Also known as Kernel Normal Form (KNF) style, this is the style used in the Linux BSD kernel.

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

2. 缩进风格

使用制表符Tabs进行进行缩进,使用空格进行续行对齐。将每个缩进视为 # 个空格(例如 -t4 / --indent=tab=4)。 # 必须介于 2 和 20 之间。如果未设置 #,则将缩进视为 4 个空格。

--indent=tab / --indent=tab=4 / -t / -t4

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();
}

3. switch-case缩进

Linux switch和case对齐不缩进,如果要缩进使用--indent-switches / -S

4. 操作符间插入空格

--pad-oper / -p
if (foo==2)
    a=bar((b-c)*a,d--);

becomes:

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

5. 在运算符周围插入空格

--pad-oper / -p
if (foo==2)
    a=bar((b-c)*a,d--);

becomes:

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

6. 在if.for,while等条件或循环语句关键字和括号之间插入空格

--pad-header / -H
if(isFoo((a+2), b))
    bar(a, b);

becomes:

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

7. 指针贴近变量名

--align-pointer=name   / -k3
char* foo1;
char & foo2;
string ^s1;

becomes (with align-pointer=name):

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

8. 注释和代码一起缩进

--indent-col1-comments / -Y
void Foo()\n"
{
// comment
    if (isFoo)
        bar();
}

becomes:

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

9. 原始文件名后缀设置

--suffix=####

将后缀 #### 而不是 '.orig' 附加到原始文件名(例如 --suffix=.bak,如果这是文件扩展名,则必须包含点 '.' ,  否则将附加后缀 到当前文件扩展名)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值