[clang] [clang-format] Add new option: WrapNamespaceBodyWithNewlines (PR #106145)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Mon Oct 14 00:20:53 PDT 2024
================
@@ -28104,6 +28104,242 @@ TEST_F(FormatTest, BreakBinaryOperations) {
Style);
}
+TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesNever) {
+ auto Style = getLLVMStyle();
+ Style.FixNamespaceComments = false;
+
+ // Empty namespace.
+ verifyFormat("namespace N {}", Style);
+
+ // Single namespace.
+ verifyFormat("namespace N {\n"
+ "int f1(int a) { return 2 * a; }\n"
+ "}",
+ Style);
+
+ // Nested namespace.
+ verifyFormat("namespace N1 {\n"
+ "namespace N2 {\n"
+ "int a = 1;\n"
+ "}\n"
+ "}",
+ Style);
+
+ Style.CompactNamespaces = true;
+
+ verifyFormat("namespace N1 { namespace N2 {\n"
+ "int a = 1;\n"
+ "}}",
+ Style);
+
+ // Removing empty lines.
+ verifyFormat("namespace N {\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "}",
+ "namespace N {\n"
+ "\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "\n"
+ "}",
+ Style);
+
+ Style.MaxEmptyLinesToKeep = 0;
+
+ verifyFormat("namespace N {\n"
+ "int a = 1;\n"
+ "}",
+ "namespace N {\n"
+ "\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "\n"
+ "}",
+ Style);
+}
+
+TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesAlways) {
+ auto Style = getLLVMStyle();
+ Style.FixNamespaceComments = false;
+ Style.WrapNamespaceBodyWithEmptyLines = FormatStyle::WNBWELS_Always;
+
+ // Empty namespace.
+ verifyFormat("namespace N {}", Style);
+
+ // Single namespace.
+ verifyFormat("namespace N {\n"
+ "\n"
+ "int f1(int a) { return 2 * a; }\n"
+ "\n"
+ "}",
+ Style);
+
+ // Nested namespace.
+ verifyFormat("namespace N1 {\n"
+ "namespace N2 {\n"
+ "\n"
+ "int a = 1;\n"
+ "\n"
+ "}\n"
+ "}",
+ Style);
+
+ // Removing empty lines.
----------------
owenca wrote:
IMO `Always` should not remove empty lines.
https://github.com/llvm/llvm-project/pull/106145
More information about the cfe-commits
mailing list