6

I've been using Prettier during a time and with JavaScript works perfect.

But since I also started to work with C#, I don't know how to setup VSC for still use Prettier for JavaScript and another plugin for C# formatting.

Edit: if I disable Prettier as default editor it works. But I can't disable only for C#. This setting doesn't works:

"prettier.disableLanguages": [ "csharp", "cs" ]

Senda Dev
  • 123
  • 1
  • 6
  • Does this answer your question? [Visual Studio Code doesn't format C# code](https://stackoverflow.com/questions/47352611/visual-studio-code-doesnt-format-c-sharp-code) – gunr2171 Mar 01 '21 at 13:26
  • @gunr2171 C# format is enabled. – Senda Dev Mar 01 '21 at 13:27
  • How about this? https://stackoverflow.com/questions/49500433/auto-format-c-sharp-code-in-visual-studio-code – gunr2171 Mar 01 '21 at 13:27
  • @gunr2171 I've format on save set to true in settings, and it works. But the problem is, if I use Prettier, it doesn't know about C#. If I don't use it, it says: "there is no csharp formatter installed". – Senda Dev Mar 01 '21 at 13:30
  • Looking at the question maybe it's something related with OmniSharp. I will investigate, thank you. – Senda Dev Mar 01 '21 at 13:37

2 Answers2

5

CSharpier is an awesome code formatter for C# inspired by Prettier!

To enable it:

  1. Install the CSharpier VSCode extension
  2. Open the control panel (Ctrl + Shift + P)
  3. Go to Preferences: Open Settings (JSON)
  4. Add this to your settings.json file
  "[csharp]": { "editor.defaultFormatter": "csharpier.csharpier-vscode" },
  1. Navigate to a C# file, then look for a pop-up in the bottom right that asks to install it as a local or global tool. Once it appears, click either the "local" or "global" option to install it.

Note: The tool part of the install is required since the Roslyn team doesn't support alternative code formatters plugging into Omnisharp directly, so instead this tool works alongside the VSCode extension to do formatting that way instead.

gridlocdev
  • 118
  • 1
  • 9
0

Instead of have Prettier as default formatter, and disable it only for C#, I had to enable Prettier only for the languages that I want.

"[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode" },

In this way, if it's not a JS file, it will use the VSC integrated formatter.

I don't know why I can't just disable Prettier for C#, but it's a solution for this problem.

Senda Dev
  • 123
  • 1
  • 6