Formatting code in Visual Studio CODE Not working on C # code

There is already an answer in this question how-do-you-format-code-in-visual-studio-code-vscode .

All answers provided in this question do not work with C # code.

I just copied my controller file and opened it VS Code. I tried to format the code using Shift + Alt + F (right and left window shifts)

But that did not work.

Is there a solution for this?

The same shortcut works for HTML and JS.

+5
source share
2 answers

For C # you need

  • Open a folder (not a file) that contains at least one .sln file
  • In the bottom panel you will see a blinking flame. Wait until it stops flashing.
  • Click on the flame. You will be asked to select a file.
  • If you selected a .sln file, click on the flame again and select the .csproj file.
  • Now you can format all .cs files that are part of the selected project using Shift + Alt + F
+3
source

In my case, the problem was related to code formatting extension.

I used C # for the extension "Visual Studio Code (based on OmniSharp)" (ms-vscode.csharp) for formatting. However, after some time, the context menu "document format" disappeared. And the keyboard shortcut for formatting "Shift + Alt + F" stopped working.

After some research, I found that there is a new extension called "C # FixFormat" (Leopotam.csharpfixformat) for formatting C # documents, which says in their description: "There is an error in omnisharp and they fix the problem temporarily."

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

+1
source

Source: https://habr.com/ru/post/1231988/


All Articles