forked from qt-creator/qt-creator
QmlDesigner: Refactor import handling in ChangeImportsVisitor
- Introduced `isEqual` function to encapsulate import comparison logic. - Added `versions` method to `Import` class to return version as an array. - Utilized `<ranges>` and `CoreUtils::toDefaultInitializedArray` for cleaner, allocation-free version parsing. - Updated `ChangeImportsVisitor::equals` to use `isEqual` and the new `versions` method. Change-Id: I5693c1750831b3db3b52e5707aa69b1d068ecb27 Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
This commit is contained in:
@@ -61,23 +61,27 @@ bool ChangeImportsVisitor::remove(QmlJS::AST::UiProgram *ast, const Import &impo
|
||||
return didRewriting();
|
||||
}
|
||||
|
||||
namespace {
|
||||
bool isEqual(QmlJS::AST::UiImport *ast, const Import &import)
|
||||
{
|
||||
if (import.isLibraryImport())
|
||||
return toString(ast->importUri) == import.url();
|
||||
else if (import.isFileImport())
|
||||
return ast->fileName == import.file();
|
||||
|
||||
return false;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
bool ChangeImportsVisitor::equals(QmlJS::AST::UiImport *ast, const Import &import)
|
||||
{
|
||||
bool equal = false;
|
||||
if (import.isLibraryImport())
|
||||
equal = toString(ast->importUri) == import.url();
|
||||
else if (import.isFileImport())
|
||||
equal = ast->fileName == import.file();
|
||||
|
||||
if (equal && ast->version) {
|
||||
const QStringList versions = import.version().split('.');
|
||||
if (versions.size() >= 1 && versions[0].toInt() == ast->version->majorVersion) {
|
||||
if (versions.size() >= 2)
|
||||
equal = versions[1].toInt() == ast->version->minorVersion;
|
||||
else
|
||||
equal = ast->version->minorVersion == 0;
|
||||
if (isEqual(ast, import)) {
|
||||
if (ast->version) {
|
||||
return std::array{ast->version->majorVersion, ast->version->minorVersion}
|
||||
== import.versions();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
return equal;
|
||||
return false;
|
||||
}
|
||||
|
@@ -41,6 +41,8 @@ public:
|
||||
const QString &alias() const { return m_alias; }
|
||||
const QStringList &importPaths() const { return m_importPathList; }
|
||||
|
||||
std::array<int, 2> versions() const;
|
||||
|
||||
QString toString(bool skipAlias = false, bool skipVersion = false) const;
|
||||
QString toImportString() const;
|
||||
|
||||
|
@@ -3,14 +3,20 @@
|
||||
|
||||
#include "import.h"
|
||||
|
||||
#include <qmldesignerutils/designeralgorithm.h>
|
||||
#include <qmldesignerutils/version.h>
|
||||
|
||||
#include <QHash>
|
||||
|
||||
#include <QStringView>
|
||||
#include <QVarLengthArray>
|
||||
|
||||
#include <ranges>
|
||||
|
||||
namespace QmlDesigner {
|
||||
|
||||
using namespace Qt::StringLiterals;
|
||||
|
||||
Import Import::createLibraryImport(const QString &url, const QString &version, const QString &alias, const QStringList &importPaths)
|
||||
{
|
||||
return Import(url, version, alias, importPaths, Type::Library);
|
||||
@@ -31,6 +37,24 @@ bool Import::hasVersion() const
|
||||
return !m_version.isEmpty() && m_version != "-1.-1";
|
||||
}
|
||||
|
||||
std::array<int, 2> Import::versions() const
|
||||
{
|
||||
QStringView version = m_version;
|
||||
|
||||
#if defined(__GNUC__) && !defined(__clang__) && (__GNUC__ < 12)
|
||||
auto toInteger = [](auto &&entry) {
|
||||
QVarLengthArray<QChar, 12> string;
|
||||
std::ranges::copy(entry, std::back_inserter(string));
|
||||
return QStringView{string}.toInt();
|
||||
};
|
||||
#else
|
||||
auto toInteger = [](QStringView entry) { return entry.toInt(); };
|
||||
#endif
|
||||
|
||||
return CoreUtils::toDefaultInitializedArray<int, 2>(version | std::views::split('.'_L1)
|
||||
| std::views::transform(toInteger));
|
||||
}
|
||||
|
||||
QString Import::toImportString() const
|
||||
{
|
||||
QString result = QStringLiteral("import ");
|
||||
|
@@ -52,4 +52,14 @@ constexpr auto to(View &&view, std::integral auto reserve)
|
||||
return container;
|
||||
}
|
||||
|
||||
template<typename Type, std::size_t size, std::ranges::view View>
|
||||
constexpr auto toDefaultInitializedArray(View &&view)
|
||||
{
|
||||
std::array<Type, size> container{};
|
||||
|
||||
std::ranges::copy(view | std::views::take(size), container.begin());
|
||||
|
||||
return container;
|
||||
}
|
||||
|
||||
} // namespace QmlDesigner::CoreUtils
|
||||
|
Reference in New Issue
Block a user