'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.
Solution 1:[1]
If you have prettier as the default formatter as I do, you should do this:
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.
Solution 2:[2]
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 ?
Solution 3:[3]
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.
Solution 4:[4]
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.
Solution 5:[5]
The C# extension powered by Omnisharp doesn't have a formatter included (as far as I know).
You can install C# FixFormat. That does the trick for me, but the formatting is not as good as in Visual Studio IDE.
Solution 6:[6]
It is resolved after updating to Visual Studio Code 1.20.1 and re-enabling OmniSharp.
And just set "csharp.format.enable" to "true" in Workspace Settings (if it was true and not working yet, change it to false and then to true).
Solution 7:[7]
I found there was another setting interfering with formatting C#.
"omnisharp.useEditorFormattingSettings": true
Setting this to false
fixed indenting for me.
Solution 8:[8]
If you are still hitting this issue, please note that the OmniSharp extension will not behave nicely on .cs files that was not opened from inside a folder (from here).
In order to workaround this - make sure you open the desired file from within a folder (Ctrl + K, Ctrl + O):
Then the formatting will work (as well as other OmniSharp features).
Solution 9:[9]
In my case, I previously installed both dotnet
and dotnet-sdk
via Homebrew and those seemed to cause a conflict of OmniSharp. So I removed dotnet
by:
brew uninstall dotnet
Also, just in case, I re-installed dotnet-sdk
by:
brew reinstall dotnet-sdk
Plus, I restarted my Mac, then it finally worked fine.
Solution 10:[10]
FOR MAC USERS
I solved the issue by setting
"omnisharp.useGlobalMono": "never"
in the VS code settings, it will also prompt you to restart Omnisharp and after that it should work with the shift + option + f
key combination
Solution 11:[11]
Try to create an OmniSharp config for your project that will allow you to customize the formatting of C# code.
Create the omnisharp.json
in the project root (near the .sln
file)
{
"FormattingOptions": {
"newLine": "\n",
"useTabs": false,
"tabSize": 4,
"indentationSize": 4
}
}
NOTE: to confirm updates in omnisharp.json
you should reload vscode (ctrl
+shift
+p
-> Developer: Reload Window
)
More info about OmniSharp config: https://www.strathweb.com/2017/01/c-code-formatting-settings-in-vs-code-and-omnisharp/
Solution 12:[12]
This issue happened to me when the visual studio released new update for 2022, I resolved this by downloading the new .Net 6.0 which you will find https://dotnet.microsoft.com/en-us/download/dotnet/6.0 in this link.
Solution 13:[13]
Use the Synaptic package manager and mark the code package for 'Full Removal' and click apply.
Like here:
Delete ~/.vscode
Delete ~/.config
Reinstall
It worked for me.
Solution 14:[14]
Even after setting tab/space size in VSCode Omnisharp still uses its own rules you can change them by having an "omnisharp.json" with custom rules. for more information on these rules and where you can add the file check this article.
Solution 15:[15]
Check the Omnisharp log to see if its giving a reason for the lack of formatting. In my case it was dotnet sdk needed updating.
Solution 16:[16]
Working fine in Windows:
--> Shift + Alt + F
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow