[PATCH] D75731: [clang-format] C# does not indent braced initializers as continuations

Jonathan B Coe via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Mar 6 04:24:17 PST 2020


jbcoe created this revision.
jbcoe added a reviewer: krasimir.
jbcoe added a project: clang-format.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

C# treats object initializers as braced init blocks. Braced init blocks are no longer indented as continuations.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75731

Files:
  clang/lib/Format/ContinuationIndenter.cpp
  clang/lib/Format/FormatToken.h
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/unittests/Format/FormatTestCSharp.cpp


Index: clang/unittests/Format/FormatTestCSharp.cpp
===================================================================
--- clang/unittests/Format/FormatTestCSharp.cpp
+++ clang/unittests/Format/FormatTestCSharp.cpp
@@ -527,14 +527,14 @@
 
   verifyFormat(R"(//
 Shape[] shapes = new[] {
-    new Circle {
-        Radius = 2.7281,
-        Colour = Colours.Red,
-    },
-    new Square {
-        Side = 101.1,
-        Colour = Colours.Yellow,
-    },
+  new Circle {
+    Radius = 2.7281,
+    Colour = Colours.Red,
+  },
+  new Square {
+    Side = 101.1,
+    Colour = Colours.Yellow,
+  },
 };)",
                Style);
 
@@ -542,8 +542,8 @@
   verifyFormat(R"(//
 Shape[] shapes = new[] { new Circle { Radius = 2.7281, Colour = Colours.Red },
                          new Square {
-                             Side = 101.1,
-                             Colour = Colours.Yellow,
+                           Side = 101.1,
+                           Colour = Colours.Yellow,
                          } };)",
                Style);
 
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1677,7 +1677,10 @@
       }
       break;
     case tok::l_square:
-      tryToParseLambda();
+      if (Style.isCSharp())
+        parseSquare();
+      else
+        tryToParseLambda();
       break;
     case tok::l_paren:
       parseParens();
Index: clang/lib/Format/FormatToken.h
===================================================================
--- clang/lib/Format/FormatToken.h
+++ clang/lib/Format/FormatToken.h
@@ -509,6 +509,9 @@
   /// Returns \c true if this tokens starts a block-type list, i.e. a
   /// list that should be indented with a block indent.
   bool opensBlockOrBlockTypeList(const FormatStyle &Style) const {
+    // C# Does not indent object initialisers as continuations.
+    if (is(tok::l_brace) && BlockKind == BK_BracedInit && Style.isCSharp())
+      return true;
     if (is(TT_TemplateString) && opensScope())
       return true;
     return is(TT_ArrayInitializerLSquare) || is(TT_ProtoExtensionLSquare) ||
Index: clang/lib/Format/ContinuationIndenter.cpp
===================================================================
--- clang/lib/Format/ContinuationIndenter.cpp
+++ clang/lib/Format/ContinuationIndenter.cpp
@@ -1047,6 +1047,9 @@
   if (NextNonComment->is(TT_ArraySubscriptLSquare)) {
     if (State.Stack.back().StartOfArraySubscripts != 0)
       return State.Stack.back().StartOfArraySubscripts;
+    // C# allows `["Key"] = value,` in braced init lists (Object initializers).
+    if (State.Stack.back().Tok->BlockKind == BK_BracedInit && Style.isCSharp())
+      return State.Stack.back().Indent;
     return ContinuationIndent;
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75731.248690.patch
Type: text/x-patch
Size: 2843 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200306/90017adc/attachment-0001.bin>


More information about the cfe-commits mailing list