How to stop ReSharper from inserting unnecessary newline between the assigned variable and chained method call

Viewed 129

I have a class that has some methods which return this, such that I can chain the calls together:

public class Person {
   public string FirstName { get; set; }
   public string LastName { get; set; }

   public Person WithFirstName(string firstName) {
       FirstName = firstName;
       return this;
   }

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

However when I chain enough methods together such that the Right margin (columns) setting in ReSharper, the expression is formatted a little funky.

var john =
    new Person().WithFirstName("John")
                .WithLastName("Smith")
                .WithLastName("Smith")
                .WithLastName("Smith")
                .WithLastName("Smith");

Obviously, the example is a little contrived because I would never just call the same method over and over again, but in my production code I'm interfacing with a Fluent API which is configured by chaining together about 10 method calls.

Ideally, the code would be formatted like this:

// on a single line when less characters than `Right margin (columns)`
var john = new Person().WithFirstName("John").WithLastName("Smith");

// aligned at the "." when longer than `Right margin (columns)`
var john = new Person().WithFirstName("John")
                       .WithLastName("Smith")
                       .WithLastName("Smith")
                       .WithLastName("Smith")
                       .WithLastName("Smith");

The real question is why ReSharper is putting the whole expression on a new line. Any suggestions?

2 Answers
Related