From bdb17e3470d273e91caba27000fe3d4e18269b76 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 10 Jun 2014 14:44:02 +0000 Subject: [PATCH] clang-format: [JS] Treat dict literals similar to objc method exprs. Before: return { link: function() { f(); // } }; return { a: a, link: function() { f(); // } } After: return { link: function() { f(); // } }; return { a: a, link: function() { f(); // } }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@210537 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/ContinuationIndenter.cpp | 10 +++++----- lib/Format/FormatToken.h | 2 +- lib/Format/TokenAnnotator.cpp | 19 ++++++++++++------- unittests/Format/FormatTestJS.cpp | 25 +++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 13 deletions(-) diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 6304ff71fc..39baef6930 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -195,7 +195,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { State.Stack.back().FirstLessLess == 0) return true; - if (Current.Type == TT_ObjCSelectorName && + if (Current.Type == TT_SelectorName && State.Stack.back().ObjCSelectorNameFound && State.Stack.back().BreakBeforeParameter) return true; @@ -278,7 +278,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun, Whitespaces.replaceWhitespace(Current, /*Newlines=*/0, /*IndentLevel=*/0, Spaces, State.Column + Spaces); - if (Current.Type == TT_ObjCSelectorName && + if (Current.Type == TT_SelectorName && !State.Stack.back().ObjCSelectorNameFound) { if (Current.LongestObjCSelectorName == 0) State.Stack.back().AlignColons = false; @@ -373,7 +373,7 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, if (NextNonComment->isMemberAccess()) { if (State.Stack.back().CallContinuation == 0) State.Stack.back().CallContinuation = State.Column; - } else if (NextNonComment->Type == TT_ObjCSelectorName) { + } else if (NextNonComment->Type == TT_SelectorName) { if (!State.Stack.back().ObjCSelectorNameFound) { if (NextNonComment->LongestObjCSelectorName == 0) { State.Stack.back().AlignColons = false; @@ -524,7 +524,7 @@ unsigned ContinuationIndenter::getNewLineColumn(const LineState &State) { (!Style.IndentFunctionDeclarationAfterType || State.Line->StartsDefinition))) return std::max(State.Stack.back().LastSpace, State.Stack.back().Indent); - if (NextNonComment->Type == TT_ObjCSelectorName) { + if (NextNonComment->Type == TT_SelectorName) { if (!State.Stack.back().ObjCSelectorNameFound) { if (NextNonComment->LongestObjCSelectorName == 0) { return State.Stack.back().Indent; @@ -597,7 +597,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State, if (Current.isMemberAccess()) State.Stack.back().StartOfFunctionCall = Current.LastOperator ? 0 : State.Column + Current.ColumnWidth; - if (Current.Type == TT_ObjCSelectorName) + if (Current.Type == TT_SelectorName) State.Stack.back().ObjCSelectorNameFound = true; if (Current.Type == TT_CtorInitializerColon) { // Indent 2 from the column, so: diff --git a/lib/Format/FormatToken.h b/lib/Format/FormatToken.h index 90208b6065..26fa061767 100644 --- a/lib/Format/FormatToken.h +++ b/lib/Format/FormatToken.h @@ -54,13 +54,13 @@ enum TokenType { TT_ObjCMethodExpr, TT_ObjCMethodSpecifier, TT_ObjCProperty, - TT_ObjCSelectorName, TT_OverloadedOperator, TT_OverloadedOperatorLParen, TT_PointerOrReference, TT_PureVirtualSpecifier, TT_RangeBasedForLoopColon, TT_RegexLiteral, + TT_SelectorName, TT_StartOfName, TT_TemplateCloser, TT_TemplateOpener, diff --git a/lib/Format/TokenAnnotator.cpp b/lib/Format/TokenAnnotator.cpp index 516aaebe6a..66060f0b83 100644 --- a/lib/Format/TokenAnnotator.cpp +++ b/lib/Format/TokenAnnotator.cpp @@ -314,8 +314,11 @@ private: return false; updateParameterCount(Left, CurrentToken); if (CurrentToken->is(tok::colon) && - Style.Language != FormatStyle::LK_Proto) + Style.Language != FormatStyle::LK_Proto) { + if (CurrentToken->getPreviousNonComment()->is(tok::identifier)) + CurrentToken->getPreviousNonComment()->Type = TT_SelectorName; Left->Type = TT_DictLiteral; + } if (!consumeToken()) return false; } @@ -389,7 +392,7 @@ private: } else if (Contexts.back().ColonIsObjCMethodExpr || Line.First->Type == TT_ObjCMethodSpecifier) { Tok->Type = TT_ObjCMethodExpr; - Tok->Previous->Type = TT_ObjCSelectorName; + Tok->Previous->Type = TT_SelectorName; if (Tok->Previous->ColumnWidth > Contexts.back().LongestObjCSelectorName) { Contexts.back().LongestObjCSelectorName = Tok->Previous->ColumnWidth; @@ -1006,7 +1009,8 @@ public: // expression. while (Current && (Current->is(tok::kw_return) || - (Current->is(tok::colon) && Current->Type == TT_ObjCMethodExpr))) + (Current->is(tok::colon) && (Current->Type == TT_ObjCMethodExpr || + Current->Type == TT_DictLiteral)))) next(); if (!Current || Precedence > PrecedenceArrowAndPeriod) @@ -1035,7 +1039,7 @@ public: int CurrentPrecedence = getCurrentPrecedence(); - if (Current && Current->Type == TT_ObjCSelectorName && + if (Current && Current->Type == TT_SelectorName && Precedence == CurrentPrecedence) { if (LatestOperator) addFakeParenthesis(Start, prec::Level(Precedence)); @@ -1086,7 +1090,7 @@ private: if (Current->Type == TT_ConditionalExpr) return prec::Conditional; else if (Current->is(tok::semi) || Current->Type == TT_InlineASMColon || - Current->Type == TT_ObjCSelectorName) + Current->Type == TT_SelectorName) return 0; else if (Current->Type == TT_RangeBasedForLoopColon) return prec::Comma; @@ -1358,7 +1362,7 @@ unsigned TokenAnnotator::splitPenalty(const AnnotatedLine &Line, // In Objective-C method expressions, prefer breaking before "param:" over // breaking after it. - if (Right.Type == TT_ObjCSelectorName) + if (Right.Type == TT_SelectorName) return 0; if (Left.is(tok::colon) && Left.Type == TT_ObjCMethodExpr) return Line.MightBeFunctionDecl ? 50 : 500; @@ -1684,7 +1688,7 @@ bool TokenAnnotator::canBreakBefore(const AnnotatedLine &Line, if (Left.is(tok::colon) && (Left.Type == TT_DictLiteral || Left.Type == TT_ObjCMethodExpr)) return true; - if (Right.Type == TT_ObjCSelectorName) + if (Right.Type == TT_SelectorName) return true; if (Left.is(tok::r_paren) && Line.Type == LT_ObjCProperty) return true; @@ -1767,6 +1771,7 @@ void TokenAnnotator::printDebugInfo(const AnnotatedLine &Line) { llvm::errs() << " M=" << Tok->MustBreakBefore << " C=" << Tok->CanBreakBefore << " T=" << Tok->Type << " S=" << Tok->SpacesRequiredBefore + << " B=" << Tok->BlockParameterCount << " P=" << Tok->SplitPenalty << " Name=" << Tok->Tok.getName() << " L=" << Tok->TotalLength << " PPK=" << Tok->PackingKind << " FakeLParens="; diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 50fef8d864..485ccd624e 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -88,6 +88,31 @@ TEST_F(FormatTestJS, ES6DestructuringAssignment) { verifyFormat("var {a, b} = {a: 1, b: 2};"); } +TEST_F(FormatTestJS, ContainerLiterals) { + verifyFormat("return {\n" + " link: function() {\n" + " f(); //\n" + " }\n" + "};"); + verifyFormat("return {\n" + " a: a,\n" + " link: function() {\n" + " f(); //\n" + " }\n" + "};"); + verifyFormat("return {\n" + " a: a,\n" + " link:\n" + " function() {\n" + " f(); //\n" + " },\n" + " link:\n" + " function() {\n" + " f(); //\n" + " }\n" + "};"); +} + TEST_F(FormatTestJS, SpacesInContainerLiterals) { verifyFormat("var arr = [1, 2, 3];"); verifyFormat("var obj = {a: 1, b: 2, c: 3};"); -- 2.40.0