From: Daniel Jasper Date: Sat, 9 Jan 2016 15:56:53 +0000 (+0000) Subject: clang-format: Improve selective comment formatting. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=40fd5bf45608891246cdb05f004086aac02d67f4;p=clang clang-format: Improve selective comment formatting. Starting here: int x; // Format this line only. int xx; // int xxxxx; // Before: int x; // Format this line only. int xx; // int xxxxx; // After: int x; // Format this line only. int xx; // int xxxxx; // git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@257258 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Format/WhitespaceManager.cpp b/lib/Format/WhitespaceManager.cpp index 725f05bcd8..c3b40fc7e2 100644 --- a/lib/Format/WhitespaceManager.cpp +++ b/lib/Format/WhitespaceManager.cpp @@ -55,8 +55,9 @@ void WhitespaceManager::replaceWhitespace(FormatToken &Tok, unsigned Newlines, return; Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue; Changes.push_back( - Change(true, Tok.WhitespaceRange, IndentLevel, Spaces, StartOfTokenColumn, - Newlines, "", "", Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst, + Change(/*CreateReplacement=*/true, Tok.WhitespaceRange, IndentLevel, + Spaces, StartOfTokenColumn, Newlines, "", "", Tok.Tok.getKind(), + InPPDirective && !Tok.IsFirst, Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName))); } @@ -64,11 +65,11 @@ void WhitespaceManager::addUntouchableToken(const FormatToken &Tok, bool InPPDirective) { if (Tok.Finalized) return; - Changes.push_back( - Change(false, Tok.WhitespaceRange, /*IndentLevel=*/0, - /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, "", "", - Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst, - Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName))); + Changes.push_back(Change( + /*CreateReplacement=*/false, Tok.WhitespaceRange, /*IndentLevel=*/0, + /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, "", "", + Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst, + Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName))); } void WhitespaceManager::replaceWhitespaceInToken( @@ -342,6 +343,12 @@ void WhitespaceManager::alignTrailingComments() { unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength; + + // If we don't create a replacement for this change, we have to consider + // it to be immovable. + if (!Changes[i].CreateReplacement) + ChangeMaxColumn = ChangeMinColumn; + if (i + 1 != e && Changes[i + 1].ContinuesPPDirective) ChangeMaxColumn -= 2; // If this comment follows an } in column 0, it probably documents the diff --git a/unittests/Format/FormatTestSelective.cpp b/unittests/Format/FormatTestSelective.cpp index d53d1c0429..699600c42d 100644 --- a/unittests/Format/FormatTestSelective.cpp +++ b/unittests/Format/FormatTestSelective.cpp @@ -162,6 +162,13 @@ TEST_F(FormatTestSelective, FormatsCommentsLocally) { "// This is\n" "// not formatted. ", 0, 0)); + EXPECT_EQ("int x; // Format this line.\n" + "int xx; //\n" + "int xxxxx; //", + format("int x; // Format this line.\n" + "int xx; //\n" + "int xxxxx; //", + 0, 0)); } TEST_F(FormatTestSelective, IndividualStatementsOfNestedBlocks) {