From b1491798353b26c07b065c6975a91ff6214b8ff6 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 9 Jul 2013 11:57:27 +0000 Subject: [PATCH] Avoid confusing indentations for chained function calls. Basically treat a function with a trailing call similar to a function with multiple parameters. Before: aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)) .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(); After: aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa( aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa)) .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(); Also fix typo. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@185930 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/Format.cpp | 22 ++++++++++++++++++---- unittests/Format/FormatTest.cpp | 11 ++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 34ed8ea07c..fb931490dc 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -672,10 +672,24 @@ private: State.Stack.back().LastSpace = State.Column; else if (Previous.Type == TT_InheritanceColon) State.Stack.back().Indent = State.Column; - else if (Previous.opensScope() && !Current.FakeLParens.empty()) - // If this function has multiple parameters or a binary expression - // parameter, indent nested calls from the start of the first parameter. - State.Stack.back().LastSpace = State.Column; + else if (Previous.opensScope()) { + // If a function has multiple parameters (including a single parameter + // that is a binary expression) or a trailing call, indented all + // parameters from the opening parenthesis. This avoids confusing + // indents like: + // OuterFunction(InnerFunctionCall( + // ParameterToInnerFunction), + // SecondParameterToOuterFunction); + bool HasMultipleParameters = !Current.FakeLParens.empty(); + bool HasTrailingCall = false; + if (Previous.MatchingParen) { + const FormatToken *Next = Previous.MatchingParen->getNextNonComment(); + if (Next && Next->isOneOf(tok::period, tok::arrow)) + HasTrailingCall = true; + } + if (HasMultipleParameters || HasTrailingCall) + State.Stack.back().LastSpace = State.Column; + } } return moveStateToNextToken(State, DryRun); diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 417c50cd2c..dda16c5249 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3051,6 +3051,15 @@ TEST_F(FormatTest, WrapsAtFunctionCallsIfNecessary) { " aaaaaaaaaaaaaaaaaaa,\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);", NoBinPacking); + + // If there is a subsequent call, change to hanging indentation. + verifyFormat( + "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa))\n" + " .aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa();"); + verifyFormat( + "aaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" + " aaaaaaaaaaaaaaaaaa, aaaaaaaaaaaaaaaaa));"); } TEST_F(FormatTest, WrapsTemplateDeclarations) { @@ -3787,7 +3796,7 @@ TEST_F(FormatTest, LayoutCxx11ConstructorBraceInitializers) { verifyFormat("DoSomethingWithVector({ {} /* No data */ }, { { 1, 2 } });"); verifyFormat( "someFunction(OtherParam, BracedList{\n" - " // comment 1 (Forcing intersting break)\n" + " // comment 1 (Forcing interesting break)\n" " param1, param2,\n" " // comment 2\n" " param3, param4\n" -- 2.40.0