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.
- Tools > External Tools
- Click Add
- Give the new command a title of your choice
- In command find clang-format.exe
- In arguments put
--style=Microsoft -i $(ItemFileName)$(ItemExt)
- In Initial Directory put
$(ItemDir)
- Now go to the desired file and do Tools > (Name of command you gave it in step 3).