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