From: Daniel Jasper Date: Mon, 14 Apr 2014 12:05:05 +0000 (+0000) Subject: clang-format: With ColumnLimit=0, keep short array literals on a line. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=7701d043851456661e4885ef37bac679199e81d7;p=clang clang-format: With ColumnLimit=0, keep short array literals on a line. Before: NSArray* a = [[NSArray alloc] initWithArray:@[ @"a" ] copyItems:YES]; After: NSArray* a = [[NSArray alloc] initWithArray:@[ @"a" ] copyItems:YES]; This fixed llvm.org/PR19080. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@206161 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 53b5867965..43997f9957 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -142,6 +142,7 @@ bool ContinuationIndenter::mustBreak(const LineState &State) { return true; if (((Previous.Type == TT_DictLiteral && Previous.is(tok::l_brace)) || Previous.Type == TT_ArrayInitializerLSquare) && + (Style.ColumnLimit > 0 || Previous.ParameterCount > 1) && getLengthToMatchingParen(Previous) + State.Column > getColumnLimit(State)) return true; if (Current.Type == TT_CtorInitializerColon && diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 0dc06594f1..7bae21b89e 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -8148,6 +8148,20 @@ TEST_F(FormatTest, FormatsWithWebKitStyle) { format("#define aNumber \\\n" " 10", Style)); + + // Keep empty and one-element array literals on a single line. + verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[]\n" + " copyItems:YES];", + Style); + verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[ @\"a\" ]\n" + " copyItems:YES];", + Style); + verifyFormat("NSArray* a = [[NSArray alloc] initWithArray:@[\n" + " @\"a\",\n" + " @\"a\"\n" + " ]\n" + " copyItems:YES];", + Style); } TEST_F(FormatTest, FormatsLambdas) {