forked from qt-creator/qt-creator
Use the existing text editor when refactoring text.
This commit is contained in:
@@ -36,6 +36,7 @@
|
|||||||
#include <extensionsystem/pluginmanager.h>
|
#include <extensionsystem/pluginmanager.h>
|
||||||
#include <utils/filesearch.h>
|
#include <utils/filesearch.h>
|
||||||
#include <coreplugin/progressmanager/progressmanager.h>
|
#include <coreplugin/progressmanager/progressmanager.h>
|
||||||
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
#include <coreplugin/icore.h>
|
#include <coreplugin/icore.h>
|
||||||
|
|
||||||
#include <ASTVisitor.h>
|
#include <ASTVisitor.h>
|
||||||
@@ -574,6 +575,23 @@ void CppFindReferences::findAll_helper(Symbol *symbol)
|
|||||||
connect(progress, SIGNAL(clicked()), _resultWindow, SLOT(popup()));
|
connect(progress, SIGNAL(clicked()), _resultWindow, SLOT(popup()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void applyChanges(QTextDocument *doc, const QString &text, const QList<Find::SearchResultItem> &items)
|
||||||
|
{
|
||||||
|
QList<QTextCursor> cursors;
|
||||||
|
|
||||||
|
foreach (const Find::SearchResultItem &item, items) {
|
||||||
|
const int blockNumber = item.lineNumber - 1;
|
||||||
|
QTextCursor tc(doc->findBlockByNumber(blockNumber));
|
||||||
|
tc.setPosition(tc.position() + item.searchTermStart);
|
||||||
|
tc.setPosition(tc.position() + item.searchTermLength,
|
||||||
|
QTextCursor::KeepAnchor);
|
||||||
|
cursors.append(tc);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (QTextCursor tc, cursors)
|
||||||
|
tc.insertText(text);
|
||||||
|
}
|
||||||
|
|
||||||
void CppFindReferences::onReplaceButtonClicked(const QString &text,
|
void CppFindReferences::onReplaceButtonClicked(const QString &text,
|
||||||
const QList<Find::SearchResultItem> &items)
|
const QList<Find::SearchResultItem> &items)
|
||||||
{
|
{
|
||||||
@@ -585,11 +603,29 @@ void CppFindReferences::onReplaceButtonClicked(const QString &text,
|
|||||||
foreach (const Find::SearchResultItem &item, items)
|
foreach (const Find::SearchResultItem &item, items)
|
||||||
changes[item.fileName].append(item);
|
changes[item.fileName].append(item);
|
||||||
|
|
||||||
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
||||||
|
|
||||||
QHashIterator<QString, QList<Find::SearchResultItem> > it(changes);
|
QHashIterator<QString, QList<Find::SearchResultItem> > it(changes);
|
||||||
while (it.hasNext()) {
|
while (it.hasNext()) {
|
||||||
it.next();
|
it.next();
|
||||||
|
|
||||||
const QString fileName = it.key();
|
const QString fileName = it.key();
|
||||||
|
const QList<Find::SearchResultItem> items = it.value();
|
||||||
|
|
||||||
|
const QList<Core::IEditor *> editors = editorManager->editorsForFileName(fileName);
|
||||||
|
TextEditor::BaseTextEditor *textEditor = 0;
|
||||||
|
foreach (Core::IEditor *editor, editors) {
|
||||||
|
textEditor = qobject_cast<TextEditor::BaseTextEditor *>(editor->widget());
|
||||||
|
if (textEditor != 0)
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (textEditor != 0) {
|
||||||
|
QTextCursor tc = textEditor->textCursor();
|
||||||
|
tc.beginEditBlock();
|
||||||
|
applyChanges(textEditor->document(), text, items);
|
||||||
|
tc.endEditBlock();
|
||||||
|
} else {
|
||||||
QFile file(fileName);
|
QFile file(fileName);
|
||||||
|
|
||||||
if (file.open(QFile::ReadOnly)) {
|
if (file.open(QFile::ReadOnly)) {
|
||||||
@@ -601,19 +637,7 @@ void CppFindReferences::onReplaceButtonClicked(const QString &text,
|
|||||||
QTextDocument doc;
|
QTextDocument doc;
|
||||||
doc.setPlainText(plainText);
|
doc.setPlainText(plainText);
|
||||||
|
|
||||||
QList<QTextCursor> cursors;
|
applyChanges(&doc, text, items);
|
||||||
const QList<Find::SearchResultItem> items = it.value();
|
|
||||||
foreach (const Find::SearchResultItem &item, items) {
|
|
||||||
const int blockNumber = item.lineNumber - 1;
|
|
||||||
QTextCursor tc(doc.findBlockByNumber(blockNumber));
|
|
||||||
tc.setPosition(tc.position() + item.searchTermStart);
|
|
||||||
tc.setPosition(tc.position() + item.searchTermLength,
|
|
||||||
QTextCursor::KeepAnchor);
|
|
||||||
cursors.append(tc);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (QTextCursor tc, cursors)
|
|
||||||
tc.insertText(text);
|
|
||||||
|
|
||||||
QFile newFile(fileName);
|
QFile newFile(fileName);
|
||||||
if (newFile.open(QFile::WriteOnly)) {
|
if (newFile.open(QFile::WriteOnly)) {
|
||||||
@@ -623,6 +647,7 @@ void CppFindReferences::onReplaceButtonClicked(const QString &text,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const QStringList fileNames = changes.keys();
|
const QStringList fileNames = changes.keys();
|
||||||
_modelManager->updateSourceFiles(fileNames);
|
_modelManager->updateSourceFiles(fileNames);
|
||||||
|
Reference in New Issue
Block a user