仅当当前行溢出clang格式时,才能在新线路上支撑

Brace on new line only if current line overflows clang format

本文关键字:新线 路上 溢出 clang 格式      更新时间:2023-10-16

我最近决定将.clang-format文件包括在我的C 项目中,以使代码更加均匀且易于阅读。我通常喜欢Google默认值,除了我想使用4个空间凹痕而不是两个。

问题是,当当前行溢出80个字符列限制时,它使某些语句更难读取。例如,在if语句中溢出:

if (some_condition || some_other_condition ||
    yet_another_condition) {
    // block starts here
}

yet_another_condition的对齐与if-block的开始匹配,这使得在没有某种休息的情况下很难阅读。理想情况下,在这种情况下我想发生的事情就是这样:

if (some_condition || some_other_condition ||
    yet_another_condition)
{
    // block starts here
}

但是,当当前行溢出到下一个时,我只希望在新线路上的开放支架,就像上面的示例一样。在所有其他情况下,我都希望在同一行上开放支架(用于/for/for/why/while/while/switch等。语句以及功能)。

可以在保留其余的Google默认设置完整时在我的.clang-format文件中指定此行为?

clang-format工具允许用BraceWrapping: Custom指定自定义支撑包装,但它的灵活性不够灵活,无法意识到所需的上下文。

提高可读性的替代解决方案是使用大于4的持续凹痕,例如ContinuationIndentWidth: 8