From 854ca794c17679ba4d0e529e8eb8b6b3703d1984 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Thu, 23 May 2013 20:46:07 +0000 Subject: [PATCH] Fix aligning of comments. Previously we started sequences to align for single line comments when the previous line had a trailing comment, but the sequence was broken for other reasons. Now we re-format: // a // b f(); // c to: // a // b f(); // c git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@182608 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Format/WhitespaceManager.cpp | 9 +++++---- unittests/Format/FormatTest.cpp | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index e6c363b373..f7b65fc72c 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -170,10 +170,11 @@ 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) || - WasAlignedWithStartOfNextLine; + BreakBeforeNext = + (i == 0) || (Changes[i].NewlinesBefore > 1) || + // Never start a sequence with a comment at the beginning of + // the line. + (Changes[i].NewlinesBefore == 1 && StartOfSequence == i); Newlines = 0; } } diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 5346bdc757..ce551e9d1b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -657,6 +657,26 @@ TEST_F(FormatTest, UnderstandsSingleLineComments) { format("lineWith(); // comment\n" "// at start\n" "otherLine(); // comment")); + EXPECT_EQ("lineWith();\n" + "// at start\n" + "otherLine(); // comment", + format("lineWith();\n" + " // at start\n" + "otherLine(); // comment")); + EXPECT_EQ("// first\n" + "// at start\n" + "otherLine(); // comment", + format("// first\n" + " // at start\n" + "otherLine(); // comment")); + EXPECT_EQ("f();\n" + "// first\n" + "// at start\n" + "otherLine(); // comment", + format("f();\n" + "// first\n" + " // at start\n" + "otherLine(); // comment")); } TEST_F(FormatTest, CanFormatCommentsLocally) { -- 2.50.1