From: Daniel Jasper Date: Wed, 21 May 2014 13:26:58 +0000 (+0000) Subject: clang-format: Fix corner case working around one-per-line dict literals. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=eea3b587677473be19533cf27db0b748a0c9e9b2;p=clang clang-format: Fix corner case working around one-per-line dict literals. Before: var object_literal_with_long_name = { a: 'aaaaaaaaaaaaaaaaaa', b: 'bbbbbbbbbbbbbbbbbb' }; After: var object_literal_with_long_name = { a: 'aaaaaaaaaaaaaaaaaa', b: 'bbbbbbbbbbbbbbbbbb' }; git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@209296 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/ContinuationIndenter.cpp b/lib/Format/ContinuationIndenter.cpp index 389047945c..e1a559d0df 100644 --- a/lib/Format/ContinuationIndenter.cpp +++ b/lib/Format/ContinuationIndenter.cpp @@ -444,11 +444,13 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State, if (State.Stack.back().AvoidBinPacking) { // If we are breaking after '(', '{', '<', this is not bin packing - // unless AllowAllParametersOfDeclarationOnNextLine is false. + // unless AllowAllParametersOfDeclarationOnNextLine is false or this is a + // dict/object literal. if (!(Previous.isOneOf(tok::l_paren, tok::l_brace) || Previous.Type == TT_BinaryOperator) || (!Style.AllowAllParametersOfDeclarationOnNextLine && - State.Line->MustBeDeclaration)) + State.Line->MustBeDeclaration) || + Previous.Type == TT_DictLiteral) State.Stack.back().BreakBeforeParameter = true; } diff --git a/unittests/Format/FormatTestJS.cpp b/unittests/Format/FormatTestJS.cpp index 257c247765..2aac89eac8 100644 --- a/unittests/Format/FormatTestJS.cpp +++ b/unittests/Format/FormatTestJS.cpp @@ -92,6 +92,11 @@ TEST_F(FormatTestJS, SpacesInContainerLiterals) { verifyFormat("var arr = [1, 2, 3];"); verifyFormat("var obj = {a: 1, b: 2, c: 3};"); + verifyFormat("var object_literal_with_long_name = {\n" + " a: 'aaaaaaaaaaaaaaaaaa',\n" + " b: 'bbbbbbbbbbbbbbbbbb'\n" + "};"); + verifyFormat("var obj = {a: 1, b: 2, c: 3};", getChromiumStyle(FormatStyle::LK_JavaScript)); verifyFormat("someVariable = {'a': [{}]};");