Is there any way to flag a block of code so it isn't reformatted by "Format Document"?

I love the VS2005 autoformatter, and I use "Edit / Advanced / Format Document" and "Format Selection" all the time. Sometimes, though, the code is much easier to read if you break the rules and put everything on one line with multiple spaces or tabs.

Example:
Property1 { get { return this.myClass.internalAttribute1; } { set this.myClass.internalattribute1 = value; }}
P2        { get { return this.p2; }                         { set this.p2 = value; }}
Property3 { get { return this.internalAttribute3; }         { set this.internalattribute3 = value; }}
This is concise and readable.

Property1 { get { return this.myClass.internalAttribute1; } { set this.myClass.internalattribute1 = value; }}
P2 { get { return this.p2; } { set this.p2 = value; }}
Property3 { get { return this.internalAttribute3; } { set this.internalattribute3 = value; }}
This is a mess.

Is there any way to flag a block of code so the autoformatter leaves it alone while formatting the rest of the file

I rely so much on reformatting that I'm putting the do-not-format code in a separate partial class file, but that's not really a solution since the whole point of formatting the code is to make it easy to read, and it's hard to read if it's not there.



Answer this question

Is there any way to flag a block of code so it isn't reformatted by "Format Document"?

  • Sidhartha

    Personally, I prefer looking at the second block of code. While it doesn't look as pretty when I'm skimming through code, its much easier to read IMHO.  The partial class idea was definitely an option but since you don't like that, considering writing your own reformatter with a macro.  There is already support to reformat code from what you don't like, to what you do like (its in the Samples.VSEditor.LineEmUp() function).  You could write a wrapper function that calls the document formatter, then looks for your custom flag, and calls "lineemup" on the code element.  Shouldn't be too hard to do.   

    Keep in mind that you might need to call LineEmUp() a few times to get the final desired result.

  • traci8891

    Unfortunately, there is no way to do this in VS2005.  We considered an option to disable formatting for a block, however, it seemed that the only reason you'd want this was because our formatting engine didn't support a scenario that you'd like to see (which is clearly true for your case).  We went the route of trying to enable scenarios in our formatting engine instead of a disable block. 

    I'll take this feedback back for when we start planning for the next version -- we're definitely taking another look what formatting options we need to expose.  Please feel free to send additional feedback at karenliu@microsoft.com as well.

    Thanks!
    Karen Liu
    Visual C# IDE

  • Is there any way to flag a block of code so it isn't reformatted by "Format Document"?