如何用clang-format将短命名空间折叠为单行格式?
用clang-format将短命名空间转为单行格式
要实现把多行短命名空间合并为单行,你需要用到clang-format的**AllowShortNamespacesOnASingleLine**配置项,而不是AllowShortBlocksOnASingleLine——后者仅针对if/for等代码块,对命名空间不生效。
具体配置步骤:
- 在你的
.clang-format配置文件中添加或修改以下两项:
AllowShortNamespacesOnASingleLine: Always NamespaceIndentation: None
配置生效后,你提供的示例代码:
namespace Internal { class StatesEditorImageProvider; }
会被格式化为:
namespace Internal { class StatesEditorImageProvider; }
如果你的clang-format版本较旧,可能没有AllowShortNamespacesOnASingleLine选项,此时可以尝试ShortNamespaceLines配置(设置为1,代表仅当命名空间内代码行数≤1时合并为单行),不过更推荐使用新版本clang-format提供的专用选项。
内容的提问来源于stack exchange,提问作者benjamin button
相关产品推荐
相关产品推荐

