[clang] db57acf - [clang-format] Adjust editor cursor position past #include blocks

via cfe-commits cfe-commits at lists.llvm.org
Wed Apr 27 14:26:31 PDT 2022


Author: owenca
Date: 2022-04-27T14:26:23-07:00
New Revision: db57acff264796c6c43214e731dbfcf97ff5f6af

URL: https://github.com/llvm/llvm-project/commit/db57acff264796c6c43214e731dbfcf97ff5f6af
DIFF: https://github.com/llvm/llvm-project/commit/db57acff264796c6c43214e731dbfcf97ff5f6af.diff

LOG: [clang-format] Adjust editor cursor position past #include blocks

Fixes #55027.

Differential Revision: https://reviews.llvm.org/D124452

Added: 
    

Modified: 
    clang/lib/Format/Format.cpp
    clang/unittests/Format/SortIncludesTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 9077330cb3b7c..b2c710174ecd9 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2638,10 +2638,10 @@ static void sortCppIncludes(const FormatStyle &Style,
                             StringRef Code, tooling::Replacements &Replaces,
                             unsigned *Cursor) {
   tooling::IncludeCategoryManager Categories(Style.IncludeStyle, FileName);
-  unsigned IncludesBeginOffset = Includes.front().Offset;
-  unsigned IncludesEndOffset =
+  const unsigned IncludesBeginOffset = Includes.front().Offset;
+  const unsigned IncludesEndOffset =
       Includes.back().Offset + Includes.back().Text.size();
-  unsigned IncludesBlockSize = IncludesEndOffset - IncludesBeginOffset;
+  const unsigned IncludesBlockSize = IncludesEndOffset - IncludesBeginOffset;
   if (!affectsRange(Ranges, IncludesBeginOffset, IncludesEndOffset))
     return;
   SmallVector<unsigned, 16> Indices =
@@ -2685,7 +2685,7 @@ static void sortCppIncludes(const FormatStyle &Style,
   // the entire block. Otherwise, no replacement is generated.
   // In case Style.IncldueStyle.IncludeBlocks != IBS_Preserve, this check is not
   // enough as additional newlines might be added or removed across #include
-  // blocks. This we handle below by generating the updated #imclude blocks and
+  // blocks. This we handle below by generating the updated #include blocks and
   // comparing it to the original.
   if (Indices.size() == Includes.size() && llvm::is_sorted(Indices) &&
       Style.IncludeStyle.IncludeBlocks == tooling::IncludeStyle::IBS_Preserve)
@@ -2706,6 +2706,9 @@ static void sortCppIncludes(const FormatStyle &Style,
     CurrentCategory = Includes[Index].Category;
   }
 
+  if (Cursor && *Cursor >= IncludesEndOffset)
+    *Cursor += result.size() - IncludesBlockSize;
+
   // If the #includes are out of order, we generate a single replacement fixing
   // the entire range of blocks. Otherwise, no replacement is generated.
   if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(

diff  --git a/clang/unittests/Format/SortIncludesTest.cpp b/clang/unittests/Format/SortIncludesTest.cpp
index a38e1a18325c2..54e43d0ba3fa4 100644
--- a/clang/unittests/Format/SortIncludesTest.cpp
+++ b/clang/unittests/Format/SortIncludesTest.cpp
@@ -890,6 +890,21 @@ TEST_F(SortIncludesTest, CalculatesCorrectCursorPosition) {
   EXPECT_EQ(10u, newCursor(Code, 43));
 }
 
+TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
+  Style.IncludeBlocks = Style.IBS_Regroup;
+  std::string Code = "#include \"b\"\n"      // Start of line: 0
+                     "\n"                    // Start of line: 13
+                     "#include \"aa\"\n"     // Start of line: 14
+                     "int i;";               // Start of line: 28
+  std::string Expected = "#include \"aa\"\n" // Start of line: 0
+                         "#include \"b\"\n"  // Start of line: 14
+                         "int i;";           // Start of line: 27
+  EXPECT_EQ(Expected, sort(Code));
+  EXPECT_EQ(12u, newCursor(Code, 26)); // Closing quote of "aa"
+  EXPECT_EQ(26u, newCursor(Code, 27)); // Newline after "aa"
+  EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
+}
+
 TEST_F(SortIncludesTest, DeduplicateIncludes) {
   EXPECT_EQ("#include <a>\n"
             "#include <b>\n"


        


More information about the cfe-commits mailing list