]> granicus.if.org Git - clang/commitdiff
clang-format: Improve nested block / lambda indentation when wrapping
authorDaniel Jasper <djasper@google.com>
Wed, 13 May 2015 16:09:21 +0000 (16:09 +0000)
committerDaniel Jasper <djasper@google.com>
Wed, 13 May 2015 16:09:21 +0000 (16:09 +0000)
before binary/ternary operators.

Basically, it doesn't seem right to indent a nested block aligned to a
binary or ternary operator.

Before:
  int i = aaaaaa ? 1  //
                 : [] {
                   return 2;  //
                 }();
  llvm::errs() << "number of twos is "
               << std::count_if(v.begin(), v.end(), [](int x) {
                 return x == 2;  // force break
               });

After:
  int i = aaaaaa ? 1  //
                 : [] {
                     return 2;  //
                   }();
  llvm::errs() << "number of twos is "
               << std::count_if(v.begin(), v.end(), [](int x) {
                    return x == 2;  // force break
                  });

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@237263 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTest.cpp

index 17a536d7f6881e707767c1b840dacdbe6375a73a..720b6140b2fad40777823e1ed2ffad0a6d1e236d 100644 (file)
@@ -667,6 +667,9 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
       State.Stack.back().AvoidBinPacking = true;
     State.Stack.back().BreakBeforeParameter = false;
   }
+  if (Current.isOneOf(TT_BinaryOperator, TT_ConditionalExpr) && Newline)
+    State.Stack.back().NestedBlockIndent =
+        State.Column + Current.ColumnWidth + 1;
 
   // Insert scopes created by fake parenthesis.
   const FormatToken *Previous = Current.getPreviousNonComment();
index c44708fbf6c3af6bf11c2ef34a8edb7d260552ed..d5614b84305a5085b6ece67a6f0c5a8da02f6efc 100644 (file)
@@ -9992,6 +9992,14 @@ TEST_F(FormatTest, FormatsLambdas) {
   verifyFormat("auto my_lambda = [](const string &some_parameter) {\n"
                "  return some_parameter.size();\n"
                "};");
+  verifyFormat("int i = aaaaaa ? 1 //\n"
+               "               : [] {\n"
+               "                   return 2; //\n"
+               "                 }();");
+  verifyFormat("llvm::errs() << \"number of twos is \"\n"
+               "             << std::count_if(v.begin(), v.end(), [](int x) {\n"
+               "                  return x == 2; // force break\n"
+               "                });");
 
   // Lambdas with return types.
   verifyFormat("int c = []() -> int { return 2; }();\n");