VS开发C++代码格式化设置(AStyle)

一.下载Astyle插件

下载地址:
https://marketplace.visualstudio.com/items?itemName=Lukamicoder.AStyleExtension

二.安装AStyle

1.打开AStyleExtension.vsix

2.选择相应的VS版本

3.重启VS,然后打开 工具 -> 选项 , 如果有 “AStyle Formatter” 选项,即为:安装成功.如图:

三.设置AStyle(以C++为例)

1.方法一:命令行(我使用的方法)

直接单击Edit,进行编辑.

例如输入:--style=java -f -U -p -N --mode=c

然后save -> 确定

测试,编辑 -> 高级 -> "Format Document(AStyle)"或"Format Selection(AStyle)",即可查看代码样式效果.

PS:修改其相关的快捷键. 工具 -> 环境 -> 键盘 ,输入”AStyle”,对应设置其相关的快捷键.

2.方法二:Settings,进行勾选,生成Command line

3.方法三:Import导入

四:部分相关参数的含义

例如:--style=java -f -U -p -N --mode=c

--style=java:设置代码风格为Java,还可以设置成:ansi等.

--mode=c:#指定文件处理模式,C/C++模式,其他选择还有cs,java等

子参数 
(1) –f  //-F也是一样的效果
在两行不相关的代码之间插入空行,如import和public class之间、public class和成员之间等;

(2) –p  //小写
在操作符两边插入空格,如=、+、-等。
如:int a=10*60;
处理后变成:int a = 10 * 60;

(3) –P  //大写
在括号两边(内和外)插入空格。另,-d只在括号外面插入空格,-D只在里面插入。
如:System.out.println(1);    //输入-P
处理后变成System.out.println( 1 );   //增加了4个空格

(4) -U
移除括号两边不必要的空格。
如:System.out.println( 1 );  //有3个空格
处理后变成System.out.println(1); //3个空格被去掉了

(5) -V
将Tab替换为空格。

(6) -N
本条命令主要针对namespaces。
如果没有此参数,效果如下:
namespacefoospace
{
class Foo
{
public:
Foo();
virtual ~Foo();
};
}
有此参数就会变成这样:
namespacefoospace
{
    class Foo
        {
    public:
        Foo();
        virtual ~Foo();
    };
}

(7) -n
不生成备份文件,默认是备份生成 .orig文件。

———————————————————-华丽分割线———————————————————-

五.进一步补充(特殊需求)

1.缩进采用4个空格

--indent=spaces=4

2.对于单行的语句增加括号

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

3.强制转换TAB为空格

--convert-tabs

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

--indent-preprocessor

5.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

6.*,&这类字符靠近变量名字,目前选择这个,

--align-pointer=name

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

--pad-oper

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

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

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

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

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

--lineend=windows

11.后台开放统一用UNIX的方式

--lineend=linux

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

--indent-switches

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

--indent-col1-comments

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

--indent-namespaces

--indent-classes

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

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

--min-conditional-indent=1

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

--max-instatement-indent=80

提示:一般一个项目组使用一套设置好的编程风格.

你可能感兴趣的:(插件,Visual,Studio,Astyle,C++风格)