Skip to content
Snippets Groups Projects
Commit fe1411cd authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Use the existing text editor when refactoring text.

parent debe1808
No related branches found
No related tags found
No related merge requests found
......@@ -36,6 +36,7 @@
#include <extensionsystem/pluginmanager.h>
#include <utils/filesearch.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
#include <ASTVisitor.h>
......@@ -574,6 +575,23 @@ void CppFindReferences::findAll_helper(Symbol *symbol)
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,
const QList<Find::SearchResultItem> &items)
{
......@@ -585,41 +603,48 @@ void CppFindReferences::onReplaceButtonClicked(const QString &text,
foreach (const Find::SearchResultItem &item, items)
changes[item.fileName].append(item);
Core::EditorManager *editorManager = Core::EditorManager::instance();
QHashIterator<QString, QList<Find::SearchResultItem> > it(changes);
while (it.hasNext()) {
it.next();
const QString fileName = it.key();
QFile file(fileName);
if (file.open(QFile::ReadOnly)) {
QTextStream stream(&file);
// ### set the encoding
const QString plainText = stream.readAll();
file.close();
QTextDocument doc;
doc.setPlainText(plainText);
QList<QTextCursor> cursors;
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);
}
const QList<Find::SearchResultItem> items = it.value();
foreach (QTextCursor tc, cursors)
tc.insertText(text);
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 newFile(fileName);
if (newFile.open(QFile::WriteOnly)) {
QTextStream stream(&newFile);
if (file.open(QFile::ReadOnly)) {
QTextStream stream(&file);
// ### set the encoding
stream << doc.toPlainText();
const QString plainText = stream.readAll();
file.close();
QTextDocument doc;
doc.setPlainText(plainText);
applyChanges(&doc, text, items);
QFile newFile(fileName);
if (newFile.open(QFile::WriteOnly)) {
QTextStream stream(&newFile);
// ### set the encoding
stream << doc.toPlainText();
}
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment