@@ -693,7 +693,9 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
693693 // does not help.
694694 bool HasTwoOperands =
695695 P->OperatorIndex == 0 && !P->NextOperator && !P->is (TT_ConditionalExpr);
696- if ((!BreakBeforeOperator && !(HasTwoOperands && Style.AlignOperands )) ||
696+ if ((!BreakBeforeOperator &&
697+ !(HasTwoOperands &&
698+ Style.AlignOperands != FormatStyle::OAS_DontAlign)) ||
697699 (!State.Stack .back ().LastOperatorWrapped && BreakBeforeOperator))
698700 State.Stack .back ().NoLineBreakInOperand = true ;
699701 }
@@ -1039,6 +1041,8 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
10391041 // * not remove the 'lead' ContinuationIndentWidth
10401042 // * always un-indent by the operator when BreakBeforeTernaryOperators=true
10411043 unsigned Indent = State.Stack .back ().Indent - Style.ContinuationIndentWidth ;
1044+ if (Style.BreakBeforeTernaryOperators && State.Stack .back ().UnindentOperator )
1045+ Indent -= 2 ;
10421046 return Indent;
10431047 }
10441048 return State.Stack .back ().QuestionColumn ;
@@ -1118,6 +1122,13 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) {
11181122 return ContinuationIndent;
11191123 if (Current.is (TT_ProtoExtensionLSquare))
11201124 return State.Stack .back ().Indent ;
1125+ if (Current.isBinaryOperator () && State.Stack .back ().UnindentOperator )
1126+ return State.Stack .back ().Indent - Current.Tok .getLength () -
1127+ Current.SpacesRequiredBefore ;
1128+ if (Current.isOneOf (tok::comment, TT_BlockComment, TT_LineComment) &&
1129+ NextNonComment->isBinaryOperator () && State.Stack .back ().UnindentOperator )
1130+ return State.Stack .back ().Indent - NextNonComment->Tok .getLength () -
1131+ NextNonComment->SpacesRequiredBefore ;
11211132 if (State.Stack .back ().Indent == State.FirstIndent && PreviousNonComment &&
11221133 !PreviousNonComment->isOneOf (tok::r_brace, TT_CtorInitializerComma))
11231134 // Ensure that we fall back to the continuation indent width instead of
@@ -1297,7 +1308,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
12971308 (Previous && (Previous->opensScope () ||
12981309 Previous->isOneOf (tok::semi, tok::kw_return) ||
12991310 (Previous->getPrecedence () == prec::Assignment &&
1300- Style.AlignOperands ) ||
1311+ Style.AlignOperands != FormatStyle::OAS_DontAlign ) ||
13011312 Previous->is (TT_ObjCMethodExpr)));
13021313 for (SmallVectorImpl<prec::Level>::const_reverse_iterator
13031314 I = Current.FakeLParens .rbegin (),
@@ -1309,6 +1320,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
13091320 NewParenState.LastOperatorWrapped = true ;
13101321 NewParenState.IsChainedConditional = false ;
13111322 NewParenState.IsWrappedConditional = false ;
1323+ NewParenState.UnindentOperator = false ;
13121324 NewParenState.NoLineBreak =
13131325 NewParenState.NoLineBreak || State.Stack .back ().NoLineBreakInOperand ;
13141326
@@ -1320,14 +1332,26 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
13201332 // a builder type call after 'return' or, if the alignment after opening
13211333 // brackets is disabled.
13221334 if (!Current.isTrailingComment () &&
1323- (Style.AlignOperands || *I < prec::Assignment) &&
1335+ (Style.AlignOperands != FormatStyle::OAS_DontAlign ||
1336+ *I < prec::Assignment) &&
13241337 (!Previous || Previous->isNot (tok::kw_return) ||
13251338 (Style.Language != FormatStyle::LK_Java && *I > 0 )) &&
13261339 (Style.AlignAfterOpenBracket != FormatStyle::BAS_DontAlign ||
1327- *I != prec::Comma || Current.NestingLevel == 0 ))
1340+ *I != prec::Comma || Current.NestingLevel == 0 )) {
13281341 NewParenState.Indent =
13291342 std::max (std::max (State.Column , NewParenState.Indent ),
13301343 State.Stack .back ().LastSpace );
1344+ }
1345+
1346+ // If BreakBeforeBinaryOperators is set, un-indent a bit to account for
1347+ // the operator and keep the operands aligned
1348+ if (Style.AlignOperands == FormatStyle::OAS_AlignAfterOperator && Previous &&
1349+ (Previous->getPrecedence () == prec::Assignment ||
1350+ Previous->is (tok::kw_return) ||
1351+ (*I == prec::Conditional && Previous->is (tok::question) &&
1352+ Previous->is (TT_ConditionalExpr))) &&
1353+ !Newline)
1354+ NewParenState.UnindentOperator = true ;
13311355
13321356 // Do not indent relative to the fake parentheses inserted for "." or "->".
13331357 // This is a special case to make the following to statements consistent:
@@ -1350,6 +1374,7 @@ void ContinuationIndenter::moveStatePastFakeLParens(LineState &State,
13501374 Previous->is (TT_ConditionalExpr) && I == Current.FakeLParens .rbegin () &&
13511375 !State.Stack .back ().IsWrappedConditional ) {
13521376 NewParenState.IsChainedConditional = true ;
1377+ NewParenState.UnindentOperator = State.Stack .back ().UnindentOperator ;
13531378 } else if (*I == prec::Conditional ||
13541379 (!SkipFirstExtraIndent && *I > prec::Assignment &&
13551380 !Current.isTrailingComment ())) {
0 commit comments