Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
}
like image 494
user3009097 Avatar asked Oct 28 '25 08:10

user3009097


1 Answers

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.

like image 98
VisorZ Avatar answered Oct 30 '25 22:10

VisorZ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!