Roslyn: Disable code formatting for some part of code by using specific comments/region

Created on 2 Jul 2019  路  5Comments  路  Source: dotnet/roslyn

VSF_TYPE_MARKDOWNOther IDEs have this feature for long. Would like to have an option where the formatting for code is disabled between 2 comments like below

// @formatter:off
public const string Foo                   =           "foo";
public const string Bar                    =           "bar";

// @formatter:on 

This would allow many developers who prefer column aligned code to do it. Visual studio will then not revert their changes while formatting the file.

_This issue has been moved from https://developercommunity.visualstudio.com/content/idea/481492/disable-code-formatting-for-some-part-of-code-by-u.html
VSTS ticketId: 813186_
_These are the original issue comments:_

Jane Wu [MSFT] on 3/7/2019, 00:10 AM (117 days ago):

Thank you for taking the time to provide your suggestion. We will do some preliminary checks to make sure we can proceed further. We'll provide an update once the issue has been triaged by the product team.


Kendra Havens [MSFT] on 7/1/2019, 04:45 PM (16 hours ago):

Thank you for this feedback. You can suppress diagnostics in code with #pragma. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/preprocessor-directives/preprocessor-pragma-warning

Does this feaure satisfy this request? Please let us know if something is missing from your experience.


puneetkeshav on 7/1/2019, 07:50 PM (13 hours ago):

This is not a warning/diagnostic issue, so it will not get solved by #pragma. Visual studio formats the document if document formatting is turned on. But in some areas of code we want that the default formatting should not take place. If we can have some simple solution as suggested in the first comment that would be very helpful.

Kendra Havens [MSFT] on 7/2/2019, 09:40 AM (34 sec ago):

I see, this would be for even the silent auto-formats that don't report diagnostics. Thank you for the additional information! I'll move this issue to our open source GitHub repository for additional design discussion with the community.

Area-IDE Developer Community Feature Request IDE-Formatter Resolution-Fixed

Most helpful comment

This is implemented in #38627 as

#pragma warning disable format
public const string Foo                   =           "foo";
public const string Bar                    =           "bar";
#pragma warning restore format

All 5 comments

This is implemented in #38627 as

#pragma warning disable format
public const string Foo                   =           "foo";
public const string Bar                    =           "bar";
#pragma warning restore format

I have a similar problem in JavaScript. The formatting ruins a regular expression I use to aid in creating
a Guid.
function newGuid(hasDashes) { var guid = hasDashes ? ([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)) : ([1e7]+1e3+4e3+8e3+1e11).replace(/[018]/g, c => (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)); return guid;
When I hit Ctrl-K Ctrl-D the formatter inserts spaces around the '+' and '-' characters. I need to get rid of this behavior so I don't have to constantly fix the code.

I have a similar problem in JavaScript

Roslyn does not power javascript formatting @flyfisher1952. Thanks! :)

Hi, I have tried #pragma warning disable format and indeed it disables code formatting what is great. But after the region I want to disable format on, if I use #pragma warning restore format it doesn't work, is ignored by the code formatter so the entire document is unformatted, not only the specific part of code I need.

I'm using VSCode 1.48.2 with the C# Extension 1.23.0 in Linux PopOS 20.04

@JuDelCo can you file a new bug report for the specific case with a code sample?
Thanks! 馃槃

Was this page helpful?
0 / 5 - 0 ratings