Index: docs/ClangFormatStyleOptions.rst =================================================================== --- docs/ClangFormatStyleOptions.rst +++ docs/ClangFormatStyleOptions.rst @@ -160,6 +160,17 @@ argument2); \endcode +**AlignConsecutiveAssignments** (``bool``) + If ``true``, aligns consecutive assignments. + + This will align the assignment operators of consecutive lines. This + will result in formattings like + \code + int aaaa = 12; + int b = 23; + int ccc = 23; + \endcode + **AlignEscapedNewlinesLeft** (``bool``) If ``true``, aligns escaped newlines as far left as possible. Otherwise puts them into the right-most column. Index: include/clang/Format/Format.h =================================================================== --- include/clang/Format/Format.h +++ include/clang/Format/Format.h @@ -247,6 +247,17 @@ /// \brief If \c true, aligns trailing comments. bool AlignTrailingComments; + /// \brief If \c true, aligns consecutive assignments. + /// + /// This will align the assignment operators of consecutive lines. This + /// will result in formattings like + /// \code + /// int aaaa = 12; + /// int b = 23; + /// int ccc = 23; + /// \endcode + bool AlignConsecutiveAssignments; + /// \brief If \c true, aligns escaped newlines as far left as possible. /// Otherwise puts them into the right-most column. bool AlignEscapedNewlinesLeft; Index: lib/Format/Format.cpp =================================================================== --- lib/Format/Format.cpp +++ lib/Format/Format.cpp @@ -174,6 +174,7 @@ IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft); IO.mapOptional("AlignOperands", Style.AlignOperands); IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments); + IO.mapOptional("AlignConsecutiveAssignments", Style.AlignConsecutiveAssignments); IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine", Style.AllowAllParametersOfDeclarationOnNextLine); IO.mapOptional("AllowShortBlocksOnASingleLine", @@ -329,6 +330,7 @@ LLVMStyle.AlignAfterOpenBracket = true; LLVMStyle.AlignOperands = true; LLVMStyle.AlignTrailingComments = true; + LLVMStyle.AlignConsecutiveAssignments = false; LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; LLVMStyle.AllowShortBlocksOnASingleLine = false; Index: lib/Format/WhitespaceManager.h =================================================================== --- lib/Format/WhitespaceManager.h +++ lib/Format/WhitespaceManager.h @@ -164,6 +164,13 @@ /// \c EscapedNewlineColumn for the first tokens or token parts in a line. void calculateLineBreakInformation(); + /// \brief Align consecutive assignments over all \c Changes. + void alignConsecutiveAssignments(); + + /// \brief Align consecutive assignments from change \p Start to change \p End at + /// the specified \p Column. + void alignConsecutiveAssignments(unsigned Start, unsigned End, unsigned Column); + /// \brief Align trailing comments over all \c Changes. void alignTrailingComments(); Index: lib/Format/WhitespaceManager.cpp =================================================================== --- lib/Format/WhitespaceManager.cpp +++ lib/Format/WhitespaceManager.cpp @@ -93,6 +93,7 @@ std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr)); calculateLineBreakInformation(); + alignConsecutiveAssignments(); alignTrailingComments(); alignEscapedNewlines(); generateChanges(); @@ -141,6 +142,97 @@ } } +void WhitespaceManager::alignConsecutiveAssignments() { + if (!Style.AlignConsecutiveAssignments) + return; + + unsigned MinColumn = 0; + unsigned StartOfSequence = 0; + unsigned EndOfSequence = 0; + bool FoundAssignmentOnLine = false; + bool FoundLeftParenOnLine = false; + unsigned CurrentLine = 0; + for (unsigned i = 0, e = Changes.size(); i != e; ++i) { + if (Changes[i].NewlinesBefore == 1) { + CurrentLine += Changes[i].NewlinesBefore; + if (!FoundAssignmentOnLine && StartOfSequence > 0) { + alignConsecutiveAssignments(StartOfSequence, i, MinColumn); + MinColumn = 0; + StartOfSequence = 0; + } + FoundAssignmentOnLine = false; + FoundLeftParenOnLine = false; + } else if (Changes[i].NewlinesBefore > 1) { + CurrentLine += Changes[i].NewlinesBefore; + if (StartOfSequence > 0) { + alignConsecutiveAssignments(StartOfSequence, i, MinColumn); + MinColumn = 0; + StartOfSequence = 0; + } + FoundAssignmentOnLine = false; + FoundLeftParenOnLine = false; + } + + if ((FoundAssignmentOnLine && Changes[i].Kind == tok::equal) || + (Changes[i].Kind == tok::equal && + (Changes[i].NewlinesBefore > 0 || + Changes[i + 1].NewlinesBefore > 0)) || + (!FoundLeftParenOnLine && Changes[i].Kind == tok::r_paren)) { + if (StartOfSequence > 0) { + alignConsecutiveAssignments(StartOfSequence, EndOfSequence, MinColumn); + MinColumn = 0; + StartOfSequence = 0; + } + } else if (Changes[i].Kind == tok::l_paren) { + FoundLeftParenOnLine = true; + if (!FoundAssignmentOnLine && StartOfSequence > 0) { + alignConsecutiveAssignments(StartOfSequence, EndOfSequence, MinColumn); + MinColumn = 0; + StartOfSequence = 0; + } + } else if (!FoundAssignmentOnLine && !FoundLeftParenOnLine && + Changes[i].Kind == tok::equal) { + FoundAssignmentOnLine = true; + EndOfSequence = i; + if (StartOfSequence == 0) + StartOfSequence = i; + + unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; + MinColumn = std::max(MinColumn, ChangeMinColumn); + } + } + + if (StartOfSequence > 0) + alignConsecutiveAssignments(StartOfSequence, Changes.size(), MinColumn); +} + +void WhitespaceManager::alignConsecutiveAssignments(unsigned Start, + unsigned End, + unsigned Column) { + bool AlignedAssignment = false; + int PreviousShift = 0; + for (unsigned i = Start; i != End; ++i) { + int Shift = 0; + if (Changes[i].NewlinesBefore > 0) + AlignedAssignment = false; + if (!AlignedAssignment && Changes[i].Kind == tok::equal) { + Shift = Column - Changes[i].StartOfTokenColumn; + AlignedAssignment = true; + PreviousShift = Shift; + } + assert(Shift >= 0); + Changes[i].Spaces += Shift; + if (i + 1 != Changes.size()) + Changes[i + 1].PreviousEndOfTokenColumn += Shift; + Changes[i].StartOfTokenColumn += Shift; + if (AlignedAssignment) { + Changes[i].StartOfTokenColumn += PreviousShift; + if (i + 1 != Changes.size()) + Changes[i + 1].PreviousEndOfTokenColumn += PreviousShift; + } + } +} + void WhitespaceManager::alignTrailingComments() { unsigned MinColumn = 0; unsigned MaxColumn = UINT_MAX; Index: unittests/Format/FormatTest.cpp =================================================================== --- unittests/Format/FormatTest.cpp +++ unittests/Format/FormatTest.cpp @@ -8347,6 +8347,118 @@ verifyFormat("a or_eq 8;", Spaces); } +TEST_F(FormatTest, AlignConsecutiveAssignments) { + FormatStyle Alignment = getLLVMStyle(); + Alignment.AlignConsecutiveAssignments = false; + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", Alignment); + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", Alignment); + + Alignment.AlignConsecutiveAssignments = true; + verifyFormat("int a = 5;\n" + "int oneTwoThree = 123;", Alignment); + verifyFormat("int a = method();\n" + "int oneTwoThree = 133;", Alignment); + verifyFormat("a &= 5;\n" + "bcd *= 5;\n" + "ghtyf += 5;\n" + "dvfvdb -= 5;\n" + "a /= 5;\n" + "vdsvsv %= 5;\n" + "sfdbddfbdfbb ^= 5;\n" + "dvsdsv |= 5;\n" + "int dsvvdvsdvvv = 123;", Alignment); + verifyFormat("int i = 1, j = 10;\n" + "something = 2000;", Alignment); + verifyFormat("something = 2000;\n" + "int i = 1, j = 10;\n", Alignment); + verifyFormat("something = 2000;\n" + "another = 911;\n" + "int i = 1, j = 10;\n" + "oneMore = 1;\n" + "i = 2;", Alignment); + verifyFormat("int a = 5;\n" + "int one = 1;\n" + "method();\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", Alignment); + verifyFormat("int oneTwoThree = 123; // comment\n" + "int oneTwo = 12; // comment", Alignment); + EXPECT_EQ("int a = 5;\n" + "\n" + "int oneTwoThree = 123;", + format("int a = 5;\n" + "\n" + "int oneTwoThree= 123;", Alignment)); + EXPECT_EQ("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", + format("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;", Alignment)); + EXPECT_EQ("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", + format("int a = 5;\n" + "int one = 1;\n" + "\n" + "int oneTwoThree = 123;\n" + "int oneTwo = 12;", Alignment)); + Alignment.AlignEscapedNewlinesLeft = true; + verifyFormat("#define A \\\n" + " int aaaa = 12; \\\n" + " int b = 23; \\\n" + " int ccc = 234; \\\n" + " int dddddddddd = 2345;", Alignment); + Alignment.AlignEscapedNewlinesLeft = false; + verifyFormat("#define A \\\n" + " int aaaa = 12; \\\n" + " int b = 23; \\\n" + " int ccc = 234; \\\n" + " int dddddddddd = 2345;", Alignment); + verifyFormat("void SomeFunction(int parameter = 1, int i = 2, int j = 3, int k = 4, int l = 5,\n" + " int m = 6) {\n" + " int j = 10;\n" + " otherThing = 1;\n" + "}", Alignment); + verifyFormat("void SomeFunction(int parameter = 0) {\n" + " int i = 1;\n" + " int j = 2;\n" + " int big = 10000;\n" + "}", Alignment); + verifyFormat("class C {\n" + "public:\n" + " int i = 1;\n" + " virtual void f() = 0;\n" + "};", Alignment); + verifyFormat("int i = 1;\n" + "if (SomeType t = getSomething()) {\n" + "}\n" + "int j = 2;\n" + "int big = 10000;", Alignment); + verifyFormat("int j = 7;\n" + "for (int k = 0; k < N; ++k) {\n" + "}\n" + "int j = 2;\n" + "int big = 10000;\n" + "}", Alignment); + Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_All; + verifyFormat("int i = 1;\n" + "LooooooooooongType loooooooooooooooooooooongVariable\n" + " = someLooooooooooooooooongFunction();\n" + "int j = 2;", Alignment); + Alignment.BreakBeforeBinaryOperators = FormatStyle::BOS_None; + verifyFormat("int i = 1;\n" + "LooooooooooongType loooooooooooooooooooooongVariable =\n" + " someLooooooooooooooooongFunction();\n" + "int j = 2;", Alignment); +} + TEST_F(FormatTest, LinuxBraceBreaking) { FormatStyle LinuxBraceStyle = getLLVMStyle(); LinuxBraceStyle.BreakBeforeBraces = FormatStyle::BS_Linux; @@ -8886,6 +8998,7 @@ CHECK_PARSE_BOOL(AlignEscapedNewlinesLeft); CHECK_PARSE_BOOL(AlignOperands); CHECK_PARSE_BOOL(AlignTrailingComments); + CHECK_PARSE_BOOL(AlignConsecutiveAssignments); CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); CHECK_PARSE_BOOL(AllowShortBlocksOnASingleLine); CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);