csharpier icon indicating copy to clipboard operation
csharpier copied to clipboard

Determine the necessary editorconfig to allow enabling IDE0055

Open belav opened this issue 2 years ago • 4 comments

The rules under C# Formatting Options appear to all be unnecessary when using CSharpier and can cause conflicts.
The rules under .NET Formatting Options could still be useful.

It would be useful to determine what set of .editorconfig options would allow enabling IDE0055, or provide alternatives for the .NET Formatting Options

belav avatar Jan 04 '23 03:01 belav

This is what I've come up with so far. I'm still looking to see if there are non-IDE0055 options that are conflicting as well. These can be set to ignore if desired, but having the editor's help pre-save seems valuable. Also, I think I could write test(s) that leverage the analyzer to ensure this is correct (likely similar for R# settings).

I can't yet figure out if there's any way to control alignment via EditorConfig.

### CSharpier-compatible settings ###

# C# formatting settings - New-line options
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options#new-line-options
csharp_new_line_before_else = true
csharp_new_line_before_catch = true
csharp_new_line_before_finally = true
csharp_new_line_before_open_brace = all
csharp_new_line_before_members_in_anonymous_types = true
csharp_new_line_before_members_in_object_initializers = true
csharp_new_line_between_query_expression_clauses = true

# C# formatting settings - Indentation options
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options#indentation-options
csharp_indent_case_contents = true
csharp_indent_switch_labels = true
csharp_indent_labels = no_change
csharp_indent_block_contents = true
csharp_indent_braces = false
csharp_indent_case_contents_when_block = false

# C# formatting settings - Spacing options
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options#spacing-options
csharp_space_after_cast = false
csharp_space_after_keywords_in_control_flow_statements = true
csharp_space_between_parentheses = none
csharp_space_before_colon_in_inheritance_clause = true
csharp_space_after_colon_in_inheritance_clause = true
csharp_space_around_binary_operators = before_and_after
csharp_space_between_method_declaration_parameter_list_parentheses = false
csharp_space_between_method_declaration_empty_parameter_list_parentheses = false
csharp_space_between_method_declaration_name_and_open_parenthesis = false
csharp_space_between_method_call_parameter_list_parentheses = false
csharp_space_between_method_call_empty_parameter_list_parentheses = false
csharp_space_between_method_call_name_and_opening_parenthesis = false
csharp_space_after_comma = true
csharp_space_before_comma = false
csharp_space_after_dot = false
csharp_space_before_dot = false
csharp_space_after_semicolon_in_for_statement = true
csharp_space_before_semicolon_in_for_statement = false
csharp_space_around_declaration_statements = false
csharp_space_before_open_square_brackets = false
csharp_space_between_empty_square_brackets = false
csharp_space_between_square_brackets = false

# C# formatting settings - Wrap options
# https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/style-rules/csharp-formatting-options#wrap-options
csharp_preserve_single_line_blocks = true
csharp_preserve_single_line_statements = false

# undocumented
dotnet_style_operator_placement_when_wrapping = beginning_of_line

elovelan avatar Jun 03 '23 17:06 elovelan

Also, these seem to be the only (documented) IDE0055 settings that aren't applicable:

dotnet_sort_system_directives_first = true
dotnet_separate_import_directive_groups = false

elovelan avatar Jun 03 '23 17:06 elovelan

There are also some experimental settings that may be applicable that Visual Studio seems to know about (at least in the preview version, I haven't checked the RTM version yet) that I have yet to investigate since they're undocumented.

elovelan avatar Jun 03 '23 18:06 elovelan

Thanks for digging into this. I tested it out on a large repo and ran into a couple things.

With csharp_new_line_before_open_brace = all

It is unhappy with braces being on the same line as a ) in some cases

    public void Method(
        string someLongName
    ) { }

    if (ex is HttpRequestException) { }

    // this is fine
    public void Method() { }

Switching to csharp_new_line_before_open_brace = none causes a lot more complaints.

It is also unhappy with this statement

return from prp in this.DataProvider.LinqQuery<ProductRelatedProduct>()
    join p in this.GetTable() on prp.RelatedProductId equals p.Id
    where
        prp.ProductId == productId
        && prp.SystemListValue.Name == type
        && p.ActivateOn < DateTimeProvider.Current.Now
        && (p.DeactivateOn ?? DateTimeOffset.MaxValue) > DateTimeProvider.Current.Now
    select p;

// it wants
return from prp in this.DataProvider.LinqQuery<ProductRelatedProduct>()
       join p in this.GetTable() on prp.RelatedProductId equals p.Id
       where
           prp.ProductId == productId
           && prp.SystemListValue.Name == type
           && p.ActivateOn < DateTimeProvider.Current.Now
           && (p.DeactivateOn ?? DateTimeOffset.MaxValue) > DateTimeProvider.Current.Now
       select p;

I didn't see any way to disable or ignore either of these. And I'm not sure if we want to make the changes to get IDE0055 happy, but if #661 is implemented, and also puts System first, then it may not be necessary to figure this out. We've started enforcing sorted usings at work, and having CSharpier do it for me would make life a bit easier

belav avatar Jun 30 '23 18:06 belav