问题
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?
回答1:
You need to set "Wrap chained method calls" to "Chop always" so that you get var john = new Person().WithFirstName("John") on the same line.
回答2:
I'm not exactly sure which one is responsible, you'll have to tweak a bit, but the settings for formatting are placed in Reshaper->Options->Code Editing->C#->Formatting Style->Line breaks and Wrapping. That's at least a good starting point for you !
来源:https://stackoverflow.com/questions/35974595/how-to-stop-resharper-from-inserting-unnecessary-newline-between-the-assigned-va