diff options
| author | Daniel Jasper <djasper@google.com> | 2015-11-23 08:36:35 +0000 |
|---|---|---|
| committer | Daniel Jasper <djasper@google.com> | 2015-11-23 08:36:35 +0000 |
| commit | b68aabf7fce442b54cc8ef4af4c0a4541144afbf (patch) | |
| tree | 855e7321fa70c22c8e0b827397f6d0f4ce6c2db6 /clang/lib/Format | |
| parent | 3fed94525cf27fce7a6c0497721036b6f15a15a4 (diff) | |
| download | bcm5719-llvm-b68aabf7fce442b54cc8ef4af4c0a4541144afbf.tar.gz bcm5719-llvm-b68aabf7fce442b54cc8ef4af4c0a4541144afbf.zip | |
clang-format: Make moving of the Cursor work properly when sorting #includes.
llvm-svn: 253860
Diffstat (limited to 'clang/lib/Format')
| -rw-r--r-- | clang/lib/Format/Format.cpp | 28 |
1 files changed, 20 insertions, 8 deletions
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 2a7b9acf230..0b6c9f59650 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -1706,7 +1706,7 @@ static bool affectsRange(ArrayRef<tooling::Range> Ranges, unsigned Start, static void sortIncludes(const FormatStyle &Style, const SmallVectorImpl<IncludeDirective> &Includes, ArrayRef<tooling::Range> Ranges, StringRef FileName, - tooling::Replacements &Replaces) { + tooling::Replacements &Replaces, unsigned *Cursor) { if (!affectsRange(Ranges, Includes.front().Offset, Includes.back().Offset + Includes.back().Text.size())) return; @@ -1730,10 +1730,21 @@ static void sortIncludes(const FormatStyle &Style, if (!OutOfOrder) return; - std::string result = Includes[Indices[0]].Text; - for (unsigned i = 1, e = Indices.size(); i != e; ++i) { - result += "\n"; - result += Includes[Indices[i]].Text; + std::string result; + bool CursorMoved = false; + for (unsigned Index : Indices) { + if (!result.empty()) + result += "\n"; + result += Includes[Index].Text; + + if (Cursor && !CursorMoved) { + unsigned Start = Includes[Index].Offset; + unsigned End = Start + Includes[Index].Text.size(); + if (*Cursor >= Start && *Cursor < End) { + *Cursor = Includes.front().Offset + result.size() + *Cursor - End; + CursorMoved = true; + } + } } // Sorting #includes shouldn't change their total number of characters. @@ -1748,7 +1759,7 @@ static void sortIncludes(const FormatStyle &Style, tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, ArrayRef<tooling::Range> Ranges, - StringRef FileName) { + StringRef FileName, unsigned *Cursor) { tooling::Replacements Replaces; if (!Style.SortIncludes) return Replaces; @@ -1811,7 +1822,8 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, LookForMainHeader = false; IncludesInBlock.push_back({IncludeName, Line, Prev, Category}); } else if (!IncludesInBlock.empty()) { - sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces); + sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, + Cursor); IncludesInBlock.clear(); } Prev = Pos + 1; @@ -1821,7 +1833,7 @@ tooling::Replacements sortIncludes(const FormatStyle &Style, StringRef Code, SearchFrom = Pos + 1; } if (!IncludesInBlock.empty()) - sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces); + sortIncludes(Style, IncludesInBlock, Ranges, FileName, Replaces, Cursor); return Replaces; } |

