clang-format: Make trailing commas in array inits force one per line.

Before:
  NSArray *array = @[ @"a", @"a", ];

After:
  NSArray *array = @[
    @"a",
    @"a",
  ];

llvm-svn: 230741
This commit is contained in:
Daniel Jasper 2015-02-27 08:41:05 +00:00
parent 7fe82ad80b
commit 308062bd0d
2 changed files with 9 additions and 2 deletions

View File

@ -1870,9 +1870,12 @@ bool TokenAnnotator::mustBreakBefore(const AnnotatedLine &Line,
// intention is to insert a line break after it in order to make shuffling
// around entries easier.
const FormatToken *BeforeClosingBrace = nullptr;
if (Left.is(tok::l_brace) && Left.BlockKind != BK_Block && Left.MatchingParen)
if (Left.isOneOf(tok::l_brace, TT_ArrayInitializerLSquare) &&
Left.BlockKind != BK_Block && Left.MatchingParen)
BeforeClosingBrace = Left.MatchingParen->Previous;
else if (Right.is(tok::r_brace) && Right.BlockKind != BK_Block)
else if (Right.MatchingParen &&
Right.MatchingParen->isOneOf(tok::l_brace,
TT_ArrayInitializerLSquare))
BeforeClosingBrace = &Left;
if (BeforeClosingBrace && (BeforeClosingBrace->is(tok::comma) ||
BeforeClosingBrace->isTrailingComment()))

View File

@ -7216,6 +7216,10 @@ TEST_F(FormatTest, ObjCArrayLiterals) {
" @\"aaaaaaaaaaaaaaaaa\",\n"
" @\"aaaaaaaaaaaaaaaaa\"\n"
"];");
verifyFormat("NSArray *array = @[\n"
" @\"a\",\n"
" @\"a\",\n" // Trailing comma -> one per line.
"];");
// We should try to be robust in case someone forgets the "@".
verifyFormat("NSArray *some_variable = [\n"