Skip to content

Commit 18163c8

Browse files
committed
[clang-format] Do not update cursor pos if no includes replacement
1 parent 98c6aa7 commit 18163c8

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

clang/lib/Format/Format.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3119,6 +3119,7 @@ static void sortCppIncludes(const FormatStyle &Style,
31193119
return;
31203120
}
31213121

3122+
unsigned NewCursor = UINT_MAX;
31223123
std::string result;
31233124
for (unsigned Index : Indices) {
31243125
if (!result.empty()) {
@@ -3131,20 +3132,24 @@ static void sortCppIncludes(const FormatStyle &Style,
31313132
}
31323133
result += Includes[Index].Text;
31333134
if (Cursor && CursorIndex == Index)
3134-
*Cursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
3135+
NewCursor = IncludesBeginOffset + result.size() - CursorToEOLOffset;
31353136
CurrentCategory = Includes[Index].Category;
31363137
}
31373138

3138-
if (Cursor && *Cursor >= IncludesEndOffset)
3139-
*Cursor += result.size() - IncludesBlockSize;
3140-
31413139
// If the #includes are out of order, we generate a single replacement fixing
31423140
// the entire range of blocks. Otherwise, no replacement is generated.
31433141
if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
31443142
IncludesBeginOffset, IncludesBlockSize)))) {
31453143
return;
31463144
}
31473145

3146+
if (Cursor) {
3147+
if (UINT_MAX != NewCursor)
3148+
*Cursor = NewCursor;
3149+
else if (*Cursor >= IncludesEndOffset)
3150+
*Cursor += result.size() - IncludesBlockSize;
3151+
}
3152+
31483153
auto Err = Replaces.add(tooling::Replacement(
31493154
FileName, Includes.front().Offset, IncludesBlockSize, result));
31503155
// FIXME: better error handling. For now, just skip the replacement for the

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,30 @@ TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
821821
EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
822822
}
823823

824+
TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) {
825+
Style.IncludeBlocks = Style.IBS_Regroup;
826+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
827+
Style.IncludeCategories = {
828+
{"^\"aa\"", 1, 0, false},
829+
{"^\"b\"", 1, 1, false},
830+
{".*", 2, 2, false}};
831+
std::string Code = "#include \"aa\"\r\n" // Start of line: 0
832+
"\r\n" // Start of line: 15
833+
"#include \"b\"\r\n" // Start of line: 17
834+
"\r\n" // Start of line: 31
835+
"#include \"c\"\r\n" // Start of line: 33
836+
"\r\n" // Start of line: 47
837+
"int i;"; // Start of line: 49
838+
EXPECT_EQ(Code, sort(Code));
839+
EXPECT_EQ(0u, newCursor(Code, 0));
840+
EXPECT_EQ(15u, newCursor(Code, 15));
841+
EXPECT_EQ(17u, newCursor(Code, 17));
842+
EXPECT_EQ(31u, newCursor(Code, 31));
843+
EXPECT_EQ(33u, newCursor(Code, 33));
844+
EXPECT_EQ(47u, newCursor(Code, 47));
845+
EXPECT_EQ(49u, newCursor(Code, 49));
846+
}
847+
824848
TEST_F(SortIncludesTest, DeduplicateIncludes) {
825849
EXPECT_EQ("#include <a>\n"
826850
"#include <b>\n"

0 commit comments

Comments
 (0)