利用AStyle对C++代码进行自动化风格规范管理

利用AStyle对C++代码进行自动化风格规范管理

一、Visual Studio 中使用AStyle

1)下载AStyle插件:

https://marketplace.visualstudio.com/items?itemName=Lukamicoder.AStyleExtension

AStyleExtension2019.vsix

https://download.csdn.net/download/lbfing/11184298

2)打开AStyleExtension.vsix

3)选择相应的VS版本

4)重启VS,然后打开 工具 -> 选项 , 如果有 AStyle Formatter 选项,即为:安装成功

5)打开VS,然后打开 工具 -> 选项,选择AStyle Formatter,选择标签栏位C/C++,点击Edit弹出规范化命令输入窗口,将下方粘贴区域代码输入文本框中。点击SAVE,勾选Format on save(保存前自动规范化)。点击确认按钮。

二、Qt Creator 中使用AStyle

1)下载AStyle https://sourceforge.net/projects/astyle/files/ 并将其解压到 “F:\package\”得到“F:\package\ AStyle_3.1_windows\bin\ AStyle.exe”

2)点击帮助关于插件,在上方搜索框粘贴Beautifier。搜索到Beautifier插件,若插件未被勾选则将其勾选,并重启Qt Creator,插件生效。

3)点击工具,Beautifier,点击Artistic Style分页框,选择浏览将“F:\package\ AStyle_3.1_windows\bin\ AStyle.exe”

4)点击use customized style 右侧的edit。将name输入QtValue中贴入粘贴区域代码。

5)点击ok保存规范化格式。勾选Use customized style

6)将标签栏选择Genera,勾选Enable auto format on file save,工具选择Artistic Style

7)点击Apply后再点击Ok

三、AStyle

AStyle 规范化配置

//下方在格式化规范粘贴区域

--style=kr
--indent=spaces=4
--indent-switches
--indent-cases
--align-pointer=name
--align-reference=name
--convert-tabs
--break-blocks
--delete-empty-lines
--unpad-paren
--attach-namespaces
--max-code-length=100  
--max-instatement-indent=120
--pad-header
--pad-oper

//上方在格式化规范粘贴区域

AStyle 规范化配置说明

--indent=spaces=4           

行缩进用4个空格

--indent-switches            

switch 与case不同列,case缩进

--indent-cases                  

缩进case下面的语句

--align-pointer=name    

指针符号缩进

--align-reference=name

引用符号缩进

--convert-tabs                  

tab转空格

--break-blocks                  

空行分隔无关块

--delete-empty-lines       

删除多余空行

--unpad-pare                    

移除括号两端多余空格

--attach-namespaces      

命名空间紧贴

--max-code-length=100

代码最长数100,有效值为50-200

--max-instatement-indent=120 

跨行缩进,比如函数定义中的参数处在多行,那么参数缩进到同一列

--pad-header 

在if for while 等条件语句中,(前增加空格

--pad-oper                        

操作符两端出入空格

包含源代码,需要的可以自行下载。 把astyle.exe 复制到 C:\WINDOWS 目录里,省的指定路径 VC6++ 设置方法 菜单->工具->定制->工具菜单内容->新建菜单,参数如下 命令行:astyle.exe 变量: --style=k&r --brackets=break --indent=spaces --indent-cases --indent-preprocessor --pad-header --pad-oper --unpad-paren --keep-one-line-statements --keep-one-line-blocks --convert-tabs $(FileName)$(FileExt) 初始目录: $(FileDir) VC2008 外部工具里设置,还可以添加快捷键 命令:astyle.exe 参数: --style=k&r --brackets=break --indent=spaces --indent-cases --indent-preprocessor --pad-header --pad-oper --unpad-paren --keep-one-line-statements --keep-one-line-blocks --convert-tabs $(ItemFileName)$(ItemExt) 初始目录: $(ItemDir) CodeBlocks 设置差不多,很多绿色版的已经设置好了 AStyle_2.02_windows.zip bin 目录里有官方编译好的 Artistic Style 2.01 Maintained by: Jim Pattee Original Author: Tal Davidson Usage : astyle [options] Source1.cpp Source2.cpp [...] astyle [options] Beautified When indenting a specific file, the resulting indented file RETAINS the original file-name. The original pre-indented file is renamed, with a suffix of ".orig" added to the original filename. Wildcards (* and ?) may be used in the filename. A 'recursive' option can process directories recursively. By default, astyle is set up to indent C/C++/C#/Java files, with four spaces per indent, a maximal indentation of 40 spaces inside continuous statements, a minimum indentation of eight spaces inside conditional statements, and NO formatting options.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值