From: Manuel Klimek Date: Thu, 23 May 2013 19:54:43 +0000 (+0000) Subject: Fix aligning of comments that are at the start of the line. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=23ad339cdb8ee55c5f243bc2573c5f9d6144a647;p=clang Fix aligning of comments that are at the start of the line. Now correctly leaves: f(); // comment // comment g(); // comment ... alone if the middle comment was aligned with g() before formatting. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182605 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index d4be86e586..e6c363b373 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -170,9 +170,10 @@ void WhitespaceManager::alignTrailingComments() { MinColumn = std::max(MinColumn, ChangeMinColumn); MaxColumn = std::min(MaxColumn, ChangeMaxColumn); } - BreakBeforeNext = - (i == 0) || (Changes[i].NewlinesBefore > 1) || - (Changes[i].NewlinesBefore == 1 && !Changes[i - 1].IsTrailingComment); + BreakBeforeNext = (i == 0) || (Changes[i].NewlinesBefore > 1) || + (Changes[i].NewlinesBefore == 1 && + !Changes[i - 1].IsTrailingComment) || + WasAlignedWithStartOfNextLine; Newlines = 0; } } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 1215be17c4..5346bdc757 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -650,6 +650,13 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { format("lineWith(); // comment\n" " // at start\n" "otherLine();")); + + EXPECT_EQ("lineWith(); // comment\n" + "// at start\n" + "otherLine(); // comment", + format("lineWith(); // comment\n" + "// at start\n" + "otherLine(); // comment")); } TEST_F(FormatTest, CanFormatCommentsLocally) {