Index: lib/Format/WhitespaceManager.cpp =================================================================== --- lib/Format/WhitespaceManager.cpp +++ lib/Format/WhitespaceManager.cpp @@ -197,7 +197,8 @@ // finalize the previous sequence. template static void AlignTokens(const FormatStyle &Style, F &&Matches, - SmallVector &Changes) { + SmallVector &Changes, + bool AligningAssignments) { unsigned MinColumn = 0; unsigned MaxColumn = UINT_MAX; @@ -214,9 +215,9 @@ unsigned NestingLevelOfLastMatch = 0; unsigned NestingLevel = 0; - // Keep track of the number of commas before the matching tokens, we will only - // align a sequence of matching tokens if they are preceded by the same number - // of commas. + // Keep track of the number of commas before the matching tokens, when + // aligning assignments we will only align a sequence of matching tokens + // if they are preceded by the same number of commas. unsigned CommasBeforeLastMatch = 0; unsigned CommasBeforeMatch = 0; @@ -271,10 +272,10 @@ continue; // If there is more than one matching token per line, or if the number of - // preceding commas, or the scope depth, do not match anymore, end the - // sequence. - if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch || - NestingLevel != NestingLevelOfLastMatch) + // preceding commas when aligning assignments, or the scope depth, do not + // match anymore, end the sequence. + if (FoundMatchOnLine || NestingLevel != NestingLevelOfLastMatch || + (AligningAssignments && CommasBeforeMatch != CommasBeforeLastMatch)) AlignCurrentSequence(); CommasBeforeLastMatch = CommasBeforeMatch; @@ -321,7 +322,7 @@ return C.Kind == tok::equal; }, - Changes); + Changes, true); } void WhitespaceManager::alignConsecutiveDeclarations() { @@ -336,7 +337,7 @@ // SomeVeryLongType const& v3; AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; }, - Changes); + Changes, false); } void WhitespaceManager::alignTrailingComments() { Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -9310,6 +9310,10 @@ verifyFormat("int oneTwoThree{0}; // comment\n" "unsigned oneTwo; // comment", Alignment); + verifyFormat("template struct pair {};\n" + "int a;\n" + "pair b;", + Alignment); EXPECT_EQ("float const a = 5;\n" "\n" "int oneTwoThree = 123;",