From 07dfd7c0b30e4a7b795b1bedf811c905f92934a2 Mon Sep 17 00:00:00 2001 From: Christian Kandeler Date: Tue, 14 Jun 2022 09:57:32 +0200 Subject: [PATCH] Wizards: Bump QtQuick import version in file wizards Use 2.15 as default, plus a little heuristic to use the same version as other files in the project. Fixes: QTCREATORBUG-27614 Change-Id: Ic84db5da97a9f35a2ad0e57fd47b75fb32a0b7f8 Reviewed-by: Qt CI Bot Reviewed-by: Reviewed-by: Alessandro Portale --- .../wizards/autotest/files/tst_qml.tmpl | 2 +- .../wizards/files/qtquick2/file.qml.tpl | 2 +- .../wizards/files/qtquick2/wizard.json | 7 +++-- src/plugins/coreplugin/corejsextensions.cpp | 31 +++++++++++++++++++ src/plugins/coreplugin/corejsextensions.h | 3 ++ 5 files changed, 41 insertions(+), 4 deletions(-) diff --git a/share/qtcreator/templates/wizards/autotest/files/tst_qml.tmpl b/share/qtcreator/templates/wizards/autotest/files/tst_qml.tmpl index 5d3da10b700..e87c3dee134 100644 --- a/share/qtcreator/templates/wizards/autotest/files/tst_qml.tmpl +++ b/share/qtcreator/templates/wizards/autotest/files/tst_qml.tmpl @@ -1,4 +1,4 @@ -import QtQuick 2.0 +import QtQuick 2.15 import QtTest 1.0 TestCase { diff --git a/share/qtcreator/templates/wizards/files/qtquick2/file.qml.tpl b/share/qtcreator/templates/wizards/files/qtquick2/file.qml.tpl index 9c36e13c5bf..53df26f056d 100644 --- a/share/qtcreator/templates/wizards/files/qtquick2/file.qml.tpl +++ b/share/qtcreator/templates/wizards/files/qtquick2/file.qml.tpl @@ -1,4 +1,4 @@ -import QtQuick 2.0 +import QtQuick %{QtQuickVersion} Item { diff --git a/share/qtcreator/templates/wizards/files/qtquick2/wizard.json b/share/qtcreator/templates/wizards/files/qtquick2/wizard.json index 0fa070cf934..bcb8ca994fe 100644 --- a/share/qtcreator/templates/wizards/files/qtquick2/wizard.json +++ b/share/qtcreator/templates/wizards/files/qtquick2/wizard.json @@ -3,13 +3,16 @@ "supportedProjectTypes": [ ], "id": "Q.Qml.2", "category": "R.Qt", - "trDescription": "Creates a QML file with boilerplate code, starting with \"import QtQuick 2.0\".", + "trDescription": "Creates a QML file with boilerplate code, starting with \"import QtQuick\".", "trDisplayName": "QML File (Qt Quick 2)", "trDisplayCategory": "Qt", "iconText": "qml", "enabled": "%{JS: value('Plugins').indexOf('QmlJSEditor') >= 0}", - "options": { "key": "DefaultSuffix", "value": "%{JS: Util.preferredSuffix('text/x-qml')}" }, + "options": [ + {"key": "DefaultSuffix", "value": "%{JS: Util.preferredSuffix('text/x-qml')}"}, + {"key": "QtQuickVersion", "value": "%{JS: Util.qtQuickVersion(value('TargetPath'))}"} + ], "pages" : [ diff --git a/src/plugins/coreplugin/corejsextensions.cpp b/src/plugins/coreplugin/corejsextensions.cpp index b73534b25b5..a88de1020c4 100644 --- a/src/plugins/coreplugin/corejsextensions.cpp +++ b/src/plugins/coreplugin/corejsextensions.cpp @@ -32,8 +32,10 @@ #include #include +#include #include #include +#include namespace Core { namespace Internal { @@ -165,5 +167,34 @@ QString UtilsJsExtension::asciify(const QString &input) const return result; } +QString UtilsJsExtension::qtQuickVersion(const QString &filePath) const +{ + QDirIterator dirIt(Utils::FilePath::fromString(filePath).parentDir().path(), {"*.qml"}, + QDir::Files, QDirIterator::Subdirectories); + while (dirIt.hasNext()) { + Utils::FileReader reader; + if (!reader.fetch(Utils::FilePath::fromString(dirIt.next()))) + continue; + const QString data = QString::fromUtf8(reader.data()); + static const QString importString("import QtQuick"); + const int importIndex = data.indexOf(importString); + if (importIndex == -1) + continue; + const int versionIndex = importIndex + importString.length(); + const int newLineIndex = data.indexOf('\n', versionIndex); + if (newLineIndex == -1) + continue; + const QString versionString = data.mid(versionIndex, + newLineIndex - versionIndex).simplified(); + if (versionString.isEmpty()) + return {}; + const auto version = QVersionNumber::fromString(versionString); + if (version.isNull()) + return {}; + return version.toString(); + } + return QLatin1String("2.15"); +} + } // namespace Internal } // namespace Core diff --git a/src/plugins/coreplugin/corejsextensions.h b/src/plugins/coreplugin/corejsextensions.h index bbdc8762512..8a668cbd161 100644 --- a/src/plugins/coreplugin/corejsextensions.h +++ b/src/plugins/coreplugin/corejsextensions.h @@ -76,6 +76,9 @@ public: // Generate a ascii-only string: Q_INVOKABLE QString asciify(const QString &input) const; + + // Heuristic to find out which QtQuick import version to use for the given file. + Q_INVOKABLE QString qtQuickVersion(const QString &filePath) const; }; } // namespace Internal