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)