因此,在弄乱了 clang 格式代码并制作了一些补丁之后,这是我的两分钱:
您可以尝试以下一件事:
std::vector<std::string> get_vec()
{
return std::vector<std::string> { //
"this is a test", //
"some of the lines are longer", //
"than other, but I would like", //
"to keep them on separate lines" //
};
}
这样做的优点是// clang-format off
就是,如果您稍后更改制表符宽度或其他选项,这些代码行仍然会获得这些格式更改,因此您无需手动进入// clang-format off
地区来修复它。然而,它仍然有点黑客,YMMV。
最终,clang-format
很大程度上是在整个代码库上采用统一的格式,确保程序中所有字符串文字的格式都相同。如果您想对换行决策进行微观控制,这并不符合该工具的精神,您必须执行诸如禁用它之类的操作。
这有时会令人沮丧,尤其是。当你想用数组做事情并对齐列或其他东西时——例如,下面是来自 lua C api 的一些自然代码:
static luaL_Reg const methods[] = {
{"matches", &dispatch::intf_match_unit},
{"to_recall", &dispatch::intf_put_recall_unit},
{"to_map", &dispatch::intf_put_unit},
{"erase", &dispatch::intf_erase_unit},
{"clone", intf_copy_unit},
{"extract", &dispatch::intf_extract_unit},
{"advance", intf_advance_unit},
};
当 clang-format 运行时,它通常不会对齐右列,而是在逗号后面放置固定数量的空格,据我所知,您对此无能为力。
或者,如果您有 4 x 4 矩阵供 OpenGL 使用:
constexpr float shadow_skew_hardcoded[16] =
{ 1.0f, 0.0f, 0.0f, 0.0f,
0.5f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f };
如果你让 clang-format 运行这样的东西,它只会破坏它们,而且据我所知,没有简单的方法可以让它很好地格式化它们,所以你只需要诉诸“大量琐碎的评论”黑客,或者使用当你有这样的东西时,请关闭 clang-format 。这些只是该工具的固有局限性。如果您不喜欢做这样的事情,那么它可能不适合您。