Visual Studio 2010 C++ code formatter
Asked Answered
P

4

19

I'm new to coding C++ programs in Visual Studio. When I am coding C# programs in VS, code formatter changes code that looked like this

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

into

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

which is way easier to read. When I write the same thing in C++, nothing happens. I tried to select the text and press Ctrl+E, F, but that didn't work. Is there a way to improve the C++ code formatter in visual studio?

Pictor answered 10/12, 2011 at 20:0 Comment(2)
Depending on your configuration, the same function may simply be a different key-chord away. I remember it being (Ctrl-A) Ctrl-K Ctrl-F (or Ctrl-Shift-F ... I'm not sure).Lebna
Ctrl-A, then Ctrl-K, Ctrl-F does indeed do an 'autoformat'. However, the autoformat in C++ only indents the code correctly, but doesn't do anything advanced, such as spacing or ensuring brackets are indented correctly. It will only sort out the tab indentation levels.Dewie
R
9

Visual Studio can't format C++-Code. Maybe there is a VS extension. I searched a long time, but never found a suitable one for free.

A very good, free, but not easy to use code formatter is GreatCode. Its a command line tool.

You can configure it as "External Tool":

After unpacking GreatCode on your HD just go Tools->External Tools->Add and insert the following settings...

enter image description here

Whenever you call that Tool, the actual opened file is being formatted.

You can configure GreatCode as you like in the gc.cfg. I tried many options, some are easy, some are complex.

If you want a Microsoft-like looking, just use my settings as a template and fine tune yourself:

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

Good luck!

Reeve answered 10/12, 2011 at 20:48 Comment(1)
Why not copy-and-paste your cfg file into your answer? It makes your answer much more useful.Trilogy
M
8

I use exactly the same approach as DirkMausF except the formatting tool itself. I would suggest you to use Artistic Style formatter:

http://astyle.sourceforge.net/

It is well documented and comes with a lot of predefined formatting styles so it is very easy to use.

Maximamaximal answered 10/12, 2011 at 21:20 Comment(3)
There's an extension for VS2010 & VS2012 that integrates Artistic Styler with the IDE. It's called AStyle; you can get it via the Extensions item on the Tools menu or from here: tinyurl.com/8ardanaIrretrievable
A note for future users: 1. you'll need to assign a keyboard shortcut to AStyle's Format Document and 2. Read their documentation before you change anything. astyle.sourceforge.net/astyle.html#_Bracket_Style_Options I'm a fan of Java brace options BTW. It's called semi-expanded in CSS and C#Panslavism
I searched lots of methods, and tried lots of methods, including the GC.exe. But this AStyle is really really good. Please check its document, very detailed. Every option's 'was' and 'is' can be seen clearly. Thanks for sharing this.Physiography
R
0

If you have cash to spend, you might want to look in to Visual Assist. See also, this question.

Roddie answered 10/12, 2011 at 20:6 Comment(1)
It seems it does a lot of good things, but formatting code is not one its advertised features...Riley
D
0

For a long time, I was writing all the C++ code in Netbeans and I was compiling it in Visual Studio. Netbeans is formatting code perfectly (with ALT+SHIFT+F) and there are many formatting options.

Dayan answered 14/4, 2013 at 7:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.