r210537 - clang-format: [JS] Treat dict literals similar to objc method exprs.
Daniel Jasper
djasper at google.com
Tue Jun 10 07:44:02 PDT 2014
Author: djasper
Date: Tue Jun 10 09:44:02 2014
New Revision: 210537
URL: http://llvm.org/viewvc/llvm-project?rev=210537&view=rev
Log:
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(); //
}
};
Modified:
cfe/trunk/lib/Format/ContinuationIndenter.cpp
cfe/trunk/lib/Format/FormatToken.h
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/unittests/Format/FormatTestJS.cpp
Modified: cfe/trunk/lib/Format/ContinuationIndenter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/ContinuationIndenter.cpp?rev=210537&r1=210536&r2=210537&view=diff
==============================================================================
--- cfe/trunk/lib/Format/ContinuationIndenter.cpp (original)
+++ cfe/trunk/lib/Format/ContinuationIndenter.cpp Tue Jun 10 09:44:02 2014
@@ -195,7 +195,7 @@ bool ContinuationIndenter::mustBreak(con
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::addTokenOnCur
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::addTokenO
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::getNewLin
(!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::moveState
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:
Modified: cfe/trunk/lib/Format/FormatToken.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/FormatToken.h?rev=210537&r1=210536&r2=210537&view=diff
==============================================================================
--- cfe/trunk/lib/Format/FormatToken.h (original)
+++ cfe/trunk/lib/Format/FormatToken.h Tue Jun 10 09:44:02 2014
@@ -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,
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=210537&r1=210536&r2=210537&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Tue Jun 10 09:44:02 2014
@@ -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(co
// 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(cons
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(cons
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=";
Modified: cfe/trunk/unittests/Format/FormatTestJS.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTestJS.cpp?rev=210537&r1=210536&r2=210537&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTestJS.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTestJS.cpp Tue Jun 10 09:44:02 2014
@@ -88,6 +88,31 @@ TEST_F(FormatTestJS, ES6DestructuringAss
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};");
More information about the cfe-commits
mailing list