From: Daniel Jasper Date: Mon, 7 Dec 2015 19:50:48 +0000 (+0000) Subject: clang-format: Make wrapping after "./->" cheaper, even if the element X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1e4fb607e70c0e4966f314460d022452adabbc34;p=clang clang-format: Make wrapping after "./->" cheaper, even if the element before it is not a closing parenthesis. Otherwise, this frequently leads to "hanging" indents that users perceive as "weird". Before: return !soooooooooooooome_map.insert( aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) .second; After: return !soooooooooooooome_map .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa) .second; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@254933 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index b5e7b9a0e6..b8487e1035 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -1743,10 +1743,20 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, return 2; if (Right.isMemberAccess()) { - if (Left.is(tok::r_paren) && Left.MatchingParen && - Left.MatchingParen->ParameterCount > 0) - return 20; // Should be smaller than breaking at a nested comma. - return 150; + // Breaking before the "./->" of a chained call/member access is reasonably + // cheap, as formatting those with one call per line is generally + // desirable. In particular, it should be cheaper to break before the call + // than it is to break inside a call's parameters, which could lead to weird + // "hanging" indents. The exception is the very last "./->" to support this + // frequent pattern: + // + // aaaaaaaa.aaaaaaaa.bbbbbbb().ccccccccccccccccccccc( + // dddddddd); + // + // which might otherwise be blown up onto many lines. Here, clang-format + // won't produce "hanging" indents anyway as there is no other trailing + // call. + return Right.LastOperator ? 150 : 40; } if (Right.is(TT_TrailingAnnotation) && diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 94d456c059..09deed9fec 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3341,7 +3341,7 @@ TEST_F(FormatTest, LineBreakingInBinaryExpressions) { verifyFormat("aaaaaa = aaaaaaa(aaaaaaa, // break\n" " aaaaaa) >>\n" " bbbbbb;"); - verifyFormat("Whitespaces.addUntouchableComment(\n" + verifyFormat("aa = Whitespaces.addUntouchableComment(\n" " SourceMgr.getSpellingColumnNumber(\n" " TheLine.Last->FormatTok.Tok.getLocation()) -\n" " 1);"); @@ -4157,9 +4157,13 @@ TEST_F(FormatTest, FormatsBuilderPattern) { verifyFormat("return aaaaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa() <\n" " aaaaaaaaaaaaaaa->aaaaa().aaaaaaaaaaaaa().aaaaaa();"); verifyFormat( - "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaa(\n" + "aaaaaaa->aaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); + verifyFormat( + "aaaaaaa->aaaaaaa\n" + " ->aaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " ->aaaaaaaa(aaaaaaaaaaaaaaa);"); verifyFormat( "aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n" " aaaaaaaaaaaaaa);"); @@ -4224,6 +4228,12 @@ TEST_F(FormatTest, FormatsBuilderPattern) { // Prefer not to break after empty parentheses. verifyFormat("FirstToken->WhitespaceRange.getBegin().getLocWithOffset(\n" " First->LastNewlineOffset);"); + + // Prefer not to create "hanging" indents. + verifyFormat( + "return !soooooooooooooome_map\n" + " .insert(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa)\n" + " .second;"); } TEST_F(FormatTest, BreaksAccordingToOperatorPrecedence) { @@ -7373,8 +7383,8 @@ TEST_F(FormatTest, FormatObjCMethodExpr) { " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa];"); verifyFormat("[aaaaaaaaaaaaaaaaaaaaaaa.aaaaaaaa[aaaaaaaaaaaaaaaaaaaaa]\n" " aaaaaaaaaaaaaaaaaaaaaa];"); - verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" - " .aaaaaaaa];", // FIXME: Indentation seems off. + verifyFormat("[call aaaaaaaa.aaaaaa.aaaaaaaa.aaaaaaaa.aaaaaaaa\n" + " .aaaaaaaa.aaaaaaaa];", // FIXME: Indentation seems off. getLLVMStyleWithColumns(60)); verifyFormat(