Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio 2010 C++ code formatter

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?

like image 872
Mike Plott Avatar asked Dec 10 '11 20:12

Mike Plott


People also ask

How do I add code formatter in Visual Studio Code?

The code formatting is available in Visual Studio Code through the following shortcuts: On Windows Shift + Alt + F. On Mac Shift + Option + F. On Linux Ctrl + Shift + I.


2 Answers

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!

like image 120
DirkMausF Avatar answered Oct 20 '22 02:10

DirkMausF


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.

like image 27
vitakot Avatar answered Oct 20 '22 01:10

vitakot