summaryrefslogtreecommitdiff
path: root/src/plugins/cpptools/cppmodelmanager.cpp
diff options
context:
space:
mode:
authorDavid Schulz <david.schulz@digia.com>2014-09-24 13:42:17 +0200
committerDavid Schulz <david.schulz@digia.com>2014-10-02 15:09:20 +0200
commit78c707760eb0c55d78cb1e2df2c63065dd032295 (patch)
treee9fbbf4701d4b47b774b013008ab2a05cbd41aa4 /src/plugins/cpptools/cppmodelmanager.cpp
parentb210294c86ed89a4765d8f40439dec9fde9ada36 (diff)
downloadqt-creator-78c707760eb0c55d78cb1e2df2c63065dd032295.tar.gz
Cpp: Adjust includes when files are renamed.
Change-Id: Ie6aaaa5d99ba3823d9d42331f45b2dcab397e1cd Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
Diffstat (limited to 'src/plugins/cpptools/cppmodelmanager.cpp')
-rw-r--r--src/plugins/cpptools/cppmodelmanager.cpp33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp
index dad4a80738..a808cd5de3 100644
--- a/src/plugins/cpptools/cppmodelmanager.cpp
+++ b/src/plugins/cpptools/cppmodelmanager.cpp
@@ -36,11 +36,13 @@
#include "cppfindreferences.h"
#include "cppindexingsupport.h"
#include "cppmodelmanagersupportinternal.h"
+#include "cpprefactoringchanges.h"
#include "cppsourceprocessor.h"
#include "cpptoolsconstants.h"
#include "cpptoolsplugin.h"
#include "editordocumenthandle.h"
+#include <coreplugin/documentmanager.h>
#include <coreplugin/icore.h>
#include <coreplugin/progressmanager/progressmanager.h>
#include <projectexplorer/projectexplorer.h>
@@ -297,6 +299,8 @@ CppModelManager::CppModelManager(QObject *parent)
this, SLOT(onAboutToLoadSession()));
connect(sessionManager, SIGNAL(aboutToUnloadSession(QString)),
this, SLOT(onAboutToUnloadSession()));
+ connect(Core::DocumentManager::instance(), &Core::DocumentManager::allDocumentsRenamed,
+ this, &CppModelManager::renameIncludes);
connect(Core::ICore::instance(), SIGNAL(coreAboutToClose()),
this, SLOT(onCoreAboutToClose()));
@@ -862,6 +866,35 @@ void CppModelManager::onAboutToUnloadSession()
} while (0);
}
+void CppModelManager::renameIncludes(const QString &oldFileName, const QString &newFileName)
+{
+ if (oldFileName.isEmpty() || newFileName.isEmpty())
+ return;
+
+ const QFileInfo oldFileInfo(oldFileName);
+ const QFileInfo newFileInfo(newFileName);
+
+ // We just want to handle renamings so return when the file was actually moved.
+ if (oldFileInfo.absoluteDir() != newFileInfo.absoluteDir())
+ return;
+
+ const TextEditor::RefactoringChanges changes;
+
+ foreach (Snapshot::IncludeLocation loc, snapshot().includeLocationsOfDocument(oldFileName)) {
+ TextEditor::RefactoringFilePtr file = changes.file(loc.first->fileName());
+ const QTextBlock &block = file->document()->findBlockByLineNumber(loc.second - 1);
+ const int replaceStart = block.text().indexOf(oldFileInfo.fileName());
+ if (replaceStart > -1) {
+ Utils::ChangeSet changeSet;
+ changeSet.replace(block.position() + replaceStart,
+ block.position() + replaceStart + oldFileInfo.fileName().length(),
+ newFileInfo.fileName());
+ file->setChangeSet(changeSet);
+ file->apply();
+ }
+ }
+}
+
void CppModelManager::onCoreAboutToClose()
{
d->m_enableGC = false;