简体   繁体   English

调整 C++20 范围管道的 clang 格式

[英]Tweaking clang-format for C++20 ranges pipelines

C++20 (and 23 with std::ranges::to<T>() ) makes idiomatic the use of operator| C++20(以及带有std::ranges::to<T>()的 23)使operator|的使用变得惯用to make a pipeline of transformations such as this:进行这样的转换管道:

    return numbers 
        | std::views::filter([](int n) { return n % 2 == 0; })
        | std::views::transform([](int n) { return n * 2; })
        | std::ranges::to<std::vector>();

With my project's current .clang-format , that looks something like使用我项目的当前.clang-format ,看起来像

    return numbers | std::views::filter([](int n) { return n % 2 == 0; }) |
           std::views::transform([](int n) { return n * 2; }) | std::ranges::to<std::vector>();

which I find pretty hard to read.我觉得很难读。 If I set BreakBeforeBinaryOperators: All I get如果我设置BreakBeforeBinaryOperators: All我得到的

    return numbers | std::views::filter([](int n) { return n % 2 == 0; })
           | std::views::transform([](int n) { return n * 2; }) | std::ranges::to<std::vector>();

which is better, but I'd really like the original version with one pipeline operation on each line.哪个更好,但我真的很喜欢每行有一个管道操作的原始版本。

I can adjust the column limit, but that is a major change and also starts to line-break my lambdas, which I don't like:我可以调整列限制,但这是一个重大变化,并且开始换行我的 lambda,这是我不喜欢的:

    return numbers | std::views::filter([](int n) {
               return n % 2 == 0;
           })
           | std::views::transform(
               [](int n) { return n * 2; })
           | std::ranges::to<std::vector>();

I can manually use empty comments to force a newline:我可以手动使用空注释来强制换行:

    return numbers                                                //
           | std::views::filter([](int n) { return n % 2 == 0; }) //
           | std::views::transform([](int n) { return n * 2; })   //
           | std::ranges::to<std::vector>();

but again, not ideal knowing that pipelines will be pretty common.但同样,知道管道将非常普遍并不理想。 Am I missing settings?我缺少设置吗? Or is this more of a feature request I should direct to clang-format , like "Add an option so when more than n operator| appears in an expression, put each subexpression on its own line."或者这更多的是我应该针对clang-format的功能请求,比如“添加一个选项,以便当表达式中出现超过 n 个operator|时,将每个子表达式放在自己的行上。”

There's a feature request for AllowBreakingBinaryOperators .有一个AllowBreakingBinaryOperators功能请求。 Before the feature completes, only compromise can be made.在功能完成之前,只能做出妥协。

  1. As you've said, use // comments to force line breaks.正如您所说,使用//注释强制换行。
  2. use clang-format off/on to disable clang-format and format it yourself.使用clang-format off/on禁用 clang-format 并自行格式化。

Here's a more complex solution which combines both:这是一个更复杂的解决方案,它结合了两者:

void function() {
  return numbers | std::views::filter([](int n) { return n % 2 == 0; })
         | std::views::transform([](int n) { return n * 2; })
         | std::views::take(3) | std::ranges::to<std::vector>();
}

First, use // to split and then clang-format the code.首先,使用//拆分,然后 clang-format 代码。

void function() {
  return numbers
         //
         | std::views::filter([](int n) { return n % 2 == 0; })
         | std::views::transform([](int n) { return n * 2; })
         | std::views::take(3)
         //
         | std::ranges::to<std::vector>();
}

Next, remove // , use clang-format off/on to disable clang-format.接下来,删除// ,使用clang-format off/on来禁用 clang-format。

void function() {
  // clang-format off
  return numbers
         | std::views::filter([](int n) { return n % 2 == 0; })
         | std::views::transform([](int n) { return n * 2; })
         | std::views::take(3)
         | std::ranges::to<std::vector>();
  // clang-format on
}

As for matrix, the option AlignArrayOfStructures might help.至于矩阵,选项AlignArrayOfStructures可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM