diff options
Diffstat (limited to 'src/qmlcompiler')
| -rw-r--r-- | src/qmlcompiler/qqmljsimporter_p.h | 6 | ||||
| -rw-r--r-- | src/qmlcompiler/qqmljsutils.cpp | 44 | ||||
| -rw-r--r-- | src/qmlcompiler/qqmljsutils_p.h | 4 |
3 files changed, 53 insertions, 1 deletions
diff --git a/src/qmlcompiler/qqmljsimporter_p.h b/src/qmlcompiler/qqmljsimporter_p.h index 45e527b967..6aec963586 100644 --- a/src/qmlcompiler/qqmljsimporter_p.h +++ b/src/qmlcompiler/qqmljsimporter_p.h @@ -34,9 +34,12 @@ public: QQmlJSImporter(const QStringList &importPaths, QQmlJSResourceFileMapper *mapper, bool useOptionalImports = false); - QQmlJSResourceFileMapper *resourceFileMapper() { return m_mapper; } + QQmlJSResourceFileMapper *resourceFileMapper() const { return m_mapper; } void setResourceFileMapper(QQmlJSResourceFileMapper *mapper) { m_mapper = mapper; } + QQmlJSResourceFileMapper *metaDataMapper() const { return m_metaDataMapper; } + void setMetaDataMapper(QQmlJSResourceFileMapper *mapper) { m_metaDataMapper = mapper; } + ImportedTypes importBuiltins(); void importQmldirs(const QStringList &qmltypesFiles); @@ -143,6 +146,7 @@ private: QList<QQmlJS::DiagnosticMessage> m_warnings; AvailableTypes m_builtins; QQmlJSResourceFileMapper *m_mapper = nullptr; + QQmlJSResourceFileMapper *m_metaDataMapper = nullptr; bool m_useOptionalImports; ImportVisitorCreator m_createImportVisitor = nullptr; diff --git a/src/qmlcompiler/qqmljsutils.cpp b/src/qmlcompiler/qqmljsutils.cpp index c07c0b6845..548c86eb67 100644 --- a/src/qmlcompiler/qqmljsutils.cpp +++ b/src/qmlcompiler/qqmljsutils.cpp @@ -146,3 +146,47 @@ std::optional<FixSuggestion> QQmlJSUtils::didYouMean(const QString &userInput, return {}; } } + +/*! \internal + + Returns a corresponding source directory path for \a buildDirectoryPath + Returns empty string on error +*/ +std::variant<QString, QQmlJS::DiagnosticMessage> +QQmlJSUtils::sourceDirectoryPath(const QQmlJSImporter *importer, const QString &buildDirectoryPath) +{ + const auto makeError = [](const QString &msg) { + return QQmlJS::DiagnosticMessage { msg, QtWarningMsg, QQmlJS::SourceLocation() }; + }; + + if (!importer->metaDataMapper()) + return makeError(u"QQmlJSImporter::metaDataMapper() is nullptr"_s); + + // for now, meta data contains just a single entry + QQmlJSResourceFileMapper::Filter matchAll { QString(), QStringList(), + QQmlJSResourceFileMapper::Directory + | QQmlJSResourceFileMapper::Recurse }; + QQmlJSResourceFileMapper::Entry entry = importer->metaDataMapper()->entry(matchAll); + if (!entry.isValid()) + return makeError(u"Failed to find meta data entry in QQmlJSImporter::metaDataMapper()"_s); + if (!buildDirectoryPath.startsWith(entry.filePath)) // assume source directory path already + return makeError(u"The module output directory does not match the build directory path"_s); + + QString qrcPath = buildDirectoryPath; + qrcPath.remove(0, entry.filePath.size()); + qrcPath.prepend(entry.resourcePath); + qrcPath.remove(0, 1); // remove extra "/" + + const QStringList sourceDirPaths = importer->resourceFileMapper()->filePaths( + QQmlJSResourceFileMapper::resourceFileFilter(qrcPath)); + if (sourceDirPaths.size() != 1) { + const QString matchedPaths = + sourceDirPaths.isEmpty() ? u"<none>"_s : sourceDirPaths.join(u", "); + return makeError( + QStringLiteral("QRC path %1 (deduced from %2) has unexpected number of mappings " + "(%3). File paths that matched:\n%4") + .arg(qrcPath, buildDirectoryPath, QString::number(sourceDirPaths.size()), + matchedPaths)); + } + return sourceDirPaths[0]; +} diff --git a/src/qmlcompiler/qqmljsutils_p.h b/src/qmlcompiler/qqmljsutils_p.h index 3698d3861b..ab3dbd9745 100644 --- a/src/qmlcompiler/qqmljsutils_p.h +++ b/src/qmlcompiler/qqmljsutils_p.h @@ -29,6 +29,7 @@ #include <optional> #include <functional> #include <type_traits> +#include <variant> QT_BEGIN_NAMESPACE @@ -333,6 +334,9 @@ struct Q_QMLCOMPILER_PRIVATE_EXPORT QQmlJSUtils static std::optional<FixSuggestion> didYouMean(const QString &userInput, QStringList candidates, QQmlJS::SourceLocation location); + + static std::variant<QString, QQmlJS::DiagnosticMessage> + sourceDirectoryPath(const QQmlJSImporter *importer, const QString &buildDirectoryPath); }; QT_END_NAMESPACE |
