VisualStudio 代码不格式化 C # 代码

我创建了一个新文件,设置了 C # 语言,并编写了一些代码。然后我按下 Ctrl + Shift + F(或者 F1 & rarr; 格式文件)。然后发现了错误

对不起,没有安装“ cSharp”文件的格式化程序。

另外,我安装了 C # 扩展,但它没有帮助。 Visual Studio Code 版本是1.18.0。

100736 次浏览

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.

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.

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

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.

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

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

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

"omnisharp.useEditorFormattingSettings": true

Setting this to false fixed indenting for me.

  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.

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.

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.

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.

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

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/

Working fine in Windows:

--> Shift + Alt + F

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.

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.

enter image description here

I solved that issue by simply pressing F1, writing "format", and hitting enter. In my case, there was no default formatter set in my environment so it prompted me options for formatters. I believe there was only one for C#.

Open vscode go to output >  the output of vs code

As you can see, the output is set to task. Change it to OmniSharp log and check if it says the dotnet version is not supported. If yes then update your dotnet version. Hope it helps.

windows : Shift+Alt+F ubuntu: : Ctrl + Shift + I

I kept pressing Shift + Alt + F (Format Document), thinking my formatter is not working because it was not chaning something I though was misformatted:


namespace MyApp // Note: actual namespace depends on the project name.
{
internal class Program
{


public static async Task Main(string[] args)
{
Console.WriteLine("Hello World!");


var app = PublicClientApplicationBuilder
.Create(_clientId)
.WithAuthority(AzureCloudInstance.AzurePublic, _tenantId)
.WithRedirectUri("http://localhost")
.Build();


Console.WriteLine("Hello World!");
}
}
}

it turns out that with the default settings, the microsoft c# extension is ok with this formatting, it doesn't think there is something to fix. If I change the indentation of the first line it fixes it.

So: Your code might already be formatted!