From cd67166a3795b74c801fcfc99b83e870356344e9 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Wed, 13 May 2015 16:09:21 +0000 Subject: [PATCH] clang-format: Improve nested block / lambda indentation when wrapping 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 | 3 +++ unittests/Format/FormatTest.cpp | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 17a536d7f6..720b6140b2 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -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(); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index c44708fbf6..d5614b8430 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -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"); -- 2.40.0