Astyle - 如何将不带大括号的条件格式化为 1TBS

Astyle - how to format condition without braces to 1TBS

我有一个程序使用 Astyle 将代码格式化为 1TBS。所以,如果我有这样的代码

if(condition)
    func(a, b);

变成这样

if(condition) {
    func(a, b);
}

问题是,当被调用函数中的参数被分成多行时,像这样:

if(condition)
    func(a, 
         b);

然后Astyle即使我试图用--add-brackets强制他也无法添加大括号。是否可以通过其他方式来实现?

我的命令现在看起来像这样:

astyle --style=1tbs --add-brackets  test.c

这可能看起来有点令人费解,但如果你有很多来源 添加大括号是您只需要一次的操作, 以下方案可能有效。

你可以坚持使用 astyle 但你暂时需要 uncrustify 以及我写的脚本 whatstyle.

在以下步骤中将 test1.c 替换为您的来源并保留备份 您的来源,因为它们将被修改。

向风格传授您资源的当前风格

whatstyle.py -f astyle --mode resilient --output astylerc test1.c

教授 uncrustify 资源的当前样式

whatstyle.py -f uncrustify --output uncrustify.cfg test1.c

告诉 uncrustify 总是给 ifs 添加大括号

( egrep -v mod_full_brace_if < uncrustify.cfg ; echo "mod_full_brace_if = force" ) \
  > uncrustify_addbrace.cfg

使用 uncrustify

以尽可能少的样式更改重新格式化您的源代码
uncrustify --replace -c uncrustify_addbrace.cfg test1.c

大括号已添加,现在用 astyle 重新转换为原始样式。

ARTISTIC_STYLE_OPTIONS=astylerc astyle test1.c

现在你的源代码看起来应该和以前差不多了,除了增加了大括号 虽然来回样式转换可能会有更多变化