Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions clang/lib/Format/ContinuationIndenter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,13 +325,35 @@ bool ContinuationIndenter::canBreak(const LineState &State) {
if (Current.isMemberAccess() && CurrentState.ContainsUnwrappedBuilder)
return false;

// Don't create a 'hanging' indent if there are multiple blocks in a single
// statement and we are aligning lambda blocks to their signatures.
// Force a lambda onto a new line so that we don't create a 'hanging' indent
// if there are multiple blocks in a single statement and we are aligning
// lambda blocks to their signatures.
if (Previous.is(tok::l_brace) && State.Stack.size() > 1 &&
State.Stack[State.Stack.size() - 2].NestedBlockInlined &&
State.Stack[State.Stack.size() - 2].HasMultipleNestedBlocks) {
return Style.isCpp() &&
Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope;
if (!Style.isCpp())
return false;

if (Style.LambdaBodyIndentation == FormatStyle::LBI_OuterScope)
return true;

// Make sure to push lambdas to a new line when they are an argument with
// other arguments preceding them.
if (State.Stack[State.Stack.size() - 2].StartOfFunctionCall > 0)
return false;

// Only force a new line if it is not just going to create a worse hanging
// indent. Otherwise, based on the ContinuationIndentWidth, we could end up
// more indented than we would've been. To avoid odd looking breaks, make
// sure we save at least IndentWidth.
if (State.Stack.size() > 2 &&
State.Stack[State.Stack.size() - 3].Indent +
Style.ContinuationIndentWidth + Style.IndentWidth <
State.Stack[State.Stack.size() - 2].Indent) {
return false;
}

return true;
}

// Don't break after very short return types (e.g. "void") as that is often
Expand Down
11 changes: 11 additions & 0 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23813,6 +23813,17 @@ TEST_F(FormatTest, FormatsLambdas) {
" }};\n"
"}",
LLVMWithBeforeLambdaBody);
verifyFormat("if ([]()\n"
" {\n"
" return true;\n"
" }()) {\n"
"}",
LLVMWithBeforeLambdaBody);
verifyFormat("fun([]()\n"
" {\n"
" return 17;\n"
" });",
LLVMWithBeforeLambdaBody);

LLVMWithBeforeLambdaBody.AllowShortLambdasOnASingleLine =
FormatStyle::ShortLambdaStyle::SLS_Empty;
Expand Down
Loading