[PATCH] D104388: [clang-format] PR50727 C# Invoke Lamda Expression indentation incorrect

MyDeveloperDay via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 16 08:12:10 PDT 2021


MyDeveloperDay created this revision.
MyDeveloperDay added reviewers: curdeius, HazardyKnusperkeks, exv, lbk, jbcoe.
MyDeveloperDay added projects: clang, clang-format.
MyDeveloperDay requested review of this revision.

https://bugs.llvm.org/show_bug.cgi?id=50727

When processing C# Lambda expression in the indentation can goes a little wrong, 
resulting the the closing } being at the wrong indentation level and meaning the remaining part of the file is
incorrectly indented.

This revision tries to address that for C#

This can be a fairly common pattern for when C# wants to peform a UI action from a thread, 
and it wants to invoke that action on the main thread

  class A
  {
  
      void foo()
      {
      Dispatcher.Invoke(DispatcherPriority.Render, (Action)(() => {
                                                    lock (A)
                                                    {
                                                        if (true)
                                                        {
                                                            A.Remove(item);
      }
  }
  }));
  }
  
  void bar()
  {
  ...
  }


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D104388

Files:
  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
@@ -640,6 +640,34 @@
     };
 })",
                MicrosoftStyle);
+
+  verifyFormat("void bar()\n"
+               "{\n"
+               "    Function(Val, (Action)(() => {\n"
+               "                 lock (mylock)\n"
+               "                 {\n"
+               "                     if (true)\n"
+               "                     {\n"
+               "                         A.Remove(item);\n"
+               "                     }\n"
+               "                 }\n"
+               "             }));\n"
+               "}",
+               MicrosoftStyle);
+
+  verifyFormat("void baz()\n"
+               "{\n"
+               "    Function(Val, (Action)(() => {\n"
+               "                 using (var a = new Lock())\n"
+               "                 {\n"
+               "                     if (true)\n"
+               "                     {\n"
+               "                         A.Remove(item);\n"
+               "                     }\n"
+               "                 }\n"
+               "             }));\n"
+               "}",
+               MicrosoftStyle);
 }
 
 TEST_F(FormatTestCSharp, CSharpObjectInitializers) {
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -1927,6 +1927,12 @@
         parseBracedList();
       }
       break;
+    case tok::equal:
+      if (Style.isCSharp() && FormatTok->is(TT_FatArrow))
+        parseBracedList();
+      else
+        nextToken();
+      break;
     case tok::kw_class:
       if (Style.Language == FormatStyle::LK_JavaScript)
         parseRecord(/*ParseAsExpr=*/true);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D104388.352442.patch
Type: text/x-patch
Size: 1976 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20210616/34ddd3d6/attachment-0001.bin>


More information about the cfe-commits mailing list