summaryrefslogtreecommitdiff
path: root/src/plugins/clangtools/clangfixitsrefactoringchanges.cpp
diff options
context:
space:
mode:
authorIvan Donchevskii <ivan.donchevskii@qt.io>2019-02-13 14:17:21 +0100
committerIvan Donchevskii <ivan.donchevskii@qt.io>2019-02-19 07:33:13 +0000
commit9bcc871ece3e23e9f67264c45a35108f00260294 (patch)
tree928984eaa18bcb73915e7272fa91b15d94a0eaa0 /src/plugins/clangtools/clangfixitsrefactoringchanges.cpp
parent660cd8da1968c51fd854e1763ce76ef833aabc9c (diff)
downloadqt-creator-9bcc871ece3e23e9f67264c45a35108f00260294.tar.gz
ClangFormat: Format multiple text ranges at once
'reformat' function in LibFormat accepts mutilple ranges. Let's provide the ranges for the same file together when formatting on save and formatting after fix-its. Change-Id: I27789da83a1efc27beb57acf238508a191562bb9 Reviewed-by: Marco Bubke <marco.bubke@qt.io>
Diffstat (limited to 'src/plugins/clangtools/clangfixitsrefactoringchanges.cpp')
-rw-r--r--src/plugins/clangtools/clangfixitsrefactoringchanges.cpp51
1 files changed, 30 insertions, 21 deletions
diff --git a/src/plugins/clangtools/clangfixitsrefactoringchanges.cpp b/src/plugins/clangtools/clangfixitsrefactoringchanges.cpp
index 88b17fb42a..4be95a2744 100644
--- a/src/plugins/clangtools/clangfixitsrefactoringchanges.cpp
+++ b/src/plugins/clangtools/clangfixitsrefactoringchanges.cpp
@@ -89,6 +89,10 @@ bool FixitsRefactoringFile::apply()
= CppTools::CppCodeStyleSettings::currentProjectTabSettings();
// Apply changes
+ std::unique_ptr<TextEditor::Indenter> indenter;
+ QString lastFilename;
+ ReplacementOperations operationsForFile;
+
for (int i=0; i < m_replacementOperations.size(); ++i) {
ReplacementOperation &op = *m_replacementOperations[i];
if (op.apply) {
@@ -103,15 +107,19 @@ bool FixitsRefactoringFile::apply()
// Apply
QTextDocument *doc = document(op.fileName);
- std::unique_ptr<TextEditor::Indenter> indenter(factory->createIndenter(doc));
- indenter->setFileName(Utils::FileName::fromString(op.fileName));
+ if (lastFilename != op.fileName) {
+ if (indenter)
+ format(*indenter, doc, operationsForFile, i);
+ operationsForFile.clear();
+ indenter = std::unique_ptr<TextEditor::Indenter>(factory->createIndenter(doc));
+ indenter->setFileName(Utils::FileName::fromString(op.fileName));
+ }
QTextCursor cursor(doc);
cursor.setPosition(op.pos);
cursor.setPosition(op.pos + op.length, QTextCursor::KeepAnchor);
cursor.insertText(op.text);
-
- tryToFormat(*indenter, tabSettings, doc, op, i);
+ operationsForFile.push_back(&op);
}
}
@@ -130,28 +138,29 @@ bool FixitsRefactoringFile::apply()
return true;
}
-void FixitsRefactoringFile::tryToFormat(TextEditor::Indenter &indenter,
- const TextEditor::TabSettings &tabSettings,
- QTextDocument *doc,
- const ReplacementOperation &op,
- int currentIndex)
+void FixitsRefactoringFile::format(TextEditor::Indenter &indenter,
+ QTextDocument *doc,
+ const ReplacementOperations &operationsForFile,
+ int firstOperationIndex)
{
- QTextCursor cursor(doc);
- cursor.beginEditBlock();
- cursor.setPosition(op.pos);
- cursor.movePosition(QTextCursor::Right,
- QTextCursor::KeepAnchor,
- op.text.length());
- const Replacements replacements = indenter.format(cursor, tabSettings);
- cursor.endEditBlock();
+ if (operationsForFile.isEmpty())
+ return;
+
+ TextEditor::RangesInLines ranges;
+ for (int i = 0; i < operationsForFile.size(); ++i) {
+ const ReplacementOperation &op = *operationsForFile.at(i);
+ const int start = doc->findBlock(op.pos).blockNumber() + 1;
+ const int end = doc->findBlock(op.pos + op.length).blockNumber() + 1;
+ ranges.push_back({start, end});
+ }
+ const Replacements replacements = indenter.format(ranges);
if (replacements.empty())
return;
- if (hasIntersection(op.fileName, replacements, currentIndex + 1))
- doc->undo(&cursor);
- else
- shiftAffectedReplacements(op.fileName, replacements, currentIndex + 1);
+ shiftAffectedReplacements(operationsForFile.front()->fileName,
+ replacements,
+ firstOperationIndex + 1);
}
QTextDocument *FixitsRefactoringFile::document(const QString &filePath) const