如何控制链接方法调用的clang格式缩进? [英] How to control clang-format indentation of chained method calls?

查看:101
本文介绍了如何控制链接方法调用的clang格式缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望结果看起来像这样:

I want the results to look like this:

auto foo = FooBuilder()
    .WithSomething()
    .WithSomethingElse()
    .Build();

,但是 clang-format 的格式如下:

auto foo = FooBuilder()
               .WithSomething()
               .WithSomethingElse()
               .Build();

我希望链接的调用相对于上一行的开头缩进,而不是相对于 FooBuilder()调用而缩进.我没有在 clang-format 选项中控制它的任何内容.设置 ContinuationIndentWidth 无效.有什么想法吗?

I want the chained calls to be indented relative to the beginning of the preceding line, not relative to the FooBuilder() call. I don't see anything in the clang-format options that control this. Setting ContinuationIndentWidth does not help. Any ideas?

推荐答案

不幸的是,这似乎是不可能的..我发现唯一会影响这一点的选项是 ContinuationIndentWidth ,就像您说的那样,它并没有执行您想要的操作.

Unfortunately, this appears to be not possible. The only option I have found that affects this at all is ContinuationIndentWidth, which, as you said, doesn't do what you want.

我个人将要做的是使用以下正则表达式查找已分解的链接方法调用:

What I personally would do is use the following regex to find chained method calls that have been broken up:

\)\ s + \.

它将匹配一个右括号,1个或多个空格字符(但不为0)和一个句点.您可能没有太多实例,因此您可以手动修复它们,然后为这些行禁用clang-format,以便将来不再使用它:

It will match a closing parenthesis, 1 or more whitespace characters (but not 0), and a period. You probably don't have too many instances of this, so you could just fix them manually, and then disable clang-format for those lines so it leaves it alone in the future:

// clang-format off

auto friggin_cool_object = SuperCoolBuilder().do_what_i_want()
    .figure()
    .out()
    .the()
    .params()
    .too();

// clang-format on

这篇关于如何控制链接方法调用的clang格式缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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