How to control clang-format indentation of chained method calls?

前端 未结 1 1922
萌比男神i
萌比男神i 2021-02-05 22:31

I want the results to look like this:

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

but instead cla

1条回答
  •  一向
    一向 (楼主)
    2021-02-05 22:59

    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+\.

    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
    

    0 讨论(0)
提交回复
热议问题