Home > Blockchain >  Visual Studio 2019 - Multiple format settings
Visual Studio 2019 - Multiple format settings

Time:08-03

The code base I am working with uses a KNR formatting like this.

void foo(bool bar) {
    if (bar) {
       // do something
    } else {
       // do something else
    }
}

I am generally not a fan of this formatting and would much prefer the following.

void foo(bool bar)
{
     if (bar)
     {
         // do something
     }
     else
     {
         // do something else
     }
}

The owners of the code base use Visual Studio 2019 and when doing Edit.FormatDocument(Ctrl K D), it will auto format to the first code block.
I was wondering if there's a method in VS I could switch to my preferred format when I'm opening a file to read/code(doesn't have to be automatically), and return back using (Ctrl K D) when I'm submitting?

CodePudding user response:

Well I found a quick and dirty solution for this using clang-format. Make sure you have clang installed and do the following in Visual Studio.

  1. Tools > External Tools
  2. Click Add
  3. Give the new command a title of your choice
  4. In command find clang-format.exe
  5. In arguments put

--style=Microsoft -i $(ItemFileName)$(ItemExt)

  1. In Initial Directory put

$(ItemDir)

  1. Now go to the desired file and do Tools > (Name of command you gave it in step 3).
  • Related