user3009097
user3009097

Reputation: 193

How to automatically indent #pragma directive in Visual Studio

Does anyone have any idea on how to ensure that #pragma directives in Visual Studio 2013 (C++ project) are indented properly?

I'm annoyed by the fact that Visual Studio always eliminates the indentation I made to these directives. It makes it difficult to collapse sections of my source code. The example below is an example of no indentation. If it was indented, then it would be easier to read.

void func()
{
#pragma region section 1
#pragma endregion
}

Upvotes: 4

Views: 2932

Answers (3)

VisorZ
VisorZ

Reputation: 566

I disagree that no #pragma expression should be indented to show, that they are scopeless. Because in this special question #pragma region MyName is nothing, that will change the code path or any meaning of it. Region pragmas are only meta information for the IDE. I am feeling tempted to say, that Microsoft mis-uses the #pragmakeyword to enable the IDE feature that regions can be collapsed.

In my daily codebase preprocessor directives are scarce, but there are many region directives. Therefore I do not risk to misinterprete the text by indenting pragmas. If you like to indent #pragma expressions, you can enable it under:

Tools > Options > Text Editor > C/C++ > Formatting > Indentation > Position of preprocessor directives > Leave indented

This works at least since Visual Studio 2017 with Productivity Power Tools installed.

Upvotes: 8

NathanOliver
NathanOliver

Reputation: 180415

I agree with the answer by Lightness Races in Orbit. Preprocessor commands are are acted on regardless of there indentation and having them with 0 indentations helps to call out the the block of code is wrapped by them.

That said if you want to stop MSVS from changing the indentation you can go to

Tools -> Options -> Text Editor -> C/C++ -> Tabs

And change it from Smart to Block. This will stop auto indentation from happening on everything else as well so you might not want it.

Upvotes: 1

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385098

If it were indented, then it would be misleading. #pragmas are preprocessor directives, and do not abide by scope. You are attempting to tell readers of your code the opposite.

Visual Studio is being sensible, for once, and I don't know how to change that behaviour.

Upvotes: 4

Related Questions