Visual Studio Code doesn't format C# code

C#Visual Studio-Code

C# Problem Overview


I created a new file, set the C# language, and wrote some code. Then I pressed Ctrl + Shift + F (or F1Format 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.

C# Solutions


Solution 1 - C#

If you have prettier as the default formatter as I do, you should do this:

  1. Open your vscode settings with these shortcut: ctrl + ,, or enter image description here

  2. Then click to "open settings (JSON)": enter image description here

  3. 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 - C#

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 FilePreferencesSettings

Enter image description here

Solution 3 - C#

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 - C#

  1. Browse the Visual Studio Code extension library, and make sure you've got the C# extension installed ms-dotnettools.csharp

    VS Code extensions pane showing C# OmniSharp extension

  2. 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.

    VS Code settings window with editor.formatOnType setting

  3. 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 - C#

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 - C#

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 - C#

I found there was another setting interfering with formatting C#.

"omnisharp.useEditorFormattingSettings": true

Setting this to false fixed indenting for me.

Solution 8 - C#

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):

Enter image description here

Then the formatting will work (as well as other OmniSharp features).

Solution 9 - C#

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 - C#

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 - C#

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 - C#

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 13 - C#

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 14 - C#

Working fine in Windows:

--> Shift + Alt + F

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionandoralView Question on Stackoverflow
Solution 1 - C#ezhupa99View Answer on Stackoverflow
Solution 2 - C#SampathView Answer on Stackoverflow
Solution 3 - C#Pavel LevchukView Answer on Stackoverflow
Solution 4 - C#Andrew BView Answer on Stackoverflow
Solution 5 - C#PeterView Answer on Stackoverflow
Solution 6 - C#Ali DehqanView Answer on Stackoverflow
Solution 7 - C#Samuel RidingsView Answer on Stackoverflow
Solution 8 - C#YanivView Answer on Stackoverflow
Solution 9 - C#ManabuView Answer on Stackoverflow
Solution 10 - C#Mr. YANOView Answer on Stackoverflow
Solution 11 - C#Yurii BrusentsovView Answer on Stackoverflow
Solution 12 - C#napi15View Answer on Stackoverflow
Solution 13 - C#Anas EineaView Answer on Stackoverflow
Solution 14 - C#R M Shahidul Islam ShahedView Answer on Stackoverflow