【问题标题】:clang-format removes indents in if condition when there is a line break当有换行符时,clang-format 删除 if 条件中的缩进
【发布时间】:2021-06-09 15:37:29
【问题描述】:

我有这个功能:

bool WS::receive(MB *const buffer) const {
    updateLastPackage();
    if ((!lastPackage_.empty()) &&
            (lastPackage_.size() <= buffer->capacity()) &&
            (lastUpdate_.getTime() <= settings_.refreshPeriod)) {
        buffer->clear();
        (*buffer) << lastPackage_;
        return true;
    }
    return false;
}

使用我的样式文件应用 clang-format 后:

bool WS::receive(MB *const buffer) const {
    updateLastPackage();
    if ((!lastPackage_.empty()) &&
    (lastPackage_.size() <= buffer->capacity()) &&
    (lastUpdate_.getTime() <= settings_.refreshPeriod)) {
        buffer->clear();
        (*buffer) << lastPackage_;
        return true;
    }
    return false;
}

您可以看到当有换行符时,clang-format 会删除 if 条件中的缩进。那么,如何保持缩进不变呢?这也发生在模板中。

我的样式文件,基于 LLVM: https://pastebin.com/ZDwZfBud

【问题讨论】:

  • 它似乎被 AlignOperands 覆盖,但要真正添加一些缩进,您需要修改 ContinuationIndentWidth,这可能会更改代码的其他部分。
  • 您的意思是删除if 条件中的缩进,而不是函数参数?
  • @cigien 是的,对不起,我的错

标签: c++ clang-format


【解决方案1】:

感谢 Yksisarvinen。

解决方案:

AlignOperands: false
ContinuationIndentWidth: 8

【讨论】:

    猜你喜欢
    • 2021-09-02
    • 1970-01-01
    • 1970-01-01
    • 2021-06-12
    • 1970-01-01
    • 2022-10-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    相关资源
    最近更新 更多