在Visual Studio 2013中自动进行c ++源代码格式化

时间:2017-06-04 11:54:32

标签: c++ visual-studio visual-studio-2013

如何在所有项目中格式化或与Visual Studio 2013对齐几个c ++源代码?

我是否有机会在项目中使用单个模板?

实际代码的示例:

for(int i= 0; i<(n+  m) ;  i++){
}

所需代码的示例:

for ( int i = 0; i < (n + m); i++ )
{
}

我使用CTRL + K格式化 - &gt; CTRL + D表示实际文档和CTRL + K - &gt; CTRL + F用于实际选择。但这不符合我的个人要求。如何根据自己的需求配置它?

如果在visual studio 2013上没有可用工具的机会,我会寻找插件。

1 个答案:

答案 0 :(得分:0)

尝试使用AStyle插件:https://marketplace.visualstudio.com/items?itemName=Lukamicoder.AStyleExtension

将其设置为format on save,然后ctrl+s成为格式命令(如果您希望它经常格式化)。另外,使用AStyle,您可以heavily customize进行格式化约定。

我们还使用astyle配置文件,并拥有一个构建服务器,定期在repo上执行格式化。您可以使用批处理文件执行相同的操作:

:: This script can be setup to run on a daily or weekly basis to clean up code commits

:: TODO : add directories and options for formatting
astyle --options=config.astyle -r ..\..\Source\*.cpp

:: TODO : add directories and messages for committing.
svn commit -m "[AUTO] Astyle Autoformatting" ..\..\Source\

config.astyle的位置如下:

--suffix=none # do not retain a backup of the original file

--style=allman

--indent=spaces=4
--convert-tabs

--indent-switches
--pad-oper

--align-pointer=type
--align-reference=type

--max-instatement-indent=40

--keep-one-line-blocks
--keep-one-line-statements

根据您的喜好。