aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Teske <daniel.teske@theqtcompany.com>2015-10-01 14:54:31 +0200
committerNikolai Kosjar <nikolai.kosjar@theqtcompany.com>2015-10-06 13:17:55 +0000
commit307061032cf8c5389d91093cc5937d9cc351772a (patch)
tree683bd807d0d3795a7ec1e2ad5c110a17b154b23f
parent5eb8fe0306bc3cc5dd69d8159530826cf40eb934 (diff)
Fix changing "#include" lines after file renaming
Didn't work if there was any folded text before the #include. Change-Id: I8f16205f06bfaa8b8541401a9ebd5995c15b2227 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@theqtcompany.com> Reviewed-by: Daniel Teske <daniel.teske@theqtcompany.com>
-rw-r--r--src/plugins/cpptools/cppmodelmanager.cpp2
-rw-r--r--src/plugins/cpptools/cppmodelmanager_test.cpp65
-rw-r--r--src/plugins/cpptools/cpptoolsplugin.h2
-rw-r--r--tests/cppmodelmanager/testdata_project1/main.cpp30
4 files changed, 96 insertions, 3 deletions
diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp
index 133bbae3cf..e15d1f659c 100644
--- a/src/plugins/cpptools/cppmodelmanager.cpp
+++ b/src/plugins/cpptools/cppmodelmanager.cpp
@@ -1113,7 +1113,7 @@ void CppModelManager::renameIncludes(const QString &oldFileName, const QString &
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 QTextBlock &block = file->document()->findBlockByNumber(loc.second - 1);
const int replaceStart = block.text().indexOf(oldFileInfo.fileName());
if (replaceStart > -1) {
Utils::ChangeSet changeSet;
diff --git a/src/plugins/cpptools/cppmodelmanager_test.cpp b/src/plugins/cpptools/cppmodelmanager_test.cpp
index 1015329879..a3c1954a7b 100644
--- a/src/plugins/cpptools/cppmodelmanager_test.cpp
+++ b/src/plugins/cpptools/cppmodelmanager_test.cpp
@@ -36,6 +36,7 @@
#include "editordocumenthandle.h"
#include "modelmanagertesthelper.h"
+#include <coreplugin/documentmanager.h>
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/fileutils.h>
#include <coreplugin/testdatadir.h>
@@ -43,6 +44,7 @@
#include <projectexplorer/session.h>
#include <cplusplus/LookupContext.h>
+#include <utils/executeondestruction.h>
#include <utils/hostosinfo.h>
#include <QDebug>
@@ -1107,6 +1109,69 @@ void CppToolsPlugin::test_modelmanager_renameIncludes()
QCOMPARE(snapshot.allIncludesForDocument(sourceFile), QSet<QString>() << newHeader);
}
+void CppToolsPlugin::test_modelmanager_renameIncludesInEditor()
+{
+ struct ModelManagerGCHelper {
+ ~ModelManagerGCHelper() { CppModelManager::instance()->GC(); }
+ } GCHelper;
+ Q_UNUSED(GCHelper); // do not warn about being unused
+
+ TemporaryDir tmpDir;
+ QVERIFY(tmpDir.isValid());
+
+ const QDir workingDir(tmpDir.path());
+ const QStringList fileNames = QStringList() << _("foo.h") << _("foo.cpp") << _("main.cpp");
+ const QString oldHeader(workingDir.filePath(_("foo.h")));
+ const QString newHeader(workingDir.filePath(_("bar.h")));
+ const QString mainFile(workingDir.filePath(_("main.cpp")));
+ CppModelManager *modelManager = CppModelManager::instance();
+ const MyTestDataDir testDir(_("testdata_project1"));
+
+ ModelManagerTestHelper helper;
+ helper.resetRefreshedSourceFiles();
+
+ // Copy test files to a temporary directory
+ QSet<QString> sourceFiles;
+ foreach (const QString &fileName, fileNames) {
+ const QString &file = workingDir.filePath(fileName);
+ QVERIFY(QFile::copy(testDir.file(fileName), file));
+ // Saving source file names for the model manager update,
+ // so we can update just the relevant files.
+ if (ProjectFile::classify(file) == ProjectFile::CXXSource)
+ sourceFiles.insert(file);
+ }
+
+ // Update the c++ model manager and check for the old includes
+ modelManager->updateSourceFiles(sourceFiles).waitForFinished();
+ QCoreApplication::processEvents();
+ CPlusPlus::Snapshot snapshot = modelManager->snapshot();
+ foreach (const QString &sourceFile, sourceFiles)
+ QCOMPARE(snapshot.allIncludesForDocument(sourceFile), QSet<QString>() << oldHeader);
+
+ // Open a file in the editor
+ QCOMPARE(Core::DocumentModel::openedDocuments().size(), 0);
+ Core::IEditor *editor = Core::EditorManager::openEditor(mainFile);
+ QVERIFY(editor);
+ EditorCloser editorCloser(editor);
+ Utils::ExecuteOnDestruction saveAllFiles([](){
+ Core::DocumentManager::saveAllModifiedDocumentsSilently();
+ });
+ QCOMPARE(Core::DocumentModel::openedDocuments().size(), 1);
+ QVERIFY(modelManager->isCppEditor(editor));
+ QVERIFY(modelManager->workingCopy().contains(mainFile));
+
+ // Renaming the header
+ QVERIFY(Core::FileUtils::renameFile(oldHeader, newHeader));
+
+ // Update the c++ model manager again and check for the new includes
+ TestCase::waitForProcessedEditorDocument(mainFile);
+ modelManager->updateSourceFiles(sourceFiles).waitForFinished();
+ QCoreApplication::processEvents();
+ snapshot = modelManager->snapshot();
+ foreach (const QString &sourceFile, sourceFiles)
+ QCOMPARE(snapshot.allIncludesForDocument(sourceFile), QSet<QString>() << newHeader);
+}
+
void CppToolsPlugin::test_modelmanager_documentsAndRevisions()
{
TestCase helper;
diff --git a/src/plugins/cpptools/cpptoolsplugin.h b/src/plugins/cpptools/cpptoolsplugin.h
index cd5ceecce6..fd70b9e905 100644
--- a/src/plugins/cpptools/cpptoolsplugin.h
+++ b/src/plugins/cpptools/cpptoolsplugin.h
@@ -79,7 +79,6 @@ public:
QSharedPointer<CppCodeModelSettings> codeModelSettings() const;
static StringTable &stringTable();
-
public slots:
void switchHeaderSource();
void switchHeaderSourceInNextSplit();
@@ -158,6 +157,7 @@ private slots:
void test_modelmanager_updateEditorsAfterProjectUpdate();
void test_modelmanager_precompiled_headers();
void test_modelmanager_renameIncludes();
+ void test_modelmanager_renameIncludesInEditor();
void test_modelmanager_documentsAndRevisions();
void test_cpplocatorfilters_CppLocatorFilter();
diff --git a/tests/cppmodelmanager/testdata_project1/main.cpp b/tests/cppmodelmanager/testdata_project1/main.cpp
index eca708ea3f..b077eee81b 100644
--- a/tests/cppmodelmanager/testdata_project1/main.cpp
+++ b/tests/cppmodelmanager/testdata_project1/main.cpp
@@ -1,4 +1,32 @@
-// Copyright header
+/****************************************************************************
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+**
+****************************************************************************/
#include "foo.h"