Cpp: Adjust includes when files are renamed.

Change-Id: Ie6aaaa5d99ba3823d9d42331f45b2dcab397e1cd
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com>
This commit is contained in:
David Schulz
2014-09-24 13:42:17 +02:00
parent b210294c86
commit 78c707760e
6 changed files with 97 additions and 0 deletions

View File

@@ -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;