Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code formatting in Visual Studio Code is not working on C# code

This question already has an answer here: How do you format code in Visual Studio Code?

All the answers provided in this question are not working with C# code.

I just copied my controller file and opened it in Visual Studio Code. I tried to format code with Shift + Alt + F (both right and left shifts in windows)

But it didn't work.

Is there a solution for this?

The same shortcut is working for HTML and JavaScript files.

like image 516
J Santosh Avatar asked Sep 22 '15 05:09

J Santosh


2 Answers

For C# you need to

  • Open a folder (not a file) that contains at least one .sln file
  • In the bottom bar you will see a blinking flame. Wait until it stops blinking.
  • Click on the flame. You are going to be asked to select a file.
  • In case you selected a .sln file click again on the flame and select a .csproj file.
  • You can now format all .cs files which are part of the selected project using Shift + Alt + F.
like image 140
Wosi Avatar answered Nov 12 '22 18:11

Wosi


In my case the problem was related with the code formatting extension.

I was using C# for "Visual Studio Code (powered by OmniSharp)" (ms-vscode.csharp) extension for formatting. However, after a while the "format document" context menu has gone missing. And the key combination for formatting "Shift+Alt+F" stopped working.

After some research I have found that there is a new extension called "C# FixFormat" (Leopotam.csharpfixformat) for formatting C# documents which says in their description: "there is bug in omnisharp and they are fixing the issue temporarily".

After installing "C# FixFormat" extension I can now format documents again.

like image 42
Oncel Umut TURER Avatar answered Nov 12 '22 16:11

Oncel Umut TURER