forked from qt-creator/qt-creator
		
	Implement string based search & replace in multiple files.
This uses the same UI as "Rename Symbol". Moves the actual rename implementation to a static method in BaseFileFind and uses it for rename symbol and search & replace. Moves the signal notification for the code model from VCSManager to the more general FileManager. Note that as for rename symbol, there's no undo yet. Task-number: QTCREATORBUG-73
This commit is contained in:
		@@ -32,6 +32,7 @@
 | 
			
		||||
#include "cpptoolsconstants.h"
 | 
			
		||||
 | 
			
		||||
#include <texteditor/basetexteditor.h>
 | 
			
		||||
#include <texteditor/basefilefind.h>
 | 
			
		||||
#include <find/searchresultwindow.h>
 | 
			
		||||
#include <extensionsystem/pluginmanager.h>
 | 
			
		||||
#include <utils/filesearch.h>
 | 
			
		||||
@@ -294,63 +295,11 @@ void CppFindReferences::onReplaceButtonClicked(const QString &text,
 | 
			
		||||
{
 | 
			
		||||
    Core::EditorManager::instance()->hideEditorInfoBar(QLatin1String("CppEditor.Rename"));
 | 
			
		||||
 | 
			
		||||
    if (text.isEmpty())
 | 
			
		||||
        return;
 | 
			
		||||
 | 
			
		||||
    QHash<QString, QList<Find::SearchResultItem> > changes;
 | 
			
		||||
 | 
			
		||||
    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();
 | 
			
		||||
        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);
 | 
			
		||||
 | 
			
		||||
            if (file.open(QFile::ReadOnly)) {
 | 
			
		||||
                QTextStream stream(&file);
 | 
			
		||||
                // ### set the encoding
 | 
			
		||||
                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();
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    const QStringList fileNames = TextEditor::BaseFileFind::replaceAll(text, items);
 | 
			
		||||
    if (!fileNames.isEmpty()) {
 | 
			
		||||
        _modelManager->updateSourceFiles(fileNames);
 | 
			
		||||
        _resultWindow->hide();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    const QStringList fileNames = changes.keys();
 | 
			
		||||
    _modelManager->updateSourceFiles(fileNames);
 | 
			
		||||
    _resultWindow->hide();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void CppFindReferences::displayResults(int first, int last)
 | 
			
		||||
 
 | 
			
		||||
@@ -48,6 +48,7 @@
 | 
			
		||||
#include <coreplugin/editormanager/editormanager.h>
 | 
			
		||||
#include <coreplugin/progressmanager/progressmanager.h>
 | 
			
		||||
#include <coreplugin/vcsmanager.h>
 | 
			
		||||
#include <coreplugin/filemanager.h>
 | 
			
		||||
#include <cppeditor/cppeditorconstants.h>
 | 
			
		||||
 | 
			
		||||
#include <QtCore/QtConcurrentRun>
 | 
			
		||||
@@ -99,10 +100,11 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
 | 
			
		||||
    // Objects
 | 
			
		||||
    m_modelManager = new CppModelManager(this);
 | 
			
		||||
    Core::VCSManager *vcsManager = core->vcsManager();
 | 
			
		||||
    Core::FileManager *fileManager = core->fileManager();
 | 
			
		||||
    connect(vcsManager, SIGNAL(repositoryChanged(QString)),
 | 
			
		||||
            m_modelManager, SLOT(updateModifiedSourceFiles()));
 | 
			
		||||
    connect(vcsManager, SIGNAL(filesChanged(QStringList)),
 | 
			
		||||
            m_modelManager, SLOT(updateModifiedSourceFiles()));
 | 
			
		||||
    connect(fileManager, SIGNAL(filesChangedInternally(QStringList)),
 | 
			
		||||
            m_modelManager, SLOT(updateSourceFiles(QStringList)));
 | 
			
		||||
    addAutoReleasedObject(m_modelManager);
 | 
			
		||||
 | 
			
		||||
    m_completion = new CppCodeCompletion(m_modelManager);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user