]> granicus.if.org Git - clang/commitdiff
clang-format: Fix formatting of multiple lambdas in initializers.
authorDaniel Jasper <djasper@google.com>
Tue, 14 Jul 2015 11:26:14 +0000 (11:26 +0000)
committerDaniel Jasper <djasper@google.com>
Tue, 14 Jul 2015 11:26:14 +0000 (11:26 +0000)
Before:
  SomeFunction({[&] {
    // comment
  },
                [&] {
                  // comment
                }});

After:
  SomeFunction({[&] {
                  // comment
                },
                [&] {
                  // comment
                }});

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

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

index c3f069b39fe21167a18d3212eb7c22ccaa3d0cf9..dd56831a3bc523664522ef9d123ade9e1b51e927 100644 (file)
@@ -862,6 +862,8 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
   unsigned LastSpace = State.Stack.back().LastSpace;
   bool AvoidBinPacking;
   bool BreakBeforeParameter = false;
+  unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall,
+                                        State.Stack.back().NestedBlockIndent);
   if (Current.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare)) {
     if (Current.opensBlockTypeList(Style)) {
       NewIndent = State.Stack.back().NestedBlockIndent + Style.IndentWidth;
@@ -875,6 +877,8 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
         Current.isOneOf(TT_ArrayInitializerLSquare, TT_DictLiteral) ||
         Style.Language == FormatStyle::LK_Proto || !Style.BinPackArguments ||
         (NextNoComment && NextNoComment->is(TT_DesignatedInitializerPeriod));
+    if (Current.ParameterCount > 1)
+      NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1);
   } else {
     NewIndent = Style.ContinuationIndentWidth +
                 std::max(State.Stack.back().LastSpace,
@@ -922,8 +926,6 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State,
   bool NoLineBreak = State.Stack.back().NoLineBreak ||
                      (Current.is(TT_TemplateOpener) &&
                       State.Stack.back().ContainsUnwrappedBuilder);
-  unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall,
-                                        State.Stack.back().NestedBlockIndent);
   State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, LastSpace,
                                    AvoidBinPacking, NoLineBreak));
   State.Stack.back().NestedBlockIndent = NestedBlockIndent;
index 6aa7681ac59c3681bb6c152e647bcaf4037df192..d61dc7f662c2fb1fddf6216bd14a032c1332efe2 100644 (file)
@@ -9866,6 +9866,15 @@ TEST_F(FormatTest, FormatsLambdas) {
                "  return aaaaaaaaaaaaaaaaaaaaaaa != aaaaaaaaaaaaaaaaaaaaaaa;\n"
                "});",
                getLLVMStyleWithColumns(60));
+  verifyFormat("SomeFunction({[&] {\n"
+               "                // comment\n"
+               "              },\n"
+               "              [&] {\n"
+               "                // comment\n"
+               "              }});");
+  verifyFormat("SomeFunction({[&] {\n"
+               "  // comment\n"
+               "}});");
 
   // Lambdas with return types.
   verifyFormat("int c = []() -> int { return 2; }();\n");