如何阻止ReSharper在赋值变量和链接方法调用之间插入不必要的换行符 [英] How to stop ReSharper from inserting unnecessary newline between the assigned variable and chained method call

查看:39
本文介绍了如何阻止ReSharper在赋值变量和链接方法调用之间插入不必要的换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,它有一些返回this的方法,这样我就可以将调用链接在一起:

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;
   }
}

但是,当我将足够多的方法链接在一起,以至于ReSharper中的Right margin (columns)设置时,表达式的格式有点时髦。

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

显然,这个示例有点做作,因为我永远不会一遍又一遍地调用相同的方法,但在我的产品代码中,我与一个流畅的API交互,该API是通过将大约10个方法调用链接在一起来配置的。

理想情况下,代码的格式如下:

// 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");

真正的问题是为什么ReSharper将整个表达式放在一个新的行上。有什么建议吗?

推荐答案

您需要将"包装链接的方法调用"设置为"始终截断",以便在同一行上获得var john = new Person().WithFirstName("John")

这篇关于如何阻止ReSharper在赋值变量和链接方法调用之间插入不必要的换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆