From 8d9ac8d94b60576f1a86ca116e5d5ae72cfc9b6e Mon Sep 17 00:00:00 2001 From: Tim Jenssen Date: Fri, 18 Jan 2019 20:38:48 +0100 Subject: [PATCH] QmlProject: update mainFile after renaming it Set mainFile at the QmlProject and replaces the string inside the .qmlproject file. If that file is open and modified just save that, it is not nice but qmake projects haves the same at the moment. Task-number: QTCREATORBUG-10629 Change-Id: Ia3916644bacfc65862802e3dc5361edd50951d35 Reviewed-by: Eike Ziller Reviewed-by: Thomas Hartmann --- src/plugins/qmlprojectmanager/qmlproject.cpp | 6 +++ src/plugins/qmlprojectmanager/qmlproject.h | 1 + .../qmlprojectmanager/qmlprojectnodes.cpp | 48 ++++++++++++++++++- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/src/plugins/qmlprojectmanager/qmlproject.cpp b/src/plugins/qmlprojectmanager/qmlproject.cpp index a9a883d63f4..e5168626170 100644 --- a/src/plugins/qmlprojectmanager/qmlproject.cpp +++ b/src/plugins/qmlprojectmanager/qmlproject.cpp @@ -180,6 +180,12 @@ QString QmlProject::mainFile() const return QString(); } +void QmlProject::setMainFile(const QString &mainFilePath) +{ + if (m_projectItem) + m_projectItem.data()->setMainFile(mainFilePath); +} + Utils::FileName QmlProject::targetDirectory(const Target *target) const { if (DeviceTypeKitInformation::deviceTypeId(target->kit()) diff --git a/src/plugins/qmlprojectmanager/qmlproject.h b/src/plugins/qmlprojectmanager/qmlproject.h index f3be29287c5..8f7bf30c044 100644 --- a/src/plugins/qmlprojectmanager/qmlproject.h +++ b/src/plugins/qmlprojectmanager/qmlproject.h @@ -64,6 +64,7 @@ public: Utils::FileName canonicalProjectDir() const; QString mainFile() const; + void setMainFile(const QString &mainFilePath); Utils::FileName targetDirectory(const ProjectExplorer::Target *target) const; Utils::FileName targetFile(const Utils::FileName &sourceFile, const ProjectExplorer::Target *target) const; diff --git a/src/plugins/qmlprojectmanager/qmlprojectnodes.cpp b/src/plugins/qmlprojectmanager/qmlprojectnodes.cpp index 9fcbe3a3053..195681aebef 100644 --- a/src/plugins/qmlprojectmanager/qmlprojectnodes.cpp +++ b/src/plugins/qmlprojectmanager/qmlprojectnodes.cpp @@ -28,9 +28,16 @@ #include #include +#include +#include +#include #include - +#include #include +#include + +#include +#include using namespace ProjectExplorer; @@ -76,8 +83,45 @@ bool QmlProjectNode::deleteFiles(const QStringList & /*filePaths*/) return true; } -bool QmlProjectNode::renameFile(const QString & /*filePath*/, const QString & /*newFilePath*/) +bool QmlProjectNode::renameFile(const QString & filePath, const QString & newFilePath) { + if (filePath.endsWith(m_project->mainFile())) { + m_project->setMainFile(newFilePath); + + // make sure to change it also in the qmlproject file + const QString qmlProjectFilePath = m_project->document()->filePath().toString(); + Core::FileChangeBlocker fileChangeBlocker(qmlProjectFilePath); + const QList editors = Core::DocumentModel::editorsForFilePath(qmlProjectFilePath); + TextEditor::TextDocument *document = nullptr; + if (!editors.isEmpty()) { + document = qobject_cast(editors.first()->document()); + if (document && document->isModified()) + if (!Core::DocumentManager::saveDocument(document)) + return false; + } + + QString fileContent; + QString error; + Utils::TextFileFormat textFileFormat; + const QTextCodec *codec = QTextCodec::codecForName("UTF-8"); // qml files are defined to be utf-8 + if (Utils::TextFileFormat::readFile(qmlProjectFilePath, codec, &fileContent, &textFileFormat, &error) + != Utils::TextFileFormat::ReadSuccess) { + qWarning() << "Failed to read file" << qmlProjectFilePath << ":" << error; + } + + // find the mainFile and do the file name with brackets in a capture group and mask the . with \. + QString originalFileName = QFileInfo(filePath).fileName(); + originalFileName.replace(".", "\\."); + const QRegularExpression expression(QString("mainFile:\\s*\"(%1)\"").arg(originalFileName)); + const QRegularExpressionMatch match = expression.match(fileContent); + + fileContent.replace(match.capturedStart(1), match.capturedLength(1), QFileInfo(newFilePath).fileName()); + + if (!textFileFormat.writeFile(qmlProjectFilePath, fileContent, &error)) + qWarning() << "Failed to write file" << qmlProjectFilePath << ":" << error; + m_project->refresh(QmlProject::Everything); + } + return true; }