我正在为一个大型库设置样式格式检查。目前,我们的团队风格指南更喜欢使用“2 行”语句,而不是一行 if 语句,如下所示
if(statementIsTrue)
{ printf("True"); }
else
{
anExampleBlock = true;
elsestatement.function();
}
注意 if 块如何部分缩进 2 个空格,以与 else 语句对齐。该样式的缩进为 3 个空格。此外,出于对称目的,分号和右大括号之间有 2 个空格。
关于风格的其他注意事项。大括号always换行。这就是上述背后的意图。对于所有 if 语句、函数、类等,左大括号另起一行。
我们如何调整 clang-format 来执行此任务?
编辑:更多块示例
/**
* This is just example code for formatting, not meant to be useful code
*/
bool testFunction(int largeNumber, int randomNumber)
{
for (int j = 0; j < largeNumber; j++)
{
if (j == randomNumber)
{ return true; }
else
{ print("not found"); }
}
}
int main()
{
//Three space indentation (tab characters should not be used anywhere)
//Single space should exist between if, else if, for, while, etc. and (...)
if (testFunction(testParm1, testParm2))
{
print("Hello");
return true;
}
}
我认为,如果您使用以下设置编辑项目存储库中的
.clang-format
文件,它应该可以工作。
请注意 - 我还没有对此进行测试,但阅读文档后,我觉得这很合适。
BreakBeforeBraces:Allman,
AllowShortBlocksOnASingleLine: Always,
AllowShortIfStatementsOnASingleLine: WithoutElse
如果这不起作用,请在Clang-format style options
中尝试这些字段的可能选项希望这有帮助!