使用 astyle 在一行上格式化C++中的嵌套命名空间

Format nested namespaces in C++ on one line using astyle

本文关键字:C++ 格式化 命名空间 嵌套 一行 astyle 使用      更新时间:2023-10-16

在我公司的编码约定中,我们将嵌套命名空间格式化为一行。例如:

namespace Foo { namespace Bar {
...
}} // Foo::Bar

我正在尝试使用 astyle 强制执行这种样式,但在文档中找不到任何内容 http://astyle.sourceforge.net/astyle.html

当我运行以下 astyle 命令时(忽略不相关的选项(

astyle --style=allman --add-brackets --align-reference=name --align-pointer=name --attach-namespaces --pad-header --pad-oper --unpad-paren -n <filename>

我最终将每个嵌套命名空间放在单独的行上,如下所示:

namespace Foo {
namespace Bar {
...
}
}

尝试修改astyle源代码。找到方法ASFormater::isOkToBreakBlock并在开头插入:

if (isBraceType(braceType, NAMESPACE_TYPE))
return false;

确保它不会破坏整个格式。

附言希望astyle团队能够听到并使用选项进行正确的更改。