Astyle - Qt中配置代码格式化工具(附:最佳教科书格式)

简介

1、Qt中格式化代码工具有好几种,目前博主使用的最多最好用的就是Astyle

2、Windows平台下:可以直接下载带有Astyle.exe文件的压缩包,放到指定的目录即可(博主习惯放在和Qt安装目录同级的目录下)

3、Linux平台下:在Astyle官网下载源码,进行编译安装

下载

1、Astyle官网下载地址:https://sourceforge.net/projects/astyle

2、Astyle官方文档地址:http://astyle.sourceforge.net/astyle.html

3、Astyle风格设置:参考《Google 开源项目 c/c++风格》

安装(Windows平台)

1、将压缩包解压到指定目录下

2、找到Astyle.exe存放目录,例D:\SoftwareRD\Qt\AStyle_3.1_windows\AStyle\bin

安装(Linux平台)

1、将压缩包解压到指定目录下

2、在终端中输入以下3条命令进行编译安装即可

cd ./AStyle
cmake CMakeLists.txt make make install

QtCreator配置Astyle

1、安装插件:打开Qt Create开发工具 -> 菜单栏选择 帮助 -> 选择 关于插件 -> 在弹出窗口中 勾选Beautifier项后面的load -> 重启Qt Create生效

2、配置插件:打开Qt Create开发工具 -> 菜单栏选择 工具 -> 选择 选项 -> General中勾选 Enable auto format on file save(保存文件时自动格式化)

 -> Artistic Style中点选 浏览找到存放Astyle.exe目录并选择Astyle.exe -> 勾选 最下面Use customized style -> 点选 最右边Add(添加自定义格式化代码风格)

:下图是博主使用的格式化代码的风格Google(注释的部分博主还没有验证可行性)(王牌在后面)

:Astyle最佳教科书格式

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

简化形式

astyle *.cpp *.h -r -A1 -c -s4 -xV -S -N -xt4 -xW -w -xw -Y -p -U -xe -k3 -W3 -e -j -xg -J

详细说明

--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 <windows.h>
#ifndef NO_EXPORT
#define EXPORT
#endif
#endif

--------------------becomes
#ifdef _WIN32
    #include <windows.h>
    #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;

--------------------becomes(with --align-pointer=middle)
char * foo1;
char & foo2;
string ^ s1;

--------------------becomes(with --align-pointer=name)
char *foo1;
char &foo2;
string ^s1;

引用符号紧贴哪

--------------------becomes(with --align-reference=type)
char& foo1;

--------------------becomes(with --align-reference=middle)
char & foo2;

--------------------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 遍历目录,文件名要指定为带通配符(*)的名字,含有空格的文件名要加引号

 

posted @ 2021-08-10 11:17  Citrusliu  阅读(3503)  评论(0编辑  收藏  举报