Settings from the .editorconfig are not respected in Omnisharp / VS Code

Viewed 3420

I'm trying to set up my Visual Stuido Code so that Omnisharp uses settings supplied in an .editorconfig, as described in https://www.strathweb.com/2019/07/editorconfig-support-in-omnisharp-and-c-extension-vs-code/. I set up a new .net-core 3.1 console project using dotnet new console and added a .editorconfig file to the root of the project. Then I filled in the exact values from the blog post (see below) and enabled editorconfig and roslyn analyzers for OmniSharp. I even tried both methods for enabling the features: In settings.json and in omnisharp.json. But when I used the refactoring capabilities of OmniSharp to create a field from a constructor parameter the generated name was equal to the parameter name instead of being prefixed with _. Restarting the OmniSharp server multiple times didn't help as well.

.editorconfig

[*.cs]
dotnet_style_qualification_for_field = false
dotnet_naming_style.instance_field_style.capitalization = camel_case
dotnet_naming_style.instance_field_style.required_prefix = _

Sample class with auto-generated field

public class MyClass
{
    private readonly string a;  // This should have been named _a

    public MyClass(string a)
    {
        this.a = a;  // Should be without the this.-prefix
    }
}

What I am using:

  • VS Code version 1.42.1
  • C# extension for VS Code version 1.21.12
  • Omnisharp server (part of the extension) version 1.34.13

I would appreciate it alot if anyone could tell me what I am doing wrong or point me in the right direction.

1 Answers

I've also had trouble with formatting lately.

I think I finally have a stable working solution.

  • I disabled all other code formatting extensions that I had previously installed and tried. This included commenting out all settings related to those extensions in settings.json
  • I removed the .editorconfig file that had been at my project root.
  • I added the following to settings.json
"[csharp]": {
    "editor.defaultFormatter": "ms-dotnettools.csharp"
 },
"omnisharp.enableMsBuildLoadProjectsOnDemand": true,
"omnisharp.enableEditorConfigSupport": true,
"omnisharp.enableRoslynAnalyzers": true,
  • I added the following to the top of my omnisharp.json file.
{
  "RoslynExtensionsOptions": {
    "enableAnalyzersSupport": true,
  },
  "FormattingOptions": {
    "enableEditorConfigSupport": true,
  • Recreate .editorconfig at the project root using the example linked in the post you mentioned above. This was important for me because the .editorconfig I had previously used was not working even after all the other changes.
  • Restart VS Code
  • Reload your project

I didn't see the option for creating readonly string _name until after restarting everything.

[Edit] The following answer from @Stephen may also be helpful for you.

#IDE1006
dotnet_naming_style.camel_case.capitalization = camel_case
dotnet_naming_symbols.private_symbols.applicable_accessibilities = private
dotnet_naming_rule.camel_case_for_private.severity = warning
dotnet_naming_rule.camel_case_for_private.symbols  = private_symbols
dotnet_naming_rule.camel_case_for_private.style = camel_case
Related