[prev in list] [next in list] [prev in thread] [next in thread]
List: cfe-commits
Subject: [PATCH] D139029: [clang-format] Don't move comments if AlignTrailingComments: Kind: Leave
From: MaĆra_Canal_via_Phabricator via cfe-commits <cfe-commits () lists !
Date: 2022-11-30 21:11:56
Message-ID: kvAwYcKHQumZRk6UavPjOg () geopod-ismtpd-3-2
[Download RAW message or body]
mairacanal updated this revision to Diff 479055.
mairacanal added a comment.
- Instead of using Changes[i].NewlinesBefore use Changes[i].Tok->NewlinesBefore
- Create test cases of multiple blank lines
- Separate \n\n on test cases
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D139029/new/
https://reviews.llvm.org/D139029
Files:
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/FormatTestComments.cpp
Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -3062,6 +3062,61 @@
"int d;// comment\n",
Style));
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ Style));
+
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "\n"
+ "// comment\n",
+ Style));
+
+
+ // Allow to keep 2 empty lines
+ Style.MaxEmptyLinesToKeep = 2;
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ Style));
+
// Just format comments normally when leaving exceeds the column limit
Style.ColumnLimit = 35;
EXPECT_EQ("int foo = 12345; // comment\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -957,7 +957,8 @@
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
auto OriginalSpaces =
Changes[i].OriginalWhitespaceRange.getEnd().getRawEncoding() -
- Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding();
+ Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding() -
+ Changes[i].Tok->NewlinesBefore;
unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
Changes[i].TokenLength + OriginalSpaces;
// If leaving comments makes the line exceed the column limit, give up to
["D139029.479055.patch" (D139029.479055.patch)]
Index: clang/unittests/Format/FormatTestComments.cpp
===================================================================
--- clang/unittests/Format/FormatTestComments.cpp
+++ clang/unittests/Format/FormatTestComments.cpp
@@ -3062,6 +3062,61 @@
"int d;// comment\n",
Style));
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ Style));
+
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "\n"
+ "// comment\n",
+ Style));
+
+
+ // Allow to keep 2 empty lines
+ Style.MaxEmptyLinesToKeep = 2;
+ EXPECT_EQ("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ format("// do not touch\n"
+ "int a; // any comments\n"
+ "\n"
+ "\n"
+ " // comment\n"
+ "// comment\n"
+ "\n"
+ "// comment\n",
+ Style));
+
// Just format comments normally when leaving exceeds the column limit
Style.ColumnLimit = 35;
EXPECT_EQ("int foo = 12345; // comment\n"
Index: clang/lib/Format/WhitespaceManager.cpp
===================================================================
--- clang/lib/Format/WhitespaceManager.cpp
+++ clang/lib/Format/WhitespaceManager.cpp
@@ -957,7 +957,8 @@
if (Style.AlignTrailingComments.Kind == FormatStyle::TCAS_Leave) {
auto OriginalSpaces =
Changes[i].OriginalWhitespaceRange.getEnd().getRawEncoding() -
- Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding();
+ Changes[i].OriginalWhitespaceRange.getBegin().getRawEncoding() -
+ Changes[i].Tok->NewlinesBefore;
unsigned RestoredLineLength = Changes[i].StartOfTokenColumn +
Changes[i].TokenLength + OriginalSpaces;
// If leaving comments makes the line exceed the column limit, give up to
[Attachment #4 (text/plain)]
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic