dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.86k stars 671 forks source link

[BUG] Custom formatting imposible #7080

Closed arkalyanms closed 5 months ago

arkalyanms commented 5 months ago

From vscode-dotnettools created by Ganakim: microsoft/vscode-dotnettools#662

Describe the Issue

I am working with Unity and VSCode on an M1 MacBook Pro. I have been digging deep into this issue, and wanted to provide a comprehensive report on what I have found.

My preliminary goal was to get my C# Unity scripts to auto format, and keep the curly braces on the same line as the conditionals. To do this, I ended up installing the C# extension, and voila everything was perfect. Until I wanted functionality from the Unity extension. Installing that requires the C# DevKit extension, which, for some reason disables the omnisharp side of the C# extension, which in turn, disabled my autoformatting rules. Mind, the auto formatting did still function, just not according to the rules defined in my omnisharp.json

Digging into it, it's due to the switch to Roslyn. Unfortunately, if I want to keep the Unity extension, my C# DevKit extension needs to be installed as well. As is, I can only get both formatting and intellisense if I have just installed the unity extension, but have not reloaded my VSCode. Which is entirely too awkward.

Steps To Reproduce

No response

Expected Behavior

I should be able to use the unity extension, but also get my formatting.

Environment Information

Version: 1.83.1 (Universal) Commit: f1b07bd25dfad64b0167beb15359ae573aecd2cc Date: 2023-10-10T23:46:55.789Z (2 wks ago) Electron: 25.8.4 ElectronBuildId: 24154031 Chromium: 114.0.5735.289 Node.js: 18.15.0 V8: 11.4.183.29-electron.0 OS: Darwin arm64 22.6.0

All extensions are on the latest version

dibarbet commented 5 months ago

@Ganakim

We don't use omnisharp.json with the new Roslyn based server. However we do support .editorconfig based formatting configurations, see the docs here - https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/code-style-rule-options. .editorconfig should cover pretty much all the formatting options that omnisharp.json supports, and will work in VS and VSCode.

I'm going to close this as by-design - we intentionally only support .editorconfig as that is the modern, cross editor way of configuring C# code style rules and analyzers.

If you hit issues with using .editorconfig (e.g. missing rules from omnisharp.json), please re-open the issue (and feel free to tag me directly).

Ganakim commented 5 months ago

@dibarbet I've read that link several times, but it's not applicable to visual studio code, only visual studio. I have installed the EditorConfig extension: EditorConfig.EditorConfig, but it doesn't support the "csharp_new_line_before_open_brace" rule

JoeRobich commented 5 months ago

@Ganakim Both O# and the Roslyn LSP support the C# editorconfig options no additional extensions are required (although I always recommend the EditorConfig.EditorConfig extension). The .editorconfig simply need to exist alongside or above your .cs files. Next to your solution/project is a great place for it. You can even use the dotnet CLI to get you started by running dotnet new editorconfig.

Ganakim commented 5 months ago

Maybe I have things set up wrong in my preferences then. Are there relevant settings you could point me to?

JoeRobich commented 5 months ago

@Ganakim If you are still using O#, there is a VS Code setting which maps to the omnisharp.json Formatting option EnableEditorConfigSupport (see FormattingOptions.cs)

Image