Index: lib/Format/ContinuationIndenter.cpp =================================================================== --- lib/Format/ContinuationIndenter.cpp +++ lib/Format/ContinuationIndenter.cpp @@ -1003,7 +1003,9 @@ (Current.is(TT_ArrayInitializerLSquare) && EndsInComma) || Current.is(TT_DictLiteral) || Style.Language == FormatStyle::LK_Proto || !Style.BinPackArguments || - (NextNoComment && NextNoComment->is(TT_DesignatedInitializerPeriod)); + (NextNoComment && + NextNoComment->isOneOf(TT_DesignatedInitializerPeriod, + TT_DesignatedInitializerLSquare)); if (Current.ParameterCount > 1) NestedBlockIndent = std::max(NestedBlockIndent, State.Column + 1); } else { Index: lib/Format/FormatToken.h =================================================================== --- lib/Format/FormatToken.h +++ lib/Format/FormatToken.h @@ -39,6 +39,7 @@ TYPE(ConflictStart) \ TYPE(CtorInitializerColon) \ TYPE(CtorInitializerComma) \ + TYPE(DesignatedInitializerLSquare) \ TYPE(DesignatedInitializerPeriod) \ TYPE(DictLiteral) \ TYPE(ForEachMacro) \ Index: lib/Format/TokenAnnotator.cpp =================================================================== --- lib/Format/TokenAnnotator.cpp +++ lib/Format/TokenAnnotator.cpp @@ -336,6 +336,11 @@ Contexts.back().ContextKind == tok::l_brace && Parent->isOneOf(tok::l_brace, tok::comma)) { Left->Type = TT_JsComputedPropertyName; + } else if ((Style.Language == FormatStyle::LK_Cpp || + Style.Language == FormatStyle::LK_ObjC) && + Parent && Contexts.back().ContextKind == tok::l_brace && + Parent->isOneOf(tok::l_brace, tok::comma)) { + Left->Type = TT_DesignatedInitializerLSquare; } else if (CurrentToken->is(tok::r_square) && Parent && Parent->is(TT_TemplateCloser)) { Left->Type = TT_ArraySubscriptLSquare; @@ -388,7 +393,8 @@ if (CurrentToken->isOneOf(tok::r_paren, tok::r_brace)) return false; if (CurrentToken->is(tok::colon)) { - if (Left->is(TT_ArraySubscriptLSquare)) { + if (Left->isOneOf(TT_ArraySubscriptLSquare, + TT_DesignatedInitializerLSquare)) { Left->Type = TT_ObjCMethodExpr; StartsObjCMethodExpr = true; Contexts.back().ColonIsObjCMethodExpr = true; @@ -1954,7 +1960,8 @@ if (Right.is(TT_LambdaLSquare) && Left.is(tok::equal)) return 35; if (!Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, - TT_ArrayInitializerLSquare)) + TT_ArrayInitializerLSquare, + TT_DesignatedInitializerLSquare)) return 500; } @@ -2173,7 +2180,8 @@ (Style.SpacesInSquareBrackets && Right.MatchingParen->is(TT_ArraySubscriptLSquare))); if (Right.is(tok::l_square) && - !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare) && + !Right.isOneOf(TT_ObjCMethodExpr, TT_LambdaLSquare, + TT_DesignatedInitializerLSquare) && !Left.isOneOf(tok::numeric_constant, TT_DictLiteral)) return false; if (Left.is(tok::l_brace) && Right.is(tok::r_brace)) @@ -2368,8 +2376,9 @@ if (Left.is(tok::greater) && Right.is(tok::greater)) return Right.is(TT_TemplateCloser) && Left.is(TT_TemplateCloser) && (Style.Standard != FormatStyle::LS_Cpp11 || Style.SpacesInAngles); - if (Right.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || - Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar)) + if (Right.isOneOf(tok::arrow, tok::arrowstar, tok::periodstar) || + Left.isOneOf(tok::arrow, tok::period, tok::arrowstar, tok::periodstar) || + (Right.is(tok::period) && Right.isNot(TT_DesignatedInitializerPeriod))) return false; if (!Style.SpaceBeforeAssignmentOperators && Right.getPrecedence() == prec::Assignment) Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -1521,6 +1521,19 @@ " .eeeeeeeeeeeeeeeeeeeeeeeeeee = 5};"); verifyGoogleFormat("const struct A a = {.a = 1, .b = 2};"); + + verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); + verifyFormat("const struct A a = {[1] = aaaaaaaaaa,\n" + " [2] = bbbbbbbbbb,\n" + " [3] = cccccccccc,\n" + " [4] = dddddddddd,\n" + " [5] = eeeeeeeeee};"); + verifyFormat("const struct Aaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaa = {\n" + " [1] = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa,\n" + " [2] = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb,\n" + " [3] = cccccccccccccccccccccccccccccccccccccc,\n" + " [4] = dddddddddddddddddddddddddddddddddddddd,\n" + " [5] = eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee};"); } TEST_F(FormatTest, NestedStaticInitializers) { @@ -5593,6 +5606,8 @@ " T member = {arg1, arg2};\n" "};"); verifyFormat("vector foo = {::SomeGlobalFunction()};"); + verifyFormat("const struct A a = {.a = 1, .b = 2};"); + verifyFormat("const struct A a = {[0] = 1, [1] = 2};"); verifyFormat("static_assert(std::is_integral{} + 0, \"\");"); verifyFormat("int a = std::is_integral{} + 0;"); @@ -5758,6 +5773,8 @@ " aaaaaaa,\n" " a};"); verifyFormat("vector foo = { ::SomeGlobalFunction() };", ExtraSpaces); + verifyFormat("const struct A a = { .a = 1, .b = 2 };", ExtraSpaces); + verifyFormat("const struct A a = { [0] = 1, [1] = 2 };", ExtraSpaces); } TEST_F(FormatTest, FormatsBracedListsInColumnLayout) {