您的位置:首页 > 编程语言

代码的格式化工具--Artistic Style

2018-01-11 18:52 471 查看


在source Insight中加入Artistic Style实现代码的格式化

Artistic style是一个开源的用于格式化代码的项目,

网站是http://astyle.sourceforge.net/

下载地址:http://sourceforge.net/projects/astyle/files/astyle/

1.首先安装Source Insight(以下简称SI),然后下个Artistic Style(以下简称AS)的Windows版本,解压。

解压后有4个文件夹,bin目录下就是编译好的AS,src和doc下分别是源代码和文档。

假设解压路径为C:\Program Files (x86)\,则C:\Program Files (x86)\AStyle\bin下就是AS的可执行程序,添加到SI中的方法如下:

2.在SI中添加格式化命令

(不同版本的SI,可能Custom Cimmands位置不一样)





Run栏输入:"C:\Program Files (x86)\AStyle\bin\AStyle.exe" --options="C:\Program Files (x86)\AStyle\bin\c.conf" %f

说明:指定工具AStyle.exe,后面为该工具传入参数,c.conf文件为格式化标准配置文件(格式模板),文件名用SI的通配符%f代替。



配置完成后单机上图中Menu选项:







配置文件(格式化模板)如下:

#指定文件处理模式,C/C++模式,其他选择还有cs,java

--mode=c

#ANSI标准的文件格式,
#--style=ansi :ANSI 风格格式和缩进
#--style=kr :Kernighan&Ritchie 风格格式和缩进
#--style=linux :Linux 风格格式和缩进
#--style=gnu :gnu 风格格式和缩进
--style=kr

#缩进采用4个空格

--indent=spaces=4

#对于单行的语句增加括号

#Add brackets to unbracketed one line conditional statements (e.g. 'if', 'for', 'while'...).

--add-brackets

#强制转换TAB为空格

--convert-tabs

#将preprocessor (#define) 这类预定的语句,如果有多行时前面填充对齐,(是对单语句(多行)进行填充呀)

--indent-preprocessor

#Attach a pointer or reference operator (* or &) to either the variable type (left) or variable name (right), or place it between the type and name.

#*,&这类字符靠近类型

#--align-pointer=type

#*,&这类字符靠近变量名字,目前选择这个,例如char *foo1;

--align-pointer=name

#在操作符号两边增加空格字符 ==

--pad-oper

#在几个关键字后面增加空格

#insert space padding after paren headers only (e.g. 'if', 'for', 'while'...).

--pad-header

#在if,for,while等代码段前后增加空行,这个其实挺好,但如果你不喜欢,可以关闭

#Pad empty lines around header blocks (e.g. 'if', 'for', 'while'...).

--break-blocks

#去掉括号两边多余的空格,

#暂时放弃,不是特别喜欢这个

#Remove extra space padding around parenthesis on the inside and outside.

#--unpad-paren

#备份文件的后缀,默认是.orig,=none表示不进行备份,

#放弃备份文件,以Astyle的版本,目前做这个没有什么差别。而且备份文件会影响你的代码

--suffix=none

#行结束用什么文件格式,UNIX的还是Windows的,

#--lineend=windows

#后台开放统一用UNIX的方式

--lineend=linux

#switch case的代码也按照标准缩进方式缩进

--indent-switches

#如果函数开始后面({后面)第一行是注释,也进行缩进

--indent-col1-comments

#namespace class内部的public,等我没有选择缩进

#--indent-namespaces

#--indent-classes

#多行条件语句如何进行缩进,=0表示和(对齐,=1表示缩进一次,=2表示缩进两次,=3缩进一个半

#MS visual studio的默认格式化是缩进一个,

--min-conditional-indent=1

#最大的缩进对齐长度,默认40,最大120,超过这个长度的多行()内语句不进行缩进,考虑到现在语句都较长

--max-instatement-indent=80

3.打开你的源代码,选择试图菜单中的astyle。SI会提示你文件被外部编辑器修改,是否重新载入,选择是。就可以看到格式化好的代码了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: