aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/qmlprojectmanager
diff options
context:
space:
mode:
authorTim Jenssen <tim.jenssen@qt.io>2019-01-18 20:38:48 +0100
committerTim Jenssen <tim.jenssen@qt.io>2019-01-23 14:35:54 +0000
commit8d9ac8d94b60576f1a86ca116e5d5ae72cfc9b6e (patch)
tree9adf61fe11408cad3e393123e9ff3e9515628e41 /src/plugins/qmlprojectmanager
parent37c009e195e08cc4557053e98d599ec3067e2da2 (diff)
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 <eike.ziller@qt.io> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Diffstat (limited to 'src/plugins/qmlprojectmanager')
-rw-r--r--src/plugins/qmlprojectmanager/qmlproject.cpp6
-rw-r--r--src/plugins/qmlprojectmanager/qmlproject.h1
-rw-r--r--src/plugins/qmlprojectmanager/qmlprojectnodes.cpp48
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 <coreplugin/idocument.h>
#include <coreplugin/fileiconprovider.h>
+#include <coreplugin/documentmanager.h>
+#include <coreplugin/editormanager/documentmodel.h>
+#include <coreplugin/editormanager/ieditor.h>
#include <projectexplorer/projectexplorer.h>
-
+#include <texteditor/textdocument.h>
#include <utils/algorithm.h>
+#include <utils/textfileformat.h>
+
+#include <QRegularExpression>
+#include <QTextCodec>
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<Core::IEditor *> editors = Core::DocumentModel::editorsForFilePath(qmlProjectFilePath);
+ TextEditor::TextDocument *document = nullptr;
+ if (!editors.isEmpty()) {
+ document = qobject_cast<TextEditor::TextDocument*>(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;
}