Skip to content

Commit

Permalink
[clang-format] Disable OuterScope lambda indentation behaviour for co…
Browse files Browse the repository at this point in the history
…nstructor initializers (#66755)

By default, OuterScope aligns lambdas to the beginning of the current
line. This makes sense for most types of statements within code blocks
but leads to unappealing and misleading indentation for lambdas within
constructor initializers.
  • Loading branch information
jp4a50 committed Sep 28, 2023
1 parent 6a2bc48 commit 82001e0
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 12 deletions.
4 changes: 2 additions & 2 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3788,8 +3788,8 @@ the configuration (without a prefix: ``Auto``).
});

* ``LBI_OuterScope`` (in configuration: ``OuterScope``)
Align lambda body relative to the indentation level of the outer scope
the lambda signature resides in.
For statements within block scope, align lambda body relative to the
indentation level of the outer scope the lambda signature resides in.

.. code-block:: c++

Expand Down
4 changes: 2 additions & 2 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -2879,8 +2879,8 @@ struct FormatStyle {
/// });
/// \endcode
LBI_Signature,
/// Align lambda body relative to the indentation level of the outer scope
/// the lambda signature resides in.
/// For statements within block scope, align lambda body relative to the
/// indentation level of the outer scope the lambda signature resides in.
/// \code
/// someMethod(
/// [](SomeReallyLongLambdaSignatureArgument foo) {
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1955,7 +1955,8 @@ void ContinuationIndenter::moveStatePastScopeCloser(LineState &State) {

void ContinuationIndenter::moveStateToNewBlock(LineState &State) {
if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope &&
State.NextToken->is(TT_LambdaLBrace)) {
State.NextToken->is(TT_LambdaLBrace) &&
!State.Line->MightBeFunctionDecl) {
State.Stack.back().NestedBlockIndent = State.FirstIndent;
}
unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent;
Expand Down
53 changes: 46 additions & 7 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22537,10 +22537,12 @@ TEST_F(FormatTest, FormatsLambdas) {
" }\n"
"}",
Style);
verifyFormat("std::sort(v.begin(), v.end(),\n"
" [](const auto &foo, const auto &bar) {\n"
" return foo.baz < bar.baz;\n"
"});",
verifyFormat("void test() {\n"
" std::sort(v.begin(), v.end(),\n"
" [](const auto &foo, const auto &bar) {\n"
" return foo.baz < bar.baz;\n"
" });\n"
"};",
Style);
verifyFormat("void test() {\n"
" (\n"
Expand All @@ -22566,6 +22568,12 @@ TEST_F(FormatTest, FormatsLambdas) {
" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx); \\\n"
" }",
Style);
verifyFormat("#define SORT(v) \\\n"
" std::sort(v.begin(), v.end(), \\\n"
" [](const auto &foo, const auto &bar) { \\\n"
" return foo.baz < bar.baz; \\\n"
" });",
Style);
verifyFormat("void foo() {\n"
" aFunction(1, b(c(foo, bar, baz, [](d) {\n"
" auto f = e(d);\n"
Expand All @@ -22589,9 +22597,40 @@ TEST_F(FormatTest, FormatsLambdas) {
verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
" AnotherLongClassName baz)\n"
" : baz{baz}, func{[&] {\n"
" auto qux = bar;\n"
" return aFunkyFunctionCall(qux);\n"
"}} {}",
" auto qux = bar;\n"
" return aFunkyFunctionCall(qux);\n"
" }} {}",
Style);
verifyFormat("void foo() {\n"
" class Foo {\n"
" public:\n"
" Foo()\n"
" : qux{[](int quux) {\n"
" auto tmp = quux;\n"
" return tmp;\n"
" }} {}\n"
"\n"
" private:\n"
" std::function<void(int quux)> qux;\n"
" };\n"
"}",
Style);
Style.BreakConstructorInitializers = FormatStyle::BCIS_AfterColon;
verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
" AnotherLongClassName baz) :\n"
" baz{baz}, func{[&] {\n"
" auto qux = bar;\n"
" return aFunkyFunctionCall(qux);\n"
" }} {}",
Style);
Style.PackConstructorInitializers = FormatStyle::PCIS_Never;
verifyFormat("Namespace::Foo::Foo(LongClassName bar,\n"
" AnotherLongClassName baz) :\n"
" baz{baz},\n"
" func{[&] {\n"
" auto qux = bar;\n"
" return aFunkyFunctionCall(qux);\n"
" }} {}",
Style);
Style.AlignAfterOpenBracket = FormatStyle::BAS_AlwaysBreak;
// FIXME: The following test should pass, but fails at the time of writing.
Expand Down

0 comments on commit 82001e0

Please sign in to comment.