]> granicus.if.org Git - clang/commitdiff
clang-format: Make trailing commas in array inits force one per line.
authorDaniel Jasper <djasper@google.com>
Fri, 27 Feb 2015 08:41:05 +0000 (08:41 +0000)
committerDaniel Jasper <djasper@google.com>
Fri, 27 Feb 2015 08:41:05 +0000 (08:41 +0000)
Before:
  NSArray *array = @[ @"a", @"a", ];

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

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@230741 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Format/TokenAnnotator.cpp
unittests/Format/FormatTest.cpp

index fa086a9773fac316f1cb695e46841c98695febe6..7c35545b50fdfae4efc92b6e858853f5ceb3e058 100644 (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()))
index 9738877e453d58c13a74d28b6728bb9063d01cda..202879827d68cb789fc42cdf32f33675653ac115 100644 (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"