From bdc07b42302eb57f4abf892323c13c26798696b1 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Thu, 27 Mar 2014 16:14:13 +0000 Subject: [PATCH] clang-format: Avoid line-breaks that increase the current column. While these might make sense for some rule (e.g. break after multi-line operand), they generally appear ugly and confusing. Before: fffffffffff(R\"x( multiline raw string literal xxxxxxxxxxxxxx )x\" + bbbbbb) After: fffffffffff(R\"x( multiline raw string literal xxxxxxxxxxxxxx )x\" + bbbbbb) git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204937 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 14 +++++++++----- unittests/Format/FormatTest.cpp | 5 ++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 55d700fd80..a8ab72c4ca 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -144,7 +144,13 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { Previous.Type == TT_ArrayInitializerLSquare) && getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State)) return true; + if (Current.Type == TT_CtorInitializerColon && + (!Style.AllowShortFunctionsOnASingleLine || + Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0)) + return true; + if (State.Column < getNewLineColumn(State)) + return false; if (!Style.BreakBeforeBinaryOperators) { // If we need to break somewhere inside the LHS of a binary expression, we // should also break after the operator. Otherwise, the formatting would @@ -204,10 +210,6 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { if (Previous.BlockKind == BK_Block && Previous.is(tok::l_brace) && !Current.isOneOf(tok::r_brace, tok::comment)) return true; - if (Current.Type == TT_CtorInitializerColon && - (!Style.AllowShortFunctionsOnASingleLine || - Style.BreakConstructorInitializersBeforeComma || Style.ColumnLimit != 0)) - return true; return false; } @@ -446,6 +448,8 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, } unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { + if (!State.NextToken || !State.NextToken->Previous) + return 0; FormatToken &Current = *State.NextToken; const FormatToken &Previous = *State.NextToken->Previous; // If we are continuing an expression, we want to use the continuation indent. @@ -528,7 +532,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { return State.FirstIndent + Style.ConstructorInitializerIndentWidth; if (NextNonComment->Type == TT_CtorInitializerComma) return State.Stack.back().Indent; - if (State.Stack.back().Indent == State.FirstIndent && + if (State.Stack.back().Indent == State.FirstIndent && PreviousNonComment && PreviousNonComment->isNot(tok::r_brace)) // Ensure that we fall back to the continuation indent width instead of // just flushing continuations left. diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 949167c8f5..b5a70760a4 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -6552,11 +6552,10 @@ TEST_F(FormatTest, CountsCharactersInMultilineRawStringLiterals) { getGoogleStyleWithColumns(20))); EXPECT_EQ("fffffffffff(R\"x(\n" "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\" +\n" - " bbbbbb);", + ")x\" + bbbbbb);", format("fffffffffff(R\"x(\n" "multiline raw string literal xxxxxxxxxxxxxx\n" - ")x\" + bbbbbb);", + ")x\" + bbbbbb);", getGoogleStyleWithColumns(20))); EXPECT_EQ("fffffffffff(\n" " R\"x(\n" -- 2.40.0