@@ -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,36 @@ 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
+ if (Style .AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
843
+ Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) {
844
+ return !Tok.Previous ->is (TT_CastRParen) &&
845
+ !(Style .isJavaScript () && Tok.is (Keywords.kw_await ));
846
+ }
847
+ return false ;
833
848
};
834
849
auto IsFunctionCallParen = [](const FormatToken &Tok) {
835
850
return Tok.is (tok::l_paren) && Tok.ParameterCount > 0 && Tok.Previous &&
836
851
Tok.Previous ->is (tok::identifier);
837
852
};
838
- auto IsInTemplateString = [this ](const FormatToken &Tok) {
853
+ auto IsInTemplateString = [this ](const FormatToken &Tok, bool NestBlocks ) {
839
854
if (!Style .isJavaScript ())
840
855
return false ;
841
856
for (const auto *Prev = &Tok; Prev; Prev = Prev->Previous ) {
842
857
if (Prev->is (TT_TemplateString) && Prev->opensScope ())
843
858
return true ;
844
- if (Prev->opensScope () ||
845
- (Prev-> is (TT_TemplateString) && Prev-> closesScope ())) {
846
- break ;
847
- }
859
+ if (Prev->opensScope () && !NestBlocks)
860
+ return false ;
861
+ if (Prev-> is (TT_TemplateString) && Prev-> closesScope ())
862
+ return false ;
848
863
}
849
864
return false ;
850
865
};
@@ -866,21 +881,24 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
866
881
Tok.isOneOf (tok::ellipsis, Keywords.kw_await ))) {
867
882
return true ;
868
883
}
869
- const auto *Previous = Tok.Previous ;
870
- if (!Previous || (!Previous->isOneOf (TT_FunctionDeclarationLParen,
871
- TT_LambdaDefinitionLParen) &&
872
- !IsFunctionCallParen (*Previous))) {
884
+ const auto *Previous = TokAfterLParen.Previous ;
885
+ assert (Previous); // IsOpeningBracket(Previous)
886
+ if (Previous->Previous && (Previous->Previous ->isIf () ||
887
+ IsOtherConditional (*Previous->Previous ))) {
888
+ return false ;
889
+ }
890
+ if (!Previous->isOneOf (TT_FunctionDeclarationLParen,
891
+ TT_LambdaDefinitionLParen) &&
892
+ !IsFunctionCallParen (*Previous)) {
873
893
return true ;
874
894
}
875
- if (IsOpeningBracket (Tok) || IsInTemplateString (Tok))
895
+ if (IsOpeningBracket (Tok) || IsInTemplateString (Tok, true ))
876
896
return true ;
877
897
const auto *Next = Tok.Next ;
878
898
return !Next || Next->isMemberAccess () ||
879
899
Next->is (TT_FunctionDeclarationLParen) || IsFunctionCallParen (*Next);
880
900
};
881
- if ((Style .AlignAfterOpenBracket == FormatStyle::BAS_AlwaysBreak ||
882
- Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent) &&
883
- IsOpeningBracket (Previous) && State.Column > getNewLineColumn (State) &&
901
+ if (IsOpeningBracket (Previous) && State.Column > getNewLineColumn (State) &&
884
902
// Don't do this for simple (no expressions) one-argument function calls
885
903
// as that feels like needlessly wasting whitespace, e.g.:
886
904
//
@@ -910,7 +928,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
910
928
!(Current.MacroParent && Previous.MacroParent ) &&
911
929
(Current.isNot (TT_LineComment) ||
912
930
Previous.isOneOf (BK_BracedInit, TT_VerilogMultiLineListLParen)) &&
913
- !IsInTemplateString (Current)) {
931
+ !IsInTemplateString (Current, false )) {
914
932
CurrentState.Indent = State.Column + Spaces;
915
933
CurrentState.IsAligned = true ;
916
934
}
@@ -1247,8 +1265,17 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
1247
1265
}
1248
1266
1249
1267
if (PreviousNonComment && PreviousNonComment->is (tok::l_paren)) {
1250
- CurrentState.BreakBeforeClosingParen =
1251
- Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1268
+ auto Previous = PreviousNonComment->Previous ;
1269
+ if (Previous &&
1270
+ (Previous->isIf () ||
1271
+ Previous->isOneOf (tok::kw_for, tok::kw_while, tok::kw_switch))) {
1272
+ CurrentState.BreakBeforeClosingParen =
1273
+ Style .AlignAfterControlStatement == FormatStyle::BACSS_MultiLine &&
1274
+ Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1275
+ } else {
1276
+ CurrentState.BreakBeforeClosingParen =
1277
+ Style .AlignAfterOpenBracket == FormatStyle::BAS_BlockIndent;
1278
+ }
1252
1279
}
1253
1280
1254
1281
if (PreviousNonComment && PreviousNonComment->is (TT_TemplateOpener))
0 commit comments