From: Daniel Jasper Date: Tue, 7 Apr 2015 08:20:35 +0000 (+0000) Subject: clang-format: Improve nested block formatting. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cd779b6b50af1b2192f3c99370955c53dbfa09ab;p=clang clang-format: Improve nested block formatting. Before: functionA(functionB({ int i; int j; }), aaaa, bbbb, cccc); After: functionA(functionB({ int i; int j; }), aaaa, bbbb, cccc); git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@234304 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 3e4bf3e88a..a2a68bd6d4 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -849,7 +849,8 @@ void ContinuationIndenter::moveStatePastScopeOpener(LineState &State, bool NoLineBreak = State.Stack.back().NoLineBreak || (Current.is(TT_TemplateOpener) && State.Stack.back().ContainsUnwrappedBuilder); - unsigned NestedBlockIndent = State.Stack.back().NestedBlockIndent; + unsigned NestedBlockIndent = std::max(State.Stack.back().StartOfFunctionCall, + State.Stack.back().NestedBlockIndent); State.Stack.push_back(ParenState(NewIndent, NewIndentLevel, State.Stack.back().LastSpace, AvoidBinPacking, NoLineBreak)); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index bf98eceacd..183fa4418b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3048,35 +3048,38 @@ TEST_F(FormatTest, FormatsJoinedLinesOnSubsequentRuns) { } TEST_F(FormatTest, LayoutBlockInsideParens) { - EXPECT_EQ("functionCall({ int i; });", format(" functionCall ( {int i;} );")); - EXPECT_EQ("functionCall({\n" - " int i;\n" - " int j;\n" - "});", - format(" functionCall ( {int i;int j;} );")); - EXPECT_EQ("functionCall({\n" - " int i;\n" - " int j;\n" - "}, aaaa, bbbb, cccc);", - format(" functionCall ( {int i;int j;}, aaaa, bbbb, cccc);")); - EXPECT_EQ("functionCall(\n" - " {\n" - " int i;\n" - " int j;\n" - " },\n" - " aaaa, bbbb, // comment\n" - " cccc);", - format(" functionCall ( {int i;int j;}, aaaa, bbbb, // comment\n" - "cccc);")); - EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", - format(" functionCall (aaaa, bbbb, {int i;});")); - EXPECT_EQ("functionCall(aaaa, bbbb, {\n" - " int i;\n" - " int j;\n" - "});", - format(" functionCall (aaaa, bbbb, {int i;int j;});")); - EXPECT_EQ("functionCall(aaaa, bbbb, { int i; });", - format(" functionCall (aaaa, bbbb, {int i;});")); + verifyFormat("functionCall({ int i; });"); + verifyFormat("functionCall({\n" + " int i;\n" + " int j;\n" + "});"); + verifyFormat("functionCall({\n" + " int i;\n" + " int j;\n" + "}, aaaa, bbbb, cccc);"); + verifyFormat("functionA(functionB({\n" + " int i;\n" + " int j;\n" + " }),\n" + " aaaa, bbbb, cccc);"); + verifyFormat("functionCall(\n" + " {\n" + " int i;\n" + " int j;\n" + " },\n" + " aaaa, bbbb, // comment\n" + " cccc);"); + verifyFormat("functionA(functionB({\n" + " int i;\n" + " int j;\n" + " }),\n" + " aaaa, bbbb, // comment\n" + " cccc);"); + verifyFormat("functionCall(aaaa, bbbb, { int i; });"); + verifyFormat("functionCall(aaaa, bbbb, {\n" + " int i;\n" + " int j;\n" + "});"); verifyFormat( "Aaa(\n" // FIXME: There shouldn't be a linebreak here. " {\n" diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 35727eb686..76c2730960 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -408,10 +408,9 @@ TEST_F(FormatTestJS, MultipleFunctionLiterals) { " body();\n" " });"); - // FIXME: This is bad, but it used to be formatted correctly by accident. - verifyFormat("getSomeLongPromise().then(function(value) {\n" - " body();\n" - "}).thenCatch(function(error) { body(); });"); + verifyFormat("getSomeLongPromise()\n" + " .then(function(value) { body(); })\n" + " .thenCatch(function(error) { body(); });"); } TEST_F(FormatTestJS, ReturnStatements) {