clang-format: [JS] Conservatively introduce column layout for JS array

initializers. For now, only use it for 20 items or more. Otherwise,
clang-format formats these one-per-line and thus increases the vertical
code size a lot.

llvm-svn: 256246
This commit is contained in:
Daniel Jasper 2015-12-22 15:48:15 +00:00
parent 72a1b6a5f2
commit e2deb59fa3
2 changed files with 23 additions and 4 deletions

View File

@ -80,8 +80,8 @@ unsigned CommaSeparatedList::formatAfterToken(LineState &State,
// Ensure that we start on the opening brace. // Ensure that we start on the opening brace.
const FormatToken *LBrace = const FormatToken *LBrace =
State.NextToken->Previous->getPreviousNonComment(); State.NextToken->Previous->getPreviousNonComment();
if (!LBrace || LBrace->isNot(tok::l_brace) || LBrace->BlockKind == BK_Block || if (!LBrace || !LBrace->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) ||
LBrace->Type == TT_DictLiteral || LBrace->BlockKind == BK_Block || LBrace->Type == TT_DictLiteral ||
LBrace->Next->Type == TT_DesignatedInitializerPeriod) LBrace->Next->Type == TT_DesignatedInitializerPeriod)
return 0; return 0;
@ -144,7 +144,8 @@ static unsigned CodePointsBetween(const FormatToken *Begin,
void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) { void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
// FIXME: At some point we might want to do this for other lists, too. // FIXME: At some point we might want to do this for other lists, too.
if (!Token->MatchingParen || Token->isNot(tok::l_brace)) if (!Token->MatchingParen ||
!Token->isOneOf(tok::l_brace, TT_ArrayInitializerLSquare))
return; return;
// In C++11 braced list style, we should not format in columns unless they // In C++11 braced list style, we should not format in columns unless they
@ -154,6 +155,12 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
Commas.size() < 19) Commas.size() < 19)
return; return;
// Limit column layout for JavaScript array initializers to 20 or more items
// for now to introduce it carefully. We can become more aggressive if this
// necessary.
if (Token->is(TT_ArrayInitializerLSquare) && Commas.size() < 19)
return;
// Column format doesn't really make sense if we don't align after brackets. // Column format doesn't really make sense if we don't align after brackets.
if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign) if (Style.AlignAfterOpenBracket == FormatStyle::BAS_DontAlign)
return; return;
@ -213,7 +220,8 @@ void CommaSeparatedList::precomputeFormattingInfos(const FormatToken *Token) {
// Don't use column layout for nested lists, lists with few elements and in // Don't use column layout for nested lists, lists with few elements and in
// presence of separating comments. // presence of separating comments.
if (Token->NestingLevel != 0 || Commas.size() < 5 || HasSeparatingComment) if ((Token->NestingLevel != 0 && Token->is(tok::l_brace)) ||
Commas.size() < 5 || HasSeparatingComment)
return; return;
// We can never place more than ColumnLimit / 3 items in a row (because of the // We can never place more than ColumnLimit / 3 items in a row (because of the

View File

@ -314,6 +314,17 @@ TEST_F(FormatTestJS, ArrayLiterals) {
verifyFormat("someFunction([], {a: a});"); verifyFormat("someFunction([], {a: a});");
} }
TEST_F(FormatTestJS, ColumnLayoutForArrayLiterals) {
verifyFormat("var array = [\n"
" a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
" a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
"];");
verifyFormat("var array = someFunction([\n"
" a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
" a, a, a, a, a, a, a, a, a, a, a, a, a, a, a,\n"
"]);");
}
TEST_F(FormatTestJS, FunctionLiterals) { TEST_F(FormatTestJS, FunctionLiterals) {
verifyFormat("doFoo(function() {});"); verifyFormat("doFoo(function() {});");
verifyFormat("doFoo(function() { return 1; });"); verifyFormat("doFoo(function() { return 1; });");