source insight 4.0 + astyle 实现代码格式化

背景:
source insight 4.0有代码格式化功能,在tools->reformat source code with xxx。但是对于不同的后缀文件(如.c),似乎会自动采用对应格式,如C文件用不了JAVA风格,就算设置了也不起作用。但是好处是可以对注释、变量等进行对齐、缩进。
Astyle则是一款通用的,可控性高的代码格式化插件。缺点自然是无法对注释、变量进行对齐。

Astyle在source insight 4.0上的使用方法:
1、 下载安装
Astyle可在网站http://astyle.sourceforge.net下载。
直接解压即可。我是放在SI的安装目录下,毕竟是跟它配套使用。
2、 加载至SI
2.1 在SI增加astyle命令的运行路径。
2.1.1 打开自定义命令。
source insight 4.0 + astyle 实现代码格式化_第1张图片
2.1.2 新增命令(第2步)、运行路径(第3步)。第2步中的自定义命令名称则是新增时设置的命令名。
source insight 4.0 + astyle 实现代码格式化_第2张图片
2.2 为astyle设置快捷键和菜单键
2.2.1 打开自定义命令的菜单设置
source insight 4.0 + astyle 实现代码格式化_第3张图片
2.2.2 设置自定义命令的菜单键位置(第1步)、设置自定义命令的快捷键(第2+3步)。其中insert\delete\up\down可设置自定义命令在菜单中的位置。
source insight 4.0 + astyle 实现代码格式化_第4张图片
3、 设置
A、 命令运行设置
source insight 4.0 + astyle 实现代码格式化_第5张图片
以下对参数做大致介绍:
Output:
Iconic window:运行si的提示性窗口,不会运行astyle的后台窗口。
Capture output:捕捉astyle命令的运行结果,并新建一个临时文件,并保存运行结果。
Paste output:粘贴astyle命令的运行结果至鼠标处。若parse links in output选中,则保存到临时文件。
Control:
Save files first:命令运行完成后,自动保存格式代后的代码。
Pause when done:命令运行完成后,暂停。如果选中了iconic window,则不要选中此处,否则会造成提示性窗口一直不会自动关闭。
Beep when done:命令运行完成后,语音提示。
Wait until done:一直等待,至命令运行完成。
Close files:命令运行完成后,关闭相关的临时文件。
Exit to windows:命令运行完成后,退出SI。
B、 命令参数设置
命令参数则参考http://astyle.sourceforge.net/astyle.html#_Brace_Style_Options
我个人的设置如下:
–style=kr --indent=spaces=4 --attach-classes --attach-inlines --attach-closing-while --indent-classes --indent-switches --indent-labels --indent-preproc-block --indent-preproc-define --indent-preproc-cond --indent-col1-comments --min-conditional-indent=0 --max-continuation-indent=80 --break-blocks --pad-oper --pad-comma --pad-paren --delete-empty-lines --align-pointer=name --align-reference=name --break-one-line-headers --add-braces --attach-return-type --convert-tabs --close-templates

参数说明如下:
–style=kr:使用K&R代码格式。
–indent=spaces=4:使用4个空格方式缩进。
–attach-classes:为类增加大括号。
–attach-inlines:为内建函数增加大括号。
–attach-closing-while:使用do-while语句时,将while与结束括号放置同一行。
–indent-classes:类中所有语句缩进。只用于C++。
–indent-switches:switch中所有语句缩进。
–indent-labels:goto语句标号缩进。
–indent-preproc-block:预定义块缩进。
–indent-preproc-define:预定义语句缩进。
–indent-preproc-cond:使用预定义的语句块缩进对齐。
–indent-col1-comments:注释和代码缩进对齐。
–min-conditional-indent=0:最小缩进。
–max-continuation-indent=80:最大缩进。
–break-blocks:语句块(如if,for,while)前后增加空行。
–pad-oper:操作符前后增加空格。
–pad-comma:分隔符后增加空格。
–pad-paren:
–delete-empty-lines:删除多余的空行。
–align-pointer=name:指针符号与变量一起。
–align-reference=name:引用符号与变量一起。
–break-one-line-headers:控制语句作为单独一行。
–add-braces:语句或者语句块增加大括号。
–attach-return-type:返回类型和函数在同一行。
–convert-tabs:将TAB转为空格。
–close-templates:

你可能感兴趣的:(sofeware,note)