Formatting: Indentation for initializer with method calls

Hello,

My team and I are currently switching from VS for mac to rider.

When it comes to formatting we are facing an issue, wich we do not have solved yet.

I have the following example code:

public class Formatting
{
    public Formatting()
    {
        new Person {
            Age = 1
        }.WithName("");

        new Person {
                Age = 1
            }.WithName("")
            .WithLastName("");
    }
}

internal class Person
{
    public int Age { get; set; }
    private string FirstName { get; set; }
    private string LastName { get; set; }

    public Person WithName(string name)
    {
        FirstName = name;
        return this;
    }

    public Person WithLastName(string lastName)
    {
        LastName = lastName;
        return this;
    }
}

 

I want the formatting for the person (s) to look like this:

 public Formatting()
    {
    	// this formatting is OK as it is
        new Person {
            Age = 1
        }.WithName("");

        new Person {
            Age = 1
        }.WithName("")
        .WithLastName("");
    }

 

What formatting rules do i have to adjust in the rider IDE or the .editorconfig to achieve this? 

 

Thanks

0
3 comments

The EditorConfigs matching your request:


[*]

# Microsoft .NET properties
csharp_indent_braces = false
csharp_new_line_before_open_brace = accessors,anonymous_methods,control_blocks,events,indexers,lambdas,local_functions,methods,properties,types

# ReSharper properties
resharper_csharp_continuous_indent_multiplier = 0
resharper_csharp_other_braces = end_of_line
resharper_place_simple_initializer_on_single_line = false
resharper_use_continuous_indent_inside_initializer_braces = false

Select the code in editor and then press Alt + Enter for Context Actions, use the auto detection feature to detect code style settings. Also, you can save the settings into IDE configuration or .editorconfig.

0

Thanks for the answer.

 

The “Detect code style settings” does not work for me. It does not detect the correct formatting. After exporting the settings to the .editorconfig it does not work either.

 

The proposed solution works for the code example.
But the entry resharper_csharp_continuous_indent_multiplier = 0 has some undesirable side effects.

 

Examples:

if (true)
// comment
    Console.WriteLine("foo");

var p = new Person(
"firstname",
"lastname");

var p2 =
new Person();

var p3 = new Person()
.WithName("");

So, normally I want resharper_csharp_continuous_indent_multiplier = 1, but not for an initializer block with more than one methods calls.

Where can I see all possible settings for the .editorconfig or the .DotSettings?

0

The EditorConfig properties are documented in Code Style settings

Not sure if your EditorConfig in Rider is exported correctly, by defaule the “Export settings that have default values” is disabled. It may be controlled by the other local settings (like settings in .DotSettings config).

0

Please sign in to leave a comment.