From 5aeeb7448817b7a036c7352c8c736d7e39f89db1 Mon Sep 17 00:00:00 2001 From: Yasser Grimes Date: Fri, 5 Jan 2024 20:37:58 +0200 Subject: [PATCH] McuSupport: Support prioritizing import paths Some QtQuick import paths in QtMCUs contain incomplete modules and Used for other purposes but it shadows the correct import path With this patch it will be possible to reshuffle the import by any import provider Fixes: QTCREATORBUG-29681 Change-Id: I5a17df6ca2587ae88643b1ca08f9e84998d51cc2 Reviewed-by: Reviewed-by: hjk --- src/libs/qmljs/qmljsinterpreter.h | 5 ++++ src/libs/qmljs/qmljslink.cpp | 1 + .../mcusupport/mcusupportimportprovider.cpp | 25 +++++++++++++++++++ .../mcusupport/mcusupportimportprovider.h | 3 +++ 4 files changed, 34 insertions(+) diff --git a/src/libs/qmljs/qmljsinterpreter.h b/src/libs/qmljs/qmljsinterpreter.h index 8ca9f4ec26c..9ea0f450218 100644 --- a/src/libs/qmljs/qmljsinterpreter.h +++ b/src/libs/qmljs/qmljsinterpreter.h @@ -1125,6 +1125,11 @@ public: [[maybe_unused]] const Document *context, [[maybe_unused]] ValueOwner *valueOwner, [[maybe_unused]] Snapshot *snapshot) {} + virtual Utils::FilePaths prioritizeImportPaths([[maybe_unused]] const Document *context, + const Utils::FilePaths &importPaths) + { + return importPaths; + } }; } // namespace QmlJS diff --git a/src/libs/qmljs/qmljslink.cpp b/src/libs/qmljs/qmljslink.cpp index b837037b7c8..97c00e1c8d9 100644 --- a/src/libs/qmljs/qmljslink.cpp +++ b/src/libs/qmljs/qmljslink.cpp @@ -219,6 +219,7 @@ Context::ImportsPerDocument LinkPrivate::linkImports() document.data(), m_valueOwner, &m_snapshot); + m_importPaths = provider->prioritizeImportPaths(document.data(), m_importPaths); } populateImportedTypes(imports, document); diff --git a/src/plugins/mcusupport/mcusupportimportprovider.cpp b/src/plugins/mcusupport/mcusupportimportprovider.cpp index b78baf8bd94..f83dec6cb70 100644 --- a/src/plugins/mcusupport/mcusupportimportprovider.cpp +++ b/src/plugins/mcusupport/mcusupportimportprovider.cpp @@ -119,6 +119,30 @@ void McuSupportImportProvider::loadBuiltins(ImportsPerDocument *importsPerDocume import.info = ImportInfo::moduleImport("qul", {1, 0}, QString()); getInterfacesImport(context->fileName(), importsPerDocument, import, valueOwner, snapshot); imports->append(import); +} + +FilePaths McuSupportImportProvider::prioritizeImportPaths(const Document *context, + const FilePaths &importPaths) +{ + if (!context) + return importPaths; + const std::optional cmakeFilesPathOpt = getTargetBuildFolder(context->fileName()); + if (!cmakeFilesPathOpt) + return importPaths; + FilePaths ret; + // qmltocpp uses an incomplete QtQuick folder present in the build folder + // to avoid taking precedence over the correct qul_install/include/*/StyleDefault + // move the import path to be last + std::copy_if(importPaths.cbegin(), + importPaths.cend(), + std::back_inserter(ret), + [cmakeFilesPathOpt](const FilePath &path) { return path != *cmakeFilesPathOpt; }); + + // nothing was removed + if (ret.size() == importPaths.size()) + return importPaths; + ret.push_back(*cmakeFilesPathOpt); + return ret; }; void McuSupportImportProvider::getInterfacesImport(const FilePath &path, @@ -156,6 +180,7 @@ std::optional McuSupportImportProvider::getFileModule(const FilePath & const FilePath &inputFile) const { const auto doc = QJsonDocument::fromJson(inputFile.fileContents().value_or("")); + if (!doc.isObject()) return {}; diff --git a/src/plugins/mcusupport/mcusupportimportprovider.h b/src/plugins/mcusupport/mcusupportimportprovider.h index 829fa711505..5ed198989be 100644 --- a/src/plugins/mcusupport/mcusupportimportprovider.h +++ b/src/plugins/mcusupport/mcusupportimportprovider.h @@ -29,6 +29,9 @@ public: ValueOwner *valueOwner, Snapshot *snapshot) override; + virtual Utils::FilePaths prioritizeImportPaths(const Document *context, + const Utils::FilePaths &importPaths) override; + // Add to the interfaces needed for a document // path: opened qml document // importsPerDocument: imports available in the document (considered imported)