@@ -814,6 +814,11 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
814
814
// parenthesis by disallowing any further line breaks if there is no line
815
815
// break after the opening parenthesis. Don't break if it doesn't conserve
816
816
// columns.
817
+ auto IsOtherConditional = [&](const FormatToken &Tok) {
818
+ return Tok.isOneOf (tok::kw_for, tok::kw_while, tok::kw_switch) ||
819
+ (Style .isJavaScript () && Tok.is (Keywords.kw_await ) && Tok.Previous &&
820
+ Tok.Previous ->is (tok::kw_for));
821
+ };
817
822
auto IsOpeningBracket = [&](const FormatToken &Tok) {
818
823
auto IsStartOfBracedList = [&]() {
819
824
return Tok.is (tok::l_brace) && Tok.isNot (BK_Block) &&
@@ -825,26 +830,37 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
825
830
}
826
831
if (!Tok.Previous )
827
832
return true ;
828
- if (Tok.Previous ->isIf ())
829
- return Style .AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak;
830
- return !Tok.Previous ->isOneOf (TT_CastRParen, tok::kw_for, tok::kw_while,
831
- tok::kw_switch) &&
832
- !(Style .isJavaScript () && Tok.Previous ->is (Keywords.kw_await ));
833
+ if (Tok.Previous ->isIf ()) {
834
+ /* For backward compatibility, use AlignAfterOpenBracket
835
+ * in case AlignAfterControlStatement is not initialized */
836
+ return Style .AlignAfterControlStatement == FormatStyle::BACSS_MultiLine ||
837
+ (Style .AlignAfterControlStatement == FormatStyle::BACSS_Default &&
838
+ Style .AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak);
839
+ }
840
+ if (IsOtherConditional (*Tok.Previous )) {
841
+ return Style .AlignAfterControlStatement == FormatStyle::BACSS_MultiLine;
842
+ }
843
+ if (Style .AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
844
+ Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
845
+ return !Tok.Previous ->is (TT_CastRParen) &&
846
+ !(Style .isJavaScript () && Tok.is (Keywords.kw_await ));
847
+ }
848
+ return false ;
833
849
};
834
850
auto IsFunctionCallParen = [](const FormatToken &Tok) {
835
851
return Tok.is (tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous &&
836
852
Tok.Previous ->is (tok::identifier);
837
853
};
838
- auto IsInTemplateString = [this ](const FormatToken &Tok) {
854
+ auto IsInTemplateString = [this ](const FormatToken &Tok, bool NestBlocks ) {
839
855
if (!Style .isJavaScript ())
840
856
return false ;
841
857
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous ) {
842
858
if (Prev->is (TT_TemplateString) && Prev->opensScope ())
843
859
return true ;
844
- if (Prev->opensScope () ||
845
- (Prev-> is (TT_TemplateString) && Prev-> closesScope ())) {
846
- break ;
847
- }
860
+ if (Prev->opensScope () && !NestBlocks)
861
+ return false ;
862
+ if (Prev-> is (TT_TemplateString) && Prev-> closesScope ())
863
+ return false ;
848
864
}
849
865
return false ;
850
866
};
@@ -866,21 +882,24 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
866
882
Tok.isOneOf (tok::ellipsis, Keywords.kw_await ))) {
867
883
return true ;
868
884
}
869
- const auto *Previous = Tok.Previous ;
870
- if (!Previous || (!Previous->isOneOf (TT_FunctionDeclarationLParen,
871
- TT_LambdaDefinitionLParen) &&
872
- !IsFunctionCallParen (*Previous))) {
885
+ const auto *Previous = TokAfterLParen.Previous ;
886
+ assert (Previous); // IsOpeningBracket(Previous)
887
+ if (Previous->Previous && (Previous->Previous ->isIf () ||
888
+ IsOtherConditional (*Previous->Previous ))) {
889
+ return false ;
890
+ }
891
+ if (!Previous->isOneOf (TT_FunctionDeclarationLParen,
892
+ TT_LambdaDefinitionLParen) &&
893
+ !IsFunctionCallParen (*Previous)) {
873
894
return true ;
874
895
}
875
- if (IsOpeningBracket (Tok) || IsInTemplateString (Tok))
896
+ if (IsOpeningBracket (Tok) || IsInTemplateString (Tok, true ))
876
897
return true ;
877
898
const auto *Next = Tok.Next ;
878
899
return !Next || Next->isMemberAccess () ||
879
900
Next->is (TT_FunctionDeclarationLParen) || IsFunctionCallParen (*Next);
880
901
};
881
- if ((Style .AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
882
- Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
883
- IsOpeningBracket (Previous) && State.Column > getNewLineColumn (State) &&
902
+ if (IsOpeningBracket (Previous) && State.Column > getNewLineColumn (State) &&
884
903
// Don't do this for simple (no expressions) one-argument function calls
885
904
// as that feels like needlessly wasting whitespace, e.g.:
886
905
//
@@ -910,7 +929,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
910
929
!(Current.MacroParent && Previous.MacroParent ) &&
911
930
(Current.isNot (TT_LineComment) ||
912
931
Previous.isOneOf (BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
913
- !IsInTemplateString (Current)) {
932
+ !IsInTemplateString (Current, false )) {
914
933
CurrentState.Indent = State.Column + Spaces;
915
934
CurrentState.IsAligned = true ;
916
935
}
@@ -1247,8 +1266,17 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
1247
1266
}
1248
1267
1249
1268
if (PreviousNonComment && PreviousNonComment->is (tok::l_paren)) {
1250
- CurrentState.BreakBeforeClosingParen =
1251
- Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1269
+ auto Previous = PreviousNonComment->Previous ;
1270
+ if (Previous &&
1271
+ (Previous->isIf () ||
1272
+ Previous->isOneOf (tok::kw_for, tok::kw_while, tok::kw_switch))) {
1273
+ CurrentState.BreakBeforeClosingParen =
1274
+ Style .AlignAfterControlStatement == FormatStyle::BACSS_MultiLine &&
1275
+ Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1276
+ } else {
1277
+ CurrentState.BreakBeforeClosingParen =
1278
+ Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1279
+ }
1252
1280
}
1253
1281
1254
1282
if (PreviousNonComment && PreviousNonComment->is (TT_TemplateOpener))
0 commit comments