如何在复杂的赋值表达式中对Lambda函数进行缩进,缩进为4个空格。

huangapple go评论64阅读模式
英文:

How to indent lambdas in complex assignment expressions by 4 spaces

问题

I have an assignment expression like this (formatted by clang-format):

auto typeParams = node->typeParams | std::views::transform([&](auto p) {
    return m_irCtx.make(IRGenericType(p));
});

I would like to have it formatted like this:

auto typeParams = node->typeParams | std::views::transform([&](auto p) {
    return m_irCtx.make(IRGenericType(p));
});

Which .clang-format option controls how lambdas are indented relative to the parent scope? How can I achieve this?

EDIT: Setting LambdaBodyIndentation to OuterScope does not change anything.

英文:

I have an assignment expression like this (formatted by clang-format):

auto typeParams = node->typeParams | std::views::transform([&](auto p) {
                      return m_irCtx.make(IRGenericType(p));
                  });

I would like to have it formatted like this:

auto typeParams = node->typeParams | std::views::transform([&](auto p) {
    return m_irCtx.make(IRGenericType(p));
});

Which .clang-format option controls how lambdas are indented relative to the parent scope? How can I achieve this?

EDIT: Setting LambdaBodyIndentation to OuterScope does not change anything

答案1

得分: 1

这种特殊情况(如果它是唯一的情况或仅有少数情况之一),我会手动格式化:

// clang-format off
auto typeParams = node->typeParams | std::views::transform([&](auto p) {
    return m_irCtx.make(IRGenericType(p));
});
// clang-format on

是的,这有点作弊。如果clang-format的输出符合您的要求,它会工作得相当不错,但您无法配置它执行任何其他操作。在这些情况下,您可以关闭自动化并手动进行操作。

查看相关代码,我认为右括号和圆括号应至少缩进一个级别。不过,我也不确定。如果您希望它保持这种格式,那就按照这种方式编写代码,不要让clang-format修改它。

英文:

In this specific case (and if it's the only case or only one of a few) I'd just format by hand:

// clang-format off
auto typeParams = node->typeParams | std::views::transform([&](auto p) {
    return m_irCtx.make(IRGenericType(p));
});
// clang-format on

Yes, that's cheating. Clang-format works reasonably well if you're happy with what it does but you cannot configure it to do anything. In these cases, you're welcome to switch the automation off and do it manually.

Looking at the code in question, I'd say that the closing brace and parenthesis should be indented by at least one level. However, what do I know. If you want it like this, write it like this and don't let clang-format touch it.

huangapple
  • 本文由 发表于 2023年2月19日 03:09:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75495754.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定