How can I change the indent rules inVisual Studio?

Hi,

How do you configured Visual Studio c++ indent for source code?

By default, it puts a tab indent after a macro like UPROPERTY() and in Epic code there are not indent.

I want to be as close as possible to Epic Formating.

Thanks,

1 Like

Here are my C++ formatting rules. You’ll need to change the extension back to .vssettings

My VS C++ Formatting Settings

Cheers,
Nick

Thanks for the file but this didn’t solve my issue.

Anytime I set UPROPERTY() and press return to write the property defintion, it automatically indent it :frowning:

Any other hints?

Thanks,

Thanks for the tips… I hope this will get fix sometimes ^^

Oh that - that’s visual studio being visual studio. It attempts to do smart indenting but because C++ is a complicated language, it sometimes gets confused. The way to trick it - after you fix the extra indent with backspace or delete, use the arrow keys to leave the line instead of the mouse or the enter key, that will keep your manually chosen indention.

Cheers,
Nick

Thanks for the tips… I hope this will get fix sometimes ^^

I was just struggling with this myself. I don’t believe it’s possible to make Visual Studio treat this case correctly. One option is to sidestep the problem on a case-by-case basis, as NickDarnell describes, but that can get really obnoxious.

Another option is just to turn off Visual Studio’s auto-formatting entirely. This is unfortunate, because when it works it can be a very nice feature. However, Unreal’s heavy use of macros and it’s custom pre-processor can confuse the auto-formatter.

To turn it off, go to Tools → Options… → Text Editor → C/C++ → Formatting → General, and then uncheck all of the checkboxes there.

Note that when you hit return after the UPROPERTY line, it will still give you the initial indentation. But it will no longer insist on re-inserting that indentation all the time.

1 Like

In Visual Studio Community 2013:

Go to Tools > Options > Text Editor > C/C++ > Tabs
In the “Indenting” section select “Block” instead of Smart.

It will stop over-indenting after macros such as GENERATED_BODY(). However:

  • it will stop indenting after access specifiers such as “public:”. You can still indent manually once and from there, the Block mode will keep your indentation.
  • it will stop indenting after other kinds of structure ending with () without {, such as if () with no {} to execute a single conditional instruction.
  • if you enter an opening bracket such as ( or { directly after the macro, or only after blank lines, the whole line will be over-indented at the same time. You can deactivate this behavior by unchecking “Automatically format braces when they are automatically completed” in Tools > Options > Text Editor > C/C++ > Formatting > General, but it will also stop useful behavior such as indenting after a {. Actually, the problem is that VS still consider what is after the macro as something that should be indented, i.e. any reformating operation will over-indent the next non-blank non-comment line after the macro. In practice, if you are listing your private attributes, you will probably have at least one field with no such symbol that will act as an inhibitor for the following lines. Attributes with list initializers and methods, however, will contain { / ( so be careful.

Alternative trick: add a semicolumn after the macro, and get some peace with that. Don’t forget to remove it later, or you will have a compile error.

1 Like

Using Block indenting instead of Smart for C++ didn’t work for me on Visual Studio 2013. It still indented everything after GENERATED_BODY().

Personally, I just go mad on ctrl+z. It will undo any auto-formatting. Always does the trick.

I wrote a visual studio extension to fix issues with smart indenting around UE4 macros. You can find it and instructions for installation on my github: GitHub - hackalyze/ue4-vs-extensions: Useful UE4 Visual Studio extensions.

Thank you! This is awesome!

Perfect, thank you for this!

you need to type smicolon after statement, and that will triger smart indenting. you can’t simply go to next line with arrow keys.

this is by far the best solution. but you loose smart indenting.

what is the difference?
https://blogs.msdn.microsoft.com/zainnab/2010/04/15/smart-vs-block-vs-none-the-indenting-smackdown/

Unfortunately the Visual Studio Extension von hackalyze doesn’t work if i use auto format

The only way i could figure out to prevent Visual Studios auto format from indenting the line after an UE Macro is to change the settings in Tools → Options… → Text Editor → C/C++ → Formatting → General → Default Formatstyle from Visual Studio to WebKit (or a similar property, my language in my visual studio is set to german). Unfortunately after changing to WebKit it isn’t possible to change any other formatting property. But i’m fine with it, WebKit formats the code almost 100% to my habits.

If anyone knows a way how to be able to change additional format settings AND prevent the indent even after an auto format, please tell me ^^

I finally got it…
I know it’s an old thread, but in case someone else does have a similar problem:

Download and install clang-format configurator and you can define your own format behaviour.
Getting started with Clang-Format Style Options.

I used the WebKit-Clang-Format-File as template and adjusted it a little bit. My .clang-format file.

How to use:
Just copy the .clang-format file into the root/source directory of your c++ project. Ensure Clang-Format support in visual studio is active (see screenshot of my previous post, ignore the red circle ^^).

4 Likes

Thank you so much for this! VS is so much more usable after adding this.

I’ve edited the file you attached to better fit with some of the Unreal/VS defaults, as I wanted it to more closely match with my previous styling. If anyone is interested in that, I’ve attached my version to this post. I haven’t used it extensively yet, so I’ll update this post if I end up making any further changes to it.

.clang-format (4.6 KB)

4 Likes