Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -868,7 +868,16 @@ void next() { if (CurrentToken) { - CurrentToken->NestingLevel = Contexts.size() - 1; + CurrentToken->NestingLevel = Line.Level + Contexts.size() - 1; + + // Put the closing brace / paren at the same NestingLevel as the open + // brace / paren. We could do this as well for square and angles, but + // it doesn't look it goes nicely with the way the formatting is usually + // done + if (CurrentToken->Tok.isOneOf(tok::r_brace, tok::r_paren) && + Contexts.size() > 1) + CurrentToken->NestingLevel--; + CurrentToken->BindingStrength = Contexts.back().BindingStrength; modifyContext(*CurrentToken); determineTokenType(*CurrentToken); @@ -982,8 +991,8 @@ // trailing token of this declaration and thus cannot be a name. if (Current.is(Keywords.kw_instanceof)) { Current.Type = TT_BinaryOperator; - } else if (isStartOfName(Current) && - (!Line.MightBeFunctionDecl || Current.NestingLevel != 0)) { + } else if (isStartOfName(Current) && (!Line.MightBeFunctionDecl || + Current.NestingLevel != Line.Level)) { Contexts.back().FirstStartOfName = &Current; Current.Type = TT_StartOfName; } else if (Current.isOneOf(tok::kw_auto, tok::kw___auto_type)) { @@ -992,7 +1001,7 @@ Style.Language == FormatStyle::LK_Java) { Current.Type = TT_LambdaArrow; } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration && - Current.NestingLevel == 0) { + Current.NestingLevel == Line.Level) { Current.Type = TT_TrailingReturnArrow; } else if (Current.isOneOf(tok::star, tok::amp, tok::ampamp)) { Current.Type = @@ -1615,7 +1624,7 @@ return false; Next = skipOperatorName(Next); } else { - if (!Current.is(TT_StartOfName) || Current.NestingLevel != 0) + if (!Current.is(TT_StartOfName) || Current.NestingLevel != Line.Level) return false; for (; Next; Next = Next->Next) { if (Next->is(TT_TemplateOpener)) { @@ -1809,7 +1818,7 @@ return 1; if (Right.is(Keywords.kw_implements)) return 2; - if (Left.is(tok::comma) && Left.NestingLevel == 0) + if (Left.is(tok::comma) && Left.NestingLevel == Line.Level) return 3; } else if (Style.Language == FormatStyle::LK_JavaScript) { if (Right.is(Keywords.kw_function) && Left.isNot(tok::comma)) @@ -1843,7 +1852,7 @@ return 3; if (Left.is(TT_StartOfName)) return 110; - if (InFunctionDecl && Right.NestingLevel == 0) + if (InFunctionDecl && Right.NestingLevel == Line.Level) return Style.PenaltyReturnTypeOnItsOwnLine; return 200; } @@ -2298,7 +2307,7 @@ // Support AllowShortFunctionsOnASingleLine for JavaScript. return Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_None || Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty || - (Left.NestingLevel == 0 && Line.Level == 0 && + (Left.NestingLevel == Line.Level && Line.Level == 0 && Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline); } else if (Style.Language == FormatStyle::LK_Java) { @@ -2339,7 +2348,7 @@ return true; if (Right.Previous->ClosesTemplateDeclaration && Right.Previous->MatchingParen && - Right.Previous->MatchingParen->NestingLevel == 0 && + Right.Previous->MatchingParen->NestingLevel == Line.Level && Style.AlwaysBreakTemplateDeclarations) return true; if ((Right.isOneOf(TT_CtorInitializerComma, TT_CtorInitializerColon)) && @@ -2351,7 +2360,8 @@ // deliberate choice and might have aligned the contents of the string // literal accordingly. Thus, we try keep existing line breaks. return Right.NewlinesBefore > 0; - if (Right.Previous->is(tok::l_brace) && Right.NestingLevel == 1 && + if (Right.Previous->is(tok::l_brace) && + Right.NestingLevel == Line.Level + 1 && Style.Language == FormatStyle::LK_Proto) // Don't put enums onto single lines in protocol buffers. return true; @@ -2398,7 +2408,7 @@ return false; if (Left.is(TT_JsTypeColon)) return true; - if (Right.NestingLevel == 0 && Right.is(Keywords.kw_is)) + if (Right.NestingLevel == Line.Level && Right.is(Keywords.kw_is)) return false; if (Left.is(Keywords.kw_in)) return Style.BreakBeforeBinaryOperators == FormatStyle::BOS_None; @@ -2461,7 +2471,7 @@ Left.is(tok::kw_operator)) return false; if (Left.is(tok::equal) && !Right.isOneOf(tok::kw_default, tok::kw_delete) && - Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == 0) + Line.Type == LT_VirtualFunctionDecl && Left.NestingLevel == Line.Level) return false; if (Left.is(tok::l_paren) && Left.is(TT_AttributeParen)) return false;