Skip to content

Commit 6c184f9

Browse files
committed
[clang-format] Do not update cursor pos if no includes replacement
Signed-off-by: NorthBlue333 <[email protected]>
1 parent 5b544b5 commit 6c184f9

File tree

2 files changed

+119
-3
lines changed

2 files changed

+119
-3
lines changed

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3114,6 +3114,7 @@ static void sortCppIncludes(const FormatStyle &Style,
31143114
return;
31153115
}
31163116

3117+
const auto OldCursor = Cursor ? *Cursor : 0;
31173118
std::string result;
31183119
for (unsigned Index : Indices) {
31193120
if (!result.empty()) {
@@ -3137,6 +3138,8 @@ static void sortCppIncludes(const FormatStyle &Style,
31373138
// the entire range of blocks. Otherwise, no replacement is generated.
31383139
if (replaceCRLF(result) == replaceCRLF(std::string(Code.substr(
31393140
IncludesBeginOffset, IncludesBlockSize)))) {
3141+
if (Cursor)
3142+
*Cursor = OldCursor;
31403143
return;
31413144
}
31423145

clang/unittests/Format/SortIncludesTest.cpp

Lines changed: 116 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
#include "FormatTestUtils.h"
9+
#include "FormatTestBase.h"
1010
#include "clang/Format/Format.h"
1111
#include "llvm/ADT/StringRef.h"
1212
#include "llvm/Support/Debug.h"
1313
#include "gtest/gtest.h"
1414

15-
#define DEBUG_TYPE "format-test"
15+
#define DEBUG_TYPE "sort-includes-test"
1616

1717
namespace clang {
1818
namespace format {
1919
namespace {
2020

21-
class SortIncludesTest : public ::testing::Test {
21+
class SortIncludesTest : public test::FormatTestBase {
2222
protected:
2323
std::vector<tooling::Range> GetCodeRange(StringRef Code) {
2424
return std::vector<tooling::Range>(1, tooling::Range(0, Code.size()));
@@ -821,6 +821,119 @@ TEST_F(SortIncludesTest, CalculatesCorrectCursorPositionWithRegrouping) {
821821
EXPECT_EQ(27u, newCursor(Code, 28)); // Start of last line
822822
}
823823

824+
TEST_F(SortIncludesTest,
825+
CalculatesCorrectCursorPositionWhenNoReplacementsWithRegroupingAndCRLF) {
826+
Style.IncludeBlocks = Style.IBS_Regroup;
827+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
828+
Style.IncludeCategories = {
829+
{"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
830+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
831+
"\r\n" // Start of line: 14
832+
"#include \"b\"\r\n" // Start of line: 16
833+
"\r\n" // Start of line: 30
834+
"#include \"c\"\r\n" // Start of line: 32
835+
"\r\n" // Start of line: 46
836+
"int i;"; // Start of line: 48
837+
verifyNoChange(Code);
838+
EXPECT_EQ(0u, newCursor(Code, 0));
839+
EXPECT_EQ(14u, newCursor(Code, 14));
840+
EXPECT_EQ(16u, newCursor(Code, 16));
841+
EXPECT_EQ(30u, newCursor(Code, 30));
842+
EXPECT_EQ(32u, newCursor(Code, 32));
843+
EXPECT_EQ(46u, newCursor(Code, 46));
844+
EXPECT_EQ(48u, newCursor(Code, 48));
845+
}
846+
847+
TEST_F(
848+
SortIncludesTest,
849+
CalculatesCorrectCursorPositionWhenRemoveLinesReplacementsWithRegroupingAndCRLF) {
850+
Style.IncludeBlocks = Style.IBS_Regroup;
851+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
852+
Style.IncludeCategories = {{".*", 0, 0, false}};
853+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
854+
"\r\n" // Start of line: 14
855+
"#include \"b\"\r\n" // Start of line: 16
856+
"\r\n" // Start of line: 30
857+
"#include \"c\"\r\n" // Start of line: 32
858+
"\r\n" // Start of line: 46
859+
"int i;"; // Start of line: 48
860+
std::string Expected = "#include \"a\"\r\n" // Start of line: 0
861+
"#include \"b\"\r\n" // Start of line: 14
862+
"#include \"c\"\r\n" // Start of line: 28
863+
"\r\n" // Start of line: 42
864+
"int i;"; // Start of line: 44
865+
EXPECT_EQ(Expected, sort(Code));
866+
EXPECT_EQ(0u, newCursor(Code, 0));
867+
EXPECT_EQ(
868+
14u,
869+
newCursor(Code, 14)); // cursor on empty line in include block is ignored
870+
EXPECT_EQ(14u, newCursor(Code, 16));
871+
EXPECT_EQ(
872+
30u,
873+
newCursor(Code, 30)); // cursor on empty line in include block is ignored
874+
EXPECT_EQ(28u, newCursor(Code, 32));
875+
EXPECT_EQ(42u, newCursor(Code, 46));
876+
EXPECT_EQ(44u, newCursor(Code, 48));
877+
}
878+
879+
TEST_F(
880+
SortIncludesTest,
881+
CalculatesCorrectCursorPositionWhenNewLineReplacementsWithRegroupingAndCRLF) {
882+
Style.IncludeBlocks = Style.IBS_Regroup;
883+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
884+
Style.IncludeCategories = {
885+
{"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
886+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
887+
"#include \"b\"\r\n" // Start of line: 14
888+
"#include \"c\"\r\n" // Start of line: 28
889+
"\r\n" // Start of line: 42
890+
"int i;"; // Start of line: 44
891+
std::string Expected = "#include \"a\"\r\n" // Start of line: 0
892+
"\r\n" // Start of line: 14
893+
"#include \"b\"\r\n" // Start of line: 16
894+
"\r\n" // Start of line: 30
895+
"#include \"c\"\r\n" // Start of line: 32
896+
"\r\n" // Start of line: 46
897+
"int i;"; // Start of line: 48
898+
EXPECT_EQ(Expected, sort(Code));
899+
EXPECT_EQ(0u, newCursor(Code, 0));
900+
EXPECT_EQ(15u, newCursor(Code, 16));
901+
EXPECT_EQ(30u, newCursor(Code, 32));
902+
EXPECT_EQ(44u, newCursor(Code, 46));
903+
EXPECT_EQ(46u, newCursor(Code, 48));
904+
}
905+
906+
TEST_F(
907+
SortIncludesTest,
908+
CalculatesCorrectCursorPositionWhenNoNewLineReplacementsWithRegroupingAndCRLF) {
909+
Style.IncludeBlocks = Style.IBS_Regroup;
910+
FmtStyle.LineEnding = FormatStyle::LE_CRLF;
911+
Style.IncludeCategories = {
912+
{"^\"a\"", 0, 0, false}, {"^\"b\"", 1, 1, false}, {".*", 2, 2, false}};
913+
std::string Code = "#include \"a\"\r\n" // Start of line: 0
914+
"\r\n" // Start of line: 14
915+
"#include \"c\"\r\n" // Start of line: 16
916+
"\r\n" // Start of line: 30
917+
"#include \"b\"\r\n" // Start of line: 32
918+
"\r\n" // Start of line: 46
919+
"int i;"; // Start of line: 48
920+
std::string Expected = "#include \"a\"\r\n" // Start of line: 0
921+
"\r\n" // Start of line: 14
922+
"#include \"b\"\r\n" // Start of line: 16
923+
"\r\n" // Start of line: 30
924+
"#include \"c\"\r\n" // Start of line: 32
925+
"\r\n" // Start of line: 46
926+
"int i;"; // Start of line: 48
927+
EXPECT_EQ(Expected, sort(Code));
928+
EXPECT_EQ(0u, newCursor(Code, 0));
929+
EXPECT_EQ(14u, newCursor(Code, 14));
930+
EXPECT_EQ(30u, newCursor(Code, 32));
931+
EXPECT_EQ(30u, newCursor(Code, 30));
932+
EXPECT_EQ(15u, newCursor(Code, 15));
933+
EXPECT_EQ(44u, newCursor(Code, 46));
934+
EXPECT_EQ(46u, newCursor(Code, 48));
935+
}
936+
824937
TEST_F(SortIncludesTest, DeduplicateIncludes) {
825938
EXPECT_EQ("#include <a>\n"
826939
"#include <b>\n"

0 commit comments

Comments
 (0)