Disable code formatting for specific block of code in Visual Studio
Asked Answered
B

3

19

How can I disable code formatting for a specific block of code in Visual Studio 2017 (C# 7)?

I have this method:

public CarViewModel(ICarsRepo carsRepo)
{
    ...

    Manufacturers = ToSelectList<Manufacturer>();
    Categories = ToSelectList<Category>();
    States = ToSelectList<State>();
}

And I would like to format it like so:

public CarViewModel(ICarsRepo carsRepo)
{
    ...

    Manufacturers   = ToSelectList<Manufacturer>();
    Categories      = ToSelectList<Category>();
    States          = ToSelectList<State>();
}

But when I press Ctrl K + Ctrl D, it goes back to what it was.

#region inspiration

I would like something to wrap the specific block of code like a #region:

public CarViewModel(ICarsRepo carsRepo)
{
    ...

    #region disable_format

    Manufacturers   = ToSelectList<Manufacturer>();
    Categories      = ToSelectList<Category>();
    States          = ToSelectList<State>();

    #endregion
}

#pragma inspiration

Or not necessarily a region, maybe a pragma used like in this code snippet:

            var parameter = 0;
            var sqlCommand = $"{parameter}";
#pragma warning disable EF1000 // Possible SQL injection vulnerability.
            this.Database.ExecuteSqlCommand(sqlCommand);
#pragma warning restore EF1000 // Possible SQL injection vulnerability.

This is more of an aesthetic preference which might not be shared by most developers, but which I quite like in my code from time to time.

Bitterweed answered 24/2, 2019 at 6:27 Comment(0)
S
30
  • Visual Studio (reference)

    • To disable formatting: #pragma warning disable format
    • To enable formatting: #pragma warning restore format
        switch (number) {
    #pragma warning disable format
            case 1:    cardinal = "one";     animal = "monkey";     break;
            case 2:    cardinal = "two";     animal = "horse";      break;
            case 3:    cardinal = "three";   animal = "pig";        break;
            case 4:    cardinal = "four";    animal = "chicken";    break;
    #pragma warning restore format
        }
    
    
  • Rider (doc)

    • To disable formatting: // @formatter:off
    • To enable formatting: // @formatter:on
        switch (number) {
            // @formatter:off
            case 1:    cardinal = "one";     animal = "monkey";     break;
            case 2:    cardinal = "two";     animal = "horse";      break;
            case 3:    cardinal = "three";   animal = "pig";        break;
            case 4:    cardinal = "four";    animal = "chicken";    break;
            // @formatter:on
        }
    
  • Combined

        switch (number) {
    #pragma warning disable format // @formatter:off
            case 1:    cardinal = "one";     animal = "monkey";     break;
            case 2:    cardinal = "two";     animal = "horse";      break;
            case 3:    cardinal = "three";   animal = "pig";        break;
            case 4:    cardinal = "four";    animal = "chicken";    break;
    #pragma warning restore format // @formatter:on
        }
    
Stereotype answered 1/12, 2020 at 0:37 Comment(0)
A
4

This matches your need. Not just C#, but also for ANY language.

Manufacturers/**/= ToSelectList<Manufacturer>();
Categories/*   */= ToSelectList<Category>();
States/*       */= ToSelectList<State>();
Agonistic answered 21/6, 2019 at 3:44 Comment(1)
Not that it needs to, but I can't see how that works for VB, even substituting " ' ' " for " /* */ ".Soundproof
U
2

This doesn't disable just a block of formatting but it stops Visual Studio from formatting any of your declaration statements.

enter image description here

Untuck answered 24/2, 2019 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.