Curly braces on new line in vscode
This past week, I dedicated my mornings to a single goal: configuring Visual Studio Code to automatically insert a new line before braces {}.
How it was:
if (true){
// do something
}
How I wanted it to be:
if (true)
{
// do something
}
The task proved to be more challenging than I expected. I found many discussions about the same issue in forums and on Stack Overflow, but no definitive solution. So I’m here to share the solution I discovered.
First of all, you will need:
- C# Extension;
omnisharp.json
file;- Changes applied to
settings.json
.
#
Configuring Omnisharp
You can find the location of your Omnisharp in %USERPROFILE%/.omnisharp/
.
My Omnisharp is at /home/amanda/.omnisharp/omnisharp.json
.
This is my Omnisharp configuration:
{
"FormattingOptions": {
"newLine": "\n",
"useTabs": false,
"tabSize": 4,
"indentationSize": 4,
"NewLinesForBracesInTypes": true,
"NewLinesForBracesInMethods": true,
"NewLinesForBracesInProperties": true,
"NewLinesForBracesInAccessors": true,
"NewLinesForBracesInAnonymousMethods": true,
"NewLinesForBracesInControlBlocks": true,
"NewLinesForBracesInAnonymousTypes": true,
"NewLinesForBracesInObjectCollectionArrayInitializers": true,
"NewLinesForBracesInLambdaExpressionBody": true,
"NewLineForElse": true,
"NewLineForCatch": true,
"NewLineForFinally": true,
"NewLineForMembersInObjectInit": true,
"NewLineForMembersInAnonymousTypes": true,
"NewLineForClausesInQuery": true,
}
}
Configuration taken from this comment.
#
Updating VSCode settings
You can find the settings.json
file in ~/.config/Code/User
.
My file is at /home/amanda/.config/Code/User/settings.json
.
I added these lines to the settings.json:
"omnisharp.json": "/home/amanda/.omnisharp",
"omnisharp.enableEditorConfigSupport": false,
"omnisharp.useEditorFormattingSettings": true,
"omnisharp.path": "latest",
"editor.formatOnType": true,
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"editor.defaultFormatter": "ms-dotnettools.csharp",
"[csharp]": {"editor.defaultFormatter": "ms-dotnettools.csharp"}
For these settings to work, you need to have the C# extension installed and enabled, and after all this, restart Omnisharp.
Here is the extension:
Ctrl+Shift+P: Restart Omnisharp
#
Everything working BUT
After all these changes, you can start using Visual Studio Code and enjoy having braces inserted on a new line… BUT automatic formatting while typing is not working. To work, you need to use the Format Document option in Visual Studio Code.
I didn’t want to keep using this option every time, so after a lot of searching, I found these settings for the editor:
- editor.formatOnSave
- editor.formatOnPaste
- editor.formatOnType
With these settings, when saving the file and pasting code, the formatting will be done automatically. BUT the option to format while typing formatOnType still doesn’t work. I found a Github issue discussing the problem. The formatOnType does not work for C# because this feature validates the ; (more used in js). They have this fix on the roadmap, but until the fix is released, the formatOnSave will do the job.