【问题标题】:Visual Studio 2010 C++ code formatterVisual Studio 2010 C++ 代码格式化程序
【发布时间】:2012-01-17 13:39:42
【问题描述】:

我是在 Visual Studio 中编写 C++ 程序的新手。当我在 VS 中编写 C# 程序时,代码格式化程序会更改看起来像这样的代码

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

进入

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

这更容易阅读。当我用 C++ 编写相同的东西时,什么也没有发生。我尝试选择文本并按 Ctrl+E、F,但这不起作用。有没有办法改进 Visual Studio 中的 C++ 代码格式化程序?

【问题讨论】:

  • 根据您的配置,相同的功能可能只是不同的键和弦。我记得它是(Ctrl-A)Ctrl-K Ctrl-F(或 Ctrl-Shift-F ...我不确定)。
  • Ctrl-A,然后是 Ctrl-K,Ctrl-F 确实会执行“自动格式化”。但是,C++ 中的自动格式化只会正确缩进代码,而不会执行任何高级操作,例如间距或确保括号正确缩进。它只会整理标签缩进级别。

标签: c++ visual-studio-2010 code-formatting


【解决方案1】:

如果您有现金可花,您可能需要查看Visual Assist。另请参阅this 问题。

【讨论】:

  • 它似乎做了很多好事,但格式化代码并不是它的 advertised 功能之一......
【解决方案2】:

Visual Studio 无法格式化 C++ 代码。也许有一个 VS 扩展。找了半天也没找到合适的免费的。

GreatCode 是一个非常好的、免费但不易使用的代码格式化程序。它是一个命令行工具。

您可以将其配置为“外部工具”:

在您的 HD 上解压 GreatCode 后,只需转到工具->外部工具->添加并插入以下设置...

每当您调用该工具时,都会对实际打开的文件进行格式化。

您可以在 gc.cfg 中随意配置 GreatCode。我尝试了很多选项,有些很简单,有些很复杂。

如果您想要类似微软的外观,只需使用我的设置作为模板并自行微调:

-code_constructor_style-1
-code_split_fctdef_style-5
-code_split_decl_style-2
-overwrite_read_only-
-verbose-
-tab_out-
-space_if-
-space_return-
-space_fctcall_inparam-
-no-space_fctcall_firstparam-
-no-space_cast_after-
-space_affect_style-0
-space_autoaffect_style-0
-code_len-180
-code_keep_more_empty_lines-
-code_decl_access_to_type-
-code_decl_break_template-
-code_remove_return_paren-
-code_align_max_blanks-80
-code_class_access_eol_after-1
-code_class_access_eol_before-1
-code_split_fctcall_style-1
-code_constructor_style-1
-no-code_split_bool_before-
-no-stmt_concat_else_if-
-no-stmt_decl_remove_empty-
-no-stmt_concat_if_remove_empty-
-no-stmt_concat_else_if-
-stmt_force_brace-1
-stmt_break_dowhile-
-stmt_switch_style-1
-stmt_switch_eol-0
-stmt_class_indent-0
-stmt_static_init_style-2
-stmt_concat_inline_class-
-pp_align_to_code-
-pp_style-1
-pp_align_breakline-
-no-cmt_first_space_cpp-
-cmt_dont_modify-
-no-cmt_add_class_access-
-no-cmt_add_gc_tag-
-no-cmt_add_fct_def_class-
-no-cmt_decl_before-
-no-cmt_decl-
-no-cmt_first_line_break_first-
-no-cmt_first_line_break_last-
-no-code_split_bool_before-
-catch_eol_before-1
-no-stmt_decl_remove_empty-
-no-cmt_add_fct_def_class-
-no-cmt_add_class_access-
-no-stmt_break_alone-
-stmt_concat_inline_class-
-cmt_keep_cpp-

祝你好运!

【讨论】:

  • 为什么不将您的cfg 文件复制并粘贴到您的答案中?它使您的答案更加有用。
  • 感谢您的回答和 .cfg 文件。
【解决方案3】:

除了格式化工具本身之外,我使用与 DirkMausF 完全相同的方法。我建议您使用艺术风格格式化程序:

http://astyle.sourceforge.net/

它有很好的文档记录,并带有许多预定义的格式样式,因此非常易于使用。

【讨论】:

  • VS2010 和 VS2012 有一个扩展,它集成了 Artistic Styler 和 IDE。它被称为 AStyle;您可以通过“工具”菜单上的“扩展”项或从此处获取它:tinyurl.com/8ardana
  • 给未来用户的注意事项:1. 您需要为 AStyle 的 Format Document 分配键盘快捷键,以及 2. 在更改任何内容之前阅读他们的文档。 astyle.sourceforge.net/astyle.html#_Bracket_Style_Options 我是 Java 大括号选项 BTW 的粉丝。在 CSS 和 C# 中称为半扩展
  • 我搜索了很多方法,尝试了很多方法,包括GC.exe。但是这个AStyle真的很不错。请查看它的文档,非常详细。可以清楚地看到每个选项的“是”和“是”。感谢分享。
【解决方案4】:

很长一段时间以来,我都在 Netbeans 中编写所有 C++ 代码,并在 Visual Studio 中编译它。 Netbeans 完美地格式化代码(使用 ALT+SHIFT+F)并且有许多格式化选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 2018-05-01
    • 2019-04-14
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多