]> granicus.if.org Git - clang/commitdiff
clang-format: Prefer an additional line-break over hanging indent.
authorDaniel Jasper <djasper@google.com>
Thu, 3 Apr 2014 12:00:33 +0000 (12:00 +0000)
committerDaniel Jasper <djasper@google.com>
Thu, 3 Apr 2014 12:00:33 +0000 (12:00 +0000)
Don't allow the RHS of an operator to be split over multiple
lines unless there is a line-break right after the operator.

Before:
  if (aaaa && bbbbb || // break
                  cccc) {
  }

After:
  if (aaaa &&
      bbbbb || // break
          cccc) {
  }

In most cases, this seems to increase readability.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@205527 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTest.cpp

index 38086379fe7466640e4047675082d9da72d7f87f..5a3dee6387075aef44ceaf52f0465dcb52574502 100644 (file)
@@ -609,6 +609,18 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
           std::max(std::max(State.Column, NewParenState.Indent),
                    State.Stack.back().LastSpace);
 
+    // Don't allow the RHS of an operator to be split over multiple lines unless
+    // there is a line-break right after the operator.
+    // Exclude relational operators, as there, it is always more desirable to
+    // have the LHS 'left' of the RHS.
+    // FIXME: Implement this for '<<' and BreakBeforeBinaryOperators.
+    if (!Newline && Previous && Previous->Type == TT_BinaryOperator &&
+        !Previous->isOneOf(tok::lessless, tok::question, tok::colon) &&
+        Previous->getPrecedence() > prec::Assignment &&
+        Previous->getPrecedence() != prec::Relational &&
+        !Style.BreakBeforeBinaryOperators)
+      NewParenState.NoLineBreak = true;
+
     // Do not indent relative to the fake parentheses inserted for "." or "->".
     // This is a special case to make the following to statements consistent:
     //   OuterFunction(InnerFunctionCall( // break
index 5395fd98923f0d31e4b62f17ad94ffdb57ae610b..76062605f706028becd58fff5f5905aad2deaca4 100644 (file)
@@ -2889,8 +2889,9 @@ TEST_F(FormatTest, ExpressionIndentation) {
                "            aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +\n"
                "        bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb) {\n}");
   verifyFormat("if () {\n"
-               "} else if (aaaaa && bbbbb > // break\n"
-               "                        ccccc) {\n"
+               "} else if (aaaaa &&\n"
+               "           bbbbb > // break\n"
+               "               ccccc) {\n"
                "}");
 
   // Presence of a trailing comment used to change indentation of b.