[clang] eb682b8 - [clang-format] C# does not indent braced initializers as continuations

Jonathan Coe via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 9 10:37:52 PDT 2020


Author: Jonathan Coe
Date: 2020-03-09T17:36:21Z
New Revision: eb682b80274d5486d062e3f53040969f592277e4

URL: https://github.com/llvm/llvm-project/commit/eb682b80274d5486d062e3f53040969f592277e4
DIFF: https://github.com/llvm/llvm-project/commit/eb682b80274d5486d062e3f53040969f592277e4.diff

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

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

Reviewers: krasimir

Reviewed By: krasimir

Subscribers: cfe-commits

Tags: #clang-format, #clang

Differential Revision: https://reviews.llvm.org/D75731

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/FormatToken.h b/clang/lib/Format/FormatToken.h
index 072e1ad565c2..1b885b518f0d 100644
--- a/clang/lib/Format/FormatToken.h
+++ b/clang/lib/Format/FormatToken.h
@@ -509,6 +509,9 @@ struct FormatToken {
   /// 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) ||

diff  --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 58cc679af9e1..00447ebdf5a9 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -1677,7 +1677,10 @@ bool UnwrappedLineParser::parseBracedList(bool ContinueOnSemicolons,
       }
       break;
     case tok::l_square:
-      tryToParseLambda();
+      if (Style.isCSharp())
+        parseSquare();
+      else
+        tryToParseLambda();
       break;
     case tok::l_paren:
       parseParens();

diff  --git a/clang/unittests/Format/FormatTestCSharp.cpp b/clang/unittests/Format/FormatTestCSharp.cpp
index e3038898dcdd..a22f48676065 100644
--- a/clang/unittests/Format/FormatTestCSharp.cpp
+++ b/clang/unittests/Format/FormatTestCSharp.cpp
@@ -527,31 +527,28 @@ TEST_F(FormatTestCSharp, CSharpObjectInitializers) {
 
   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);
 
   // Omitted final `,`s will change the formatting.
   verifyFormat(R"(//
 Shape[] shapes = new[] { new Circle { Radius = 2.7281, Colour = Colours.Red },
-                         new Square {
-                             Side = 101.1,
-                             Colour = Colours.Yellow,
-                         } };)",
+                         new Square { Side = 101.1, Colour = Colours.Yellow } };)",
                Style);
 
   // Lambdas can be supplied as initialiser arguments.
   verifyFormat(R"(//
 private Transformer _transformer = new X.Y {
-    Filler = (Shape shape) => { return new Transform.Fill(shape, RED); },
-    Scaler = (Shape shape) => { return new Transform.Resize(shape, 0.1); },
+  Filler = (Shape shape) => { return new Transform.Fill(shape, RED); },
+  Scaler = (Shape shape) => { return new Transform.Resize(shape, 0.1); },
 };)",
                Style);
 }


        


More information about the cfe-commits mailing list