Clang format:如何以这种方式格式化C结构初始值设定项
我使用linux内核.clang格式作为参考,但这一部分让我感到困扰 我怎样才能获得clang格式来格式化此代码Clang format:如何以这种方式格式化C结构初始值设定项,c,struct,initializer,clang-format,C,Struct,Initializer,Clang Format,我使用linux内核.clang格式作为参考,但这一部分让我感到困扰 我怎样才能获得clang格式来格式化此代码 const struct my_struct hello = {.id = 0, .modsmin = 10, .modsmax = 20, .strengthmin = 0,
const struct my_struct hello = {.id = 0,
.modsmin = 10,
.modsmax = 20,
.strengthmin = 0,
.strengthmax = 100,
.color = COLOR_FOREGROUND };
对此
const struct my_struct hello = {
.id = 0,
.modsmin = 10,
.modsmax = 20,
.strengthmin = 0,
.strengthmax = 100,
.color = COLOR_FOREGROUND
};
看看,似乎没有任何clangformat
样式选项可以满足您的需要
但是,您可以使用“最后一项后的逗号”技巧。在最后一个指定的初始值设定项后,最后一个右括号前加一个逗号。我相信这仍然是有效的C代码。然后,clangformat
会将每个初始值设定项放在单独的一行上,这会将缩进移回您要查找的内容。因此,输出将如下所示:
const struct my_struct hello = {
.id = 0,
.modsmin = 10,
.modsmax = 20,
.strengthmin = 0,
.strengthmax = 100,
.color = COLOR_FOREGROUND,
};
这种行为在任何地方都没有被记录(据我所知),所以我认为它可能在将来被改变。但是这种行为已经存在很多年了,而且有很多版本的clangformat
,所以我认为依赖它是合理的