有没有办法配置 clang-format 以将嵌套的命名空间声明保持在同一行?

Is there a way to configure clang-format to keep nested namespace declarations on the same line?

在我工作的代码库中,我们总是这样声明嵌套的命名空间:

namespace foo { namespace detail {

// stuff

} }  // foo::detail namespace

我还没有找到配置 clang-format not 的方法来将其分成多行:

namespace foo {
namespace detail {

// stuff

}
}  // foo::detail namespace

我试过 BreakBeforeBraces 配置,我研究了 clang 3.8 中新的 BraceWrapping 配置,但都没有成功。

是否可以在不使用 // clang-format [on/off] 注释代码的情况下执行此操作?

原来这是clang-format团队考虑过的一个特性,但是被否决了。有关其他详细信息,请参阅 https://llvm.org/bugs/show_bug.cgi?id=17928

clang-format 6.0 的 "CompactNamespaces: true" 选项完全符合您的要求。参见 http://clang.llvm.org/docs/ClangFormatStyleOptions.html