Search code examples
visual-studiovisual-studio-2017code-formatting

Disable code style for block in Visual Studio 2017


I want to disable code formatting for a certain block of code in Visual Studio 2017, e.g. to prevent indentings, line breaks or spacing to increase the readability.

I'm using Jetbrain's IntelliJ IDEA for a long time (Java and Typescript development) and now I have to switch to C# for some projects and have to use Visual Studio Professional 2017 (w/o ReSharper).
In IDEA there is an option to enable/disable the formatter for a certain block by using control markers in comments.

/* formatted code */


//@formatter:off

/* some unformatted code */

//@formatter:on


/* formatted code */

I only found this questions, but the answer wasn't helpful. The answer only describes how to disable the formatter in general for a case. I want to "break" the rules of the formatter to increase the readability.


Solution

  • No, unfortunately, Visual Studio does not provide an equivalent feature. There is no way to selectively disable code formatting for a particular block of code. It is an all-or-nothing proposition.

    You could potentially invert the problem: disable all automatic formatting, and then manually format individual blocks (select the block, and then press Ctrl+K, Ctrl+F).