Visual Studio Code doesn't format C# code
I created a new file, set the C# language, and wrote some code. Then I pressed Ctrl + Shift + F (or F1 → Format Document). And got I the error
Sorry, but there is no formatter for 'csharp'-files installed.
Also, I installed C# Extension, but it didn't help. The Visual Studio Code version is 1.18.0.
If you have prettier as the default formatter as I do, you should do this:
-
Open your vscode settings with these shortcut:
ctrl + ,
, or -
Then click to "open settings (JSON)":
-
This is where you should paste the below snippet.
This is my config:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[csharp]": {
"editor.defaultFormatter": "ms-dotnettools.csharp"
}
}
For you to apply this configuration you need c# extension.
This works for me.
Note: If it is true
then clear the checkbox and set it to true again. After that, you must restart Visual Studio Code.
Menu File → Preferences → Settings →
Visual Studio Code with OmniSharp doesn't format C# code without a .csproj file.
You can create a new project with dotnet new console
with the .NET Core SDK.
-
Browse the Visual Studio Code extension library, and make sure you've got the C# extension installed
ms-dotnettools.csharp
-
Press Ctrl + , to open the Settings panel. Change the
editor.formatOnType
setting to be enabled. This wasn't enabled by default in my Visual Studio Code. Restart Visual Studio Code. It should now make code auto-format when you complete the line with a semicolon, or when you close the outer brace of a scope.
Note: this is a global setting for the editor, so it may enable auto-formatting for other languages and not just C#.
This was tested on Visual Studio Code version 1.43.1.