Question about formatting combination of invocation + chained method calls (fluent interface)

I have the following code:


var person = new PersonBuilder().WithContext(new ContextBuilder().WithDomain(LocalDomain).WithContextContactInfo(new ContextContactInfoBuilder().WithIsPublic(false).WithDomain(LocalDomain)).WithContextType(new ContextTypeBuilder().WithDomain(LocalDomain))).Build();


That I would like to have formatted as follows:


var person = new PersonBuilder()
    .WithContext(new ContextBuilder()
        .WithContextContactInfo(new ContextContactInfoBuilder()
        .WithContextType(new ContextTypeBuilder()


Right now the only solution I can find is to manually format it as such myself and check the option "keep existing line breaks" but this means that elsewhere in the code unneeded line breaks are kept as well.

I have played with several options but there is no combination that I am aware of that will result in code formatted as I want. Is there a better way to accomplish what i want? Or maybe I should reconsider the desired formatting?

1 comment
Official comment

Hello Gabriel!


Currently there's no such option, I've filed corresponding feature request -

As a workaround you can disable formatting for this code block (R# 2017.3 feature):

// @formatter:keep_existing_linebreaks true
 person = new PersonBuilder()     .WithContext(new ContextBuilder()         .WithDomain(LocalDomain)         .WithContextContactInfo(new ContextContactInfoBuilder()             .WithIsPublic(false)             .WithDomain(LocalDomain))         .WithContextType(new ContextTypeBuilder()             .WithDomain(LocalDomain)))     .Build();

// @formatter:keep_existing_linebreaks restore

Thank you.

Please sign in to leave a comment.