[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.
+  verifyFormat("namespace N {\n"
+               "\n"
+               "int a = 1;\n"
+               "\n"
+               "}",
+               "namespace N {"
+               "\n"
+               "\n"
+               "int a = 1;\n"
+               "\n"
+               "\n"
+               "}",
+               Style);
+
+  Style.CompactNamespaces = true;
+
+  // Nested namespace.
+  verifyFormat("namespace N1 { namespace N2 {\n"
+               "\n"
+               "int a = 1;\n"
+               "\n"
+               "}}",
+               Style);
+
+  Style.MaxEmptyLinesToKeep = 2;
+
+  // Nested namespace.
+  verifyNoChange("namespace N1 { namespace N2 {\n"
+                 "\n"
+                 "\n"
+                 "int a = 1;\n"
+                 "\n"
+                 "\n"
+                 "}}",
+                 Style);
+
+  Style.CompactNamespaces = false;
+
+  // Empty namespace.
+  verifyFormat("namespace N {}", Style);
+
+  // Single namespace.
+  verifyNoChange("namespace N {\n"
+                 "\n"
+                 "\n"
+                 "int a = 1;\n"
+                 "\n"
+                 "\n"
+                 "}",
+                 Style);
+
+  // Nested namespace.
+  verifyNoChange("namespace N1 {\n"
+                 "namespace N2 {\n"
+                 "\n"
+                 "\n"
+                 "int a = 1;\n"
+                 "\n"
+                 "\n"
+                 "}\n"
+                 "}",
+                 Style);
+}
+
+TEST_F(FormatTest, WrapNamespaceBodyWithEmptyLinesLeave) {
----------------
owenca wrote:

IMO we don't need this test at all as `Leave` is a no-op for `WrapNamespaceBodyWithEmptyLines`.

https://github.com/llvm/llvm-project/pull/106145


More information about the cfe-commits mailing list