Ind*_*oad 5 c++ code-formatting indentation c++11 clang-format
我有一些C++ 11代码与clang格式规则使用正常的indent(IndentWidth)4和continuation indent(ContinuationIndentWidth)为8.所以一个长函数看起来像这样:
// All indented "correctly"
void this_function_has_a_very_long_name_and_lots_of_parameters(
int parameter_a, int parameter_b, int parameter_c)
{
this_function_has_a_very_long_name_and_lots_of_parameters(
parameter_a, parameter_b, parameter_c);
}
Run Code Online (Sandbox Code Playgroud)
但是,我也有这样的数据(注意最后的逗号,以防止bin-packing并保持每行一项 - 在现实生活中这些数字不仅仅是1,2,3):
static std::vector<std::vector<int>> data{
{
1, // Comment
2,
3, // Comment
},
};
Run Code Online (Sandbox Code Playgroud)
考虑到Cpp11BracedListStyle设置为false,上面是我期望它的外观,所以它应该使用块缩进(4),而不是连续缩进(8).从clang格式的文档中,如果是这样的话true:
"重要区别: - 支撑列表中没有空格. - 在右括号之前没有换行符. - 使用延续缩进缩进,而不是使用块缩进."
所以,我希望看到使用"块缩进"(4).但是,我实际得到的是:
static std::vector<std::vector<int>> data{
{
1, // Comment - indented by 4 + 8!
2,
3, // Comment
},
};
Run Code Online (Sandbox Code Playgroud)
如您所见,"内部"初始化列表缩进8,但外部列表的元素仅缩进4(如预期的那样).
如果我Cpp11BracedListStyle改为true,则所有级别都缩进8(根据文档的预期):
static std::vector<std::vector<int>> data{
{
1,
2,
3,
},
};
Run Code Online (Sandbox Code Playgroud)
如何在不更改我在代码中其他地方使用的8空间延续缩进的情况下,使用clang格式来格式化这些列表?
NB clang-format版本是7.0.1 (tags/RELEASE_701/final),但根据配置程序,这种行为在各种版本中似乎是相同的.
一个.clang-format应用当前相关规则的简单文件是:
BasedOnStyle: LLVM
BreakBeforeBraces: Allman
ContinuationIndentWidth: 8
ColumnLimit: 72
Cpp11BracedListStyle: false
IndentWidth: 4
Run Code Online (Sandbox Code Playgroud)