Astyle格式化C++代码

Astyle格式化C++代码,第1张

1、Astyle简介

AStyle是artistic style的简称,用于格式化程序代码,便于保持统一的编码风格,从而提升代码的可读性和可维护性。

2、Astyle安装 2.1、vscode插件安装

2.2、vistual studio插件安装 
  1. 下载AStyleExtension2019.vsix;

  2. 双击AStyleExtension2019.vsix,选择相应的VS安装版本;

  3. 重启Visual Studio 2019,然后打开“工具 -> 选项”,如果有“AStyle Formatter”选项则表示已经安装成功;

  4. 配置AStyle格式化参数,勾选Format on save(保存前自动规范化);

AStyle的格式化参数设置提供了两种方式,可视化界面设置和命令行设置两种方式,选择其中一种即可,如下图所示。

 2.3、下载AStyle.exe

Artistic Style - Browse Files at SourceForge.net

下载AStyle_3.1_windows.zip,解压AStyle.exe到C:\Windows\System32

 3、AStyle配置  3.1、visual studio配置选项

将以下配置选项复制到AStyle Formatter中保存即可。

--style=allman --convert-tabs --indent=spaces=4 --attach-namespaces --attach-extern-c --attach-closing-while --indent-switches --indent-labels --indent-continuation=4 --indent-preproc-block --indent-preproc-define --indent-preproc-cond --indent-col1-comments --min-conditional-indent=0 --break-blocks --pad-oper --pad-comma --unpad-paren --align-pointer=type --align-reference=type --break-one-line-headers --add-braces --attach-return-type-decl --remove-comment-prefix --max-code-length=120 --break-after-logical --mode=c

3.2、vscode配置选项

设置->扩展->Astyle configuration->Cmd_options

"astyle.cmd_options": [

        "--style=allman", "--convert-tabs", "--indent=spaces=4",

        "--attach-namespaces", "--attach-extern-c", "--attach-closing-while",

        "--indent-switches", "--indent-labels", "--indent-continuation=4",

        "--indent-preproc-block", "--indent-preproc-define", "--indent-preproc-cond",

        "--indent-col1-comments", "--min-conditional-indent=0", "--break-blocks",

        "--pad-oper", "--pad-comma", "--unpad-paren", "--align-pointer=type",

        "--align-reference=type", "--break-one-line-headers", "--add-braces",

        "--attach-return-type-decl", "--remove-comment-prefix",

        "--max-code-length=120", "--break-after-logical", "--mode=c"

    ],

 3.3、配置选项说明 3.3.1、--style=allman

3.3.2、--convert-tabs 

将tab转换为空格,默认为四个。

3.3.3、--indent=spaces=4

缩进默认为4个空格;

3.3.4、--attach-namespaces

通常命名空间有多个层级,为避免缩进层级过多,namespace将设置为不缩进;

 

3.3.5、--attach-extern-c

 3.3.6、--attach-closing-while 

 3.3.7、--indent-switches

 3.3.8、--indent-labels

 3.3.9、--indent-continuation=4

 3.3.10、--indent-preproc-block

 3.3.11、--indent-preproc-define

 3.3.12、--indent-preproc-cond

 3.3.13、--indent-col1-comments

 3.3.14、--min-conditional-indent=0

 3.3.15、--break-blocks

在if、for、while等前后添加空行;

 3.3.16、--pad-oper

在 *** 作符前后添加空格;

 3.3.17、--pad-comma

 

 3.3.18、--unpad-paren

 3.3.19、--align-pointer=type

 3.3.20、--align-reference=type

 3.3.21、--break-one-line-headers

一行只写一条语句;

 3.3.22、--add-braces

if、for、while等后面必须带上{};

 3.3.23、--attach-return-type-decl

 3.3.24、--remove-comment-prefix

 3.3.25、--max-code-length=120 --break-after-logical

现在屏幕都比较大,每一行超过120个字符将自动换行即可(sonar规定是不超过160个字符);

 3.3.26、--mode=c

用于指定格式化C类型的文件,例如C、C++、Objective-C等;

 3.4、特殊配置说明  3.4.1、禁止格式化语句块

若要禁止格式化某一段代码,需要在段首添加注释“// *INDENT-OFF*”,段尾添加注释“// *INDENT-ON*”;

 3.4.2、禁止格式化语句行

若要禁止格式化某一行代码,需要在行尾添加注释“// *NOPAD*”;

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1498939.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-25
下一篇 2022-06-25

发表评论

登录后才能评论

评论列表(0条)

保存