mirror of https://github.com/microsoft/clang.git
[clang-format] Sort whole block of using declarations while partially formatting
Summary: This patch enables sorting the full block of using declarations when some line is affected. Reviewers: djasper Reviewed By: djasper Subscribers: cfe-commits, klimek Differential Revision: https://reviews.llvm.org/D39024 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@316130 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
070fec4a83
commit
ac9a20e957
|
@ -76,6 +76,17 @@ std::string computeUsingDeclarationLabel(const FormatToken *UsingTok) {
|
||||||
void endUsingDeclarationBlock(
|
void endUsingDeclarationBlock(
|
||||||
SmallVectorImpl<UsingDeclaration> *UsingDeclarations,
|
SmallVectorImpl<UsingDeclaration> *UsingDeclarations,
|
||||||
const SourceManager &SourceMgr, tooling::Replacements *Fixes) {
|
const SourceManager &SourceMgr, tooling::Replacements *Fixes) {
|
||||||
|
bool BlockAffected = false;
|
||||||
|
for (const UsingDeclaration& Declaration : *UsingDeclarations) {
|
||||||
|
if (Declaration.Line->Affected) {
|
||||||
|
BlockAffected = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!BlockAffected) {
|
||||||
|
UsingDeclarations->clear();
|
||||||
|
return;
|
||||||
|
}
|
||||||
SmallVector<UsingDeclaration, 4> SortedUsingDeclarations(
|
SmallVector<UsingDeclaration, 4> SortedUsingDeclarations(
|
||||||
UsingDeclarations->begin(), UsingDeclarations->end());
|
UsingDeclarations->begin(), UsingDeclarations->end());
|
||||||
std::stable_sort(SortedUsingDeclarations.begin(),
|
std::stable_sort(SortedUsingDeclarations.begin(),
|
||||||
|
@ -122,7 +133,7 @@ tooling::Replacements UsingDeclarationsSorter::analyze(
|
||||||
tooling::Replacements Fixes;
|
tooling::Replacements Fixes;
|
||||||
SmallVector<UsingDeclaration, 4> UsingDeclarations;
|
SmallVector<UsingDeclaration, 4> UsingDeclarations;
|
||||||
for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
|
for (size_t I = 0, E = AnnotatedLines.size(); I != E; ++I) {
|
||||||
if (!AnnotatedLines[I]->Affected || AnnotatedLines[I]->InPPDirective ||
|
if (AnnotatedLines[I]->InPPDirective ||
|
||||||
!AnnotatedLines[I]->startsWith(tok::kw_using) ||
|
!AnnotatedLines[I]->startsWith(tok::kw_using) ||
|
||||||
AnnotatedLines[I]->First->Finalized) {
|
AnnotatedLines[I]->First->Finalized) {
|
||||||
endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
|
endUsingDeclarationBlock(&UsingDeclarations, SourceMgr, &Fixes);
|
||||||
|
|
|
@ -291,13 +291,41 @@ TEST_F(UsingDeclarationsSorterTest, SupportsClangFormatOff) {
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_F(UsingDeclarationsSorterTest, SortsPartialRangeOfUsingDeclarations) {
|
TEST_F(UsingDeclarationsSorterTest, SortsPartialRangeOfUsingDeclarations) {
|
||||||
EXPECT_EQ("using b;\n"
|
// Sorts the whole block of using declarations surrounding the range.
|
||||||
"using a;\n"
|
EXPECT_EQ("using a;\n"
|
||||||
|
"using b;\n"
|
||||||
"using c;",
|
"using c;",
|
||||||
sortUsingDeclarations("using b;\n"
|
sortUsingDeclarations("using b;\n"
|
||||||
"using c;\n" // starts at offset 10
|
"using c;\n" // starts at offset 10
|
||||||
"using a;",
|
"using a;",
|
||||||
{tooling::Range(10, 15)}));
|
{tooling::Range(10, 15)}));
|
||||||
|
EXPECT_EQ("using a;\n"
|
||||||
|
"using b;\n"
|
||||||
|
"using c;\n"
|
||||||
|
"using A = b;",
|
||||||
|
sortUsingDeclarations("using b;\n"
|
||||||
|
"using c;\n" // starts at offset 10
|
||||||
|
"using a;\n"
|
||||||
|
"using A = b;",
|
||||||
|
{tooling::Range(10, 15)}));
|
||||||
|
|
||||||
|
EXPECT_EQ("using d;\n"
|
||||||
|
"using c;\n"
|
||||||
|
"\n"
|
||||||
|
"using a;\n"
|
||||||
|
"using b;\n"
|
||||||
|
"\n"
|
||||||
|
"using f;\n"
|
||||||
|
"using e;",
|
||||||
|
sortUsingDeclarations("using d;\n"
|
||||||
|
"using c;\n"
|
||||||
|
"\n"
|
||||||
|
"using b;\n" // starts at offset 19
|
||||||
|
"using a;\n"
|
||||||
|
"\n"
|
||||||
|
"using f;\n"
|
||||||
|
"using e;",
|
||||||
|
{tooling::Range(19, 1)}));
|
||||||
}
|
}
|
||||||
|
|
||||||
} // end namespace
|
} // end namespace
|
||||||
|
|
Loading…
Reference in New Issue