[PATCH] D72401: Fixes for spaces around C# object initializers

Jonathan B Coe via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jan 8 08:56:15 PST 2020


jbcoe created this revision.
jbcoe added a project: clang-format.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
jbcoe added reviewers: MyDeveloperDay, klimek.

Fix spaces around typename and [] in C# object initializers.

Handling of newlines and binpacking will be addressed in a following revision - currently a trailing ',' is needed after the last argument to an object initializer to keep object initializer arguments on distinct lines.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72401

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTestCSharp.cpp


Index: clang/unittests/Format/FormatTestCSharp.cpp
===================================================================
--- clang/unittests/Format/FormatTestCSharp.cpp
+++ clang/unittests/Format/FormatTestCSharp.cpp
@@ -374,5 +374,22 @@
                Style);
 }
 
+TEST_F(FormatTestCSharp, CSharpObjectInitializers) {
+  FormatStyle Style = getGoogleStyle(FormatStyle::LK_CSharp);
+  Style.SpaceBeforeParens = FormatStyle::SBPO_Always;
+
+  verifyFormat("Shape[] shapes = new[] {\n"
+               "    new Circle {\n"
+               "        Radius = 2.7281,\n"
+               "        Colour = Colours.Red,\n"
+               "    },\n"
+               "    new Square {\n"
+               "        Side = 101.1,\n"
+               "        Colour = Colours.Yellow,\n"
+               "    },\n"
+               "};",
+               Style);
+}
+
 } // namespace format
 } // end namespace clang
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2862,6 +2862,13 @@
     if (Right.is(tok::l_paren))
       if (Left.is(tok::kw_using))
         return spaceRequiredBeforeParens(Left);
+    // space between ']' and '{'
+    if (Left.is(tok::r_square) && Right.is(tok::l_brace))
+      return true;
+    // space before '{' in "new MyType {"
+    if (Left.is(TT_Unknown) && Right.is(tok::l_brace) && Left.Previous &&
+        Left.Previous->is(tok::kw_new))
+      return true;
   } else if (Style.Language == FormatStyle::LK_JavaScript) {
     if (Left.is(TT_JsFatArrow))
       return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72401.236839.patch
Type: text/x-patch
Size: 1640 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200108/6b32d745/attachment.bin>


More information about the cfe-commits mailing list