forked from qt-creator/qt-creator
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>
This commit is contained in:
@@ -180,6 +180,12 @@ QString QmlProject::mainFile() const
|
|||||||
return QString();
|
return QString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void QmlProject::setMainFile(const QString &mainFilePath)
|
||||||
|
{
|
||||||
|
if (m_projectItem)
|
||||||
|
m_projectItem.data()->setMainFile(mainFilePath);
|
||||||
|
}
|
||||||
|
|
||||||
Utils::FileName QmlProject::targetDirectory(const Target *target) const
|
Utils::FileName QmlProject::targetDirectory(const Target *target) const
|
||||||
{
|
{
|
||||||
if (DeviceTypeKitInformation::deviceTypeId(target->kit())
|
if (DeviceTypeKitInformation::deviceTypeId(target->kit())
|
||||||
|
@@ -64,6 +64,7 @@ public:
|
|||||||
|
|
||||||
Utils::FileName canonicalProjectDir() const;
|
Utils::FileName canonicalProjectDir() const;
|
||||||
QString mainFile() const;
|
QString mainFile() const;
|
||||||
|
void setMainFile(const QString &mainFilePath);
|
||||||
Utils::FileName targetDirectory(const ProjectExplorer::Target *target) const;
|
Utils::FileName targetDirectory(const ProjectExplorer::Target *target) const;
|
||||||
Utils::FileName targetFile(const Utils::FileName &sourceFile,
|
Utils::FileName targetFile(const Utils::FileName &sourceFile,
|
||||||
const ProjectExplorer::Target *target) const;
|
const ProjectExplorer::Target *target) const;
|
||||||
|
@@ -28,9 +28,16 @@
|
|||||||
|
|
||||||
#include <coreplugin/idocument.h>
|
#include <coreplugin/idocument.h>
|
||||||
#include <coreplugin/fileiconprovider.h>
|
#include <coreplugin/fileiconprovider.h>
|
||||||
|
#include <coreplugin/documentmanager.h>
|
||||||
|
#include <coreplugin/editormanager/documentmodel.h>
|
||||||
|
#include <coreplugin/editormanager/ieditor.h>
|
||||||
#include <projectexplorer/projectexplorer.h>
|
#include <projectexplorer/projectexplorer.h>
|
||||||
|
#include <texteditor/textdocument.h>
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
|
#include <utils/textfileformat.h>
|
||||||
|
|
||||||
|
#include <QRegularExpression>
|
||||||
|
#include <QTextCodec>
|
||||||
|
|
||||||
using namespace ProjectExplorer;
|
using namespace ProjectExplorer;
|
||||||
|
|
||||||
@@ -76,8 +83,45 @@ bool QmlProjectNode::deleteFiles(const QStringList & /*filePaths*/)
|
|||||||
return true;
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user