dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.04k stars 4.03k forks source link

Update csharp_indent_braces to allow for more fine grained control #63256

Open drieseng opened 2 years ago

drieseng commented 2 years ago

The csharp_indent_braces .editorconfig option currently only allows one to enable or disable indent of braces for all code elements.

Would it be possible to allows indenting of braces to be enabled for specific code elements (just as other options like csharp_new_line_before_open_brace have)?

In our team, we indent braces for object initializers, but not for namespaces, types and methods. For example:

namespace CodeStyle
{
    public class Program
    {
        var x = new Person
            {
                Name = "John",
                Age = 49
            };
        Console.WriteLine(x.Name);
    }
}

Currently it's not possible to configure this style. We therefore have to disable IDE0055 (dotnet_diagnostic.IDE0055.severity = none) which is quite a pity as we miss out on other useful formatting checks.

I propose to update csharp_indent_braces to allow for the following values:

For compatibility, the current allowed values true and false can remain supported:

drieseng commented 2 years ago

@vatsalyaagrawal, is there anything I can do to help? I suppose there are two parts to this issue:

  1. Update VS to correctly apply this setting when formatting code.
  2. Have an analyzer report any violations (via IDE0055).
CyrusNajmabadi commented 2 years ago

@drieseng we are unlikely to make any changes to our current formatting engine, due to the high risk of regressions and unintended impact on users currently on it. We are currently working on a design for a new formatter (@jmarolf for details) that would ideally allow for more extensibility. We'll add this feature request to that item so it is tracked.