Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent Visual Studio editor from reformatting #if ... #endif

Assuming I have the following code in C#:

void Func () {
    int i=3;

    #if DEBUG
    ...
    #endif

    for (int j=0;j<i;j++) {
        ...
    }
}

If I use Edit->Advanced->Format document, it gets reformatted to this (using my current code formatting settings):

void Func ()
{
    int i = 3;

#if DEBUG
    ...
#endif

    for ( int j = 0; j < i; j++ )
    {
        ...
    }
}

Is there a way to prevent the #if and #endif lines from being moved to the far left? I couldn't find any settings for these but I'm hoping there's a registry setting that controls how this works.

Any advice would be greatly appreciated - this behavior drives me crazy since my code has several hundred such blocks, they keep getting broken by VS.

like image 874
xxbbcc Avatar asked May 17 '12 19:05

xxbbcc


1 Answers

one style that I've been using is adding space to the # tag like this

void Func ()
{
    int i = 3;

#   if DEBUG
    ...
#   endif

    for ( int j = 0; j < i; j++ )
    {
        ...
    }
}

..the if block looks like its lined up with the code correctly and re-formatting code doesn't move the preprocessor tags.

like image 85
corn3lius Avatar answered Sep 19 '22 16:09

corn3lius