[PATCH] D46879: [clang-format] Fix putting ObjC message arguments in one line for multiline receiver

Jacek Olesiak via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 17 01:39:11 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL332582: [clang-format] Fix putting ObjC message arguments in one line for multiline… (authored by jolesiak, committed by ).
Herald added a subscriber: llvm-commits.

Repository:
  rL LLVM

https://reviews.llvm.org/D46879

Files:
  cfe/trunk/lib/Format/ContinuationIndenter.cpp
  cfe/trunk/unittests/Format/FormatTestObjC.cpp


Index: cfe/trunk/lib/Format/ContinuationIndenter.cpp
===================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp
@@ -1073,8 +1073,34 @@
   if (Current.isMemberAccess())
     State.Stack.back().StartOfFunctionCall =
         !Current.NextOperator ? 0 : State.Column;
-  if (Current.is(TT_SelectorName))
+  if (Current.is(TT_SelectorName) &&
+      !State.Stack.back().ObjCSelectorNameFound) {
     State.Stack.back().ObjCSelectorNameFound = true;
+
+    // Reevaluate whether ObjC message arguments fit into one line.
+    // If a receiver spans multiple lines, e.g.:
+    //   [[object block:^{
+    //     return 42;
+    //   }] a:42 b:42];
+    // BreakBeforeParameter is calculated based on an incorrect assumption
+    // (it is checked whether the whole expression fits into one line without
+    // considering a line break inside a message receiver).
+    if (Current.Previous && Current.Previous->closesScope() &&
+        Current.Previous->MatchingParen &&
+        Current.Previous->MatchingParen->Previous) {
+      const FormatToken &CurrentScopeOpener =
+          *Current.Previous->MatchingParen->Previous;
+      if (CurrentScopeOpener.is(TT_ObjCMethodExpr) &&
+          CurrentScopeOpener.MatchingParen) {
+        int NecessarySpaceInLine =
+            getLengthToMatchingParen(CurrentScopeOpener, State.Stack) +
+            CurrentScopeOpener.TotalLength - Current.TotalLength - 1;
+        if (State.Column + Current.ColumnWidth + NecessarySpaceInLine <=
+            Style.ColumnLimit)
+          State.Stack.back().BreakBeforeParameter = false;
+      }
+    }
+  }
   if (Current.is(TT_CtorInitializerColon) &&
       Style.BreakConstructorInitializers != FormatStyle::BCIS_AfterColon) {
     // Indent 2 from the column, so:
Index: cfe/trunk/unittests/Format/FormatTestObjC.cpp
===================================================================
--- cfe/trunk/unittests/Format/FormatTestObjC.cpp
+++ cfe/trunk/unittests/Format/FormatTestObjC.cpp
@@ -792,6 +792,35 @@
                "      a = 42;\n"
                "    }];");
 
+  // Message receiver taking multiple lines.
+  Style.ColumnLimit = 20;
+  // Non-corner case.
+  verifyFormat("[[object block:^{\n"
+               "  return 42;\n"
+               "}] a:42 b:42];");
+  // Arguments just fit into one line.
+  verifyFormat("[[object block:^{\n"
+               "  return 42;\n"
+               "}] aaaaaaa:42 b:42];");
+  // Arguments just over a column limit.
+  verifyFormat("[[object block:^{\n"
+               "  return 42;\n"
+               "}] aaaaaaa:42\n"
+               "        bb:42];");
+  // Non-corner case.
+  verifyFormat("[[object aaa:42\n"
+               "           b:42]\n"
+               "    cc:42 d:42];");
+  // Arguments just fit into one line.
+  verifyFormat("[[object aaa:42\n"
+               "           b:42]\n"
+               "    cccccc:42 d:42];");
+  // Arguments just over a column limit.
+  verifyFormat("[[object aaa:42\n"
+               "           b:42]\n"
+               "    cccccc:42\n"
+               "        dd:42];");
+
   Style.ColumnLimit = 70;
   verifyFormat(
       "void f() {\n"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D46879.147266.patch
Type: text/x-patch
Size: 3244 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180517/70255697/attachment.bin>


More information about the llvm-commits mailing list