diff --git a/src/plugins/qmldesigner/libs/designercore/include/model.h b/src/plugins/qmldesigner/libs/designercore/include/model.h index ee80d1b5483..ba73b3ab44c 100644 --- a/src/plugins/qmldesigner/libs/designercore/include/model.h +++ b/src/plugins/qmldesigner/libs/designercore/include/model.h @@ -209,6 +209,7 @@ public: #endif QList itemLibraryEntries() const; + QList directoryImportsItemLibraryEntries() const; void attachView(AbstractView *view); void detachView(AbstractView *view, ViewNotification emitDetachNotify = NotifyView); diff --git a/src/plugins/qmldesigner/libs/designercore/model/model.cpp b/src/plugins/qmldesigner/libs/designercore/model/model.cpp index 28b847fdfa4..9f385c2a904 100644 --- a/src/plugins/qmldesigner/libs/designercore/model/model.cpp +++ b/src/plugins/qmldesigner/libs/designercore/model/model.cpp @@ -2819,6 +2819,17 @@ QList Model::itemLibraryEntries() const #endif } +QList Model::directoryImportsItemLibraryEntries() const +{ +#ifdef QDS_USE_PROJECTSTORAGE + using namespace Storage::Info; + return toItemLibraryEntries(*d->pathCache, + d->projectStorage->directoryImportsItemLibraryEntries(d->m_sourceId)); +#else + return {}; +#endif +} + NodeMetaInfo Model::qtQuickTimelineKeyframeGroupMetaInfo() const { if constexpr (useProjectStorage()) { diff --git a/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.cpp b/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.cpp index f7dfa4bcaac..d5395905d85 100644 --- a/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.cpp +++ b/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.cpp @@ -870,7 +870,7 @@ struct ProjectStorage::Statements " USING(moduleId) " " WHERE di.sourceId=?)", database}; - mutable Sqlite::ReadStatement<4, 2> selectLocalFileItemLibraryEntriesBySourceIdStatement{ + mutable Sqlite::ReadStatement<4, 2> selectDirectoryImportsItemLibraryEntriesBySourceIdStatement{ "SELECT typeId, etn.name, m.name, t.sourceId " "FROM documentImports AS di " " JOIN exportedTypeNames AS etn USING(moduleId) " @@ -1877,16 +1877,16 @@ Storage::Info::ItemLibraryEntries ProjectStorage::itemLibraryEntries(SourceId so using Storage::Info::ItemLibraryProperties; Storage::Info::ItemLibraryEntries entries; - auto typeAnnotationCallback = [&](TypeId typeId, - Utils::SmallStringView typeName, - Utils::SmallStringView name, - Utils::SmallStringView iconPath, - Utils::SmallStringView category, - Utils::SmallStringView import, - Utils::SmallStringView toolTip, - Utils::SmallStringView properties, - Utils::SmallStringView extraFilePaths, - Utils::SmallStringView templatePath) { + auto callback = [&](TypeId typeId, + Utils::SmallStringView typeName, + Utils::SmallStringView name, + Utils::SmallStringView iconPath, + Utils::SmallStringView category, + Utils::SmallStringView import, + Utils::SmallStringView toolTip, + Utils::SmallStringView properties, + Utils::SmallStringView extraFilePaths, + Utils::SmallStringView templatePath) { auto &last = entries.emplace_back( typeId, typeName, name, iconPath, category, import, toolTip, templatePath); if (properties.size()) @@ -1895,23 +1895,7 @@ Storage::Info::ItemLibraryEntries ProjectStorage::itemLibraryEntries(SourceId so s->selectItemLibraryExtraFilePathsStatement.readTo(last.extraFilePaths, extraFilePaths); }; - s->selectItemLibraryEntriesBySourceIdStatement.readCallbackWithTransaction(typeAnnotationCallback, - sourceId); - - auto fileComponentCallback = [&](TypeId typeId, - Utils::SmallStringView typeName, - Utils::SmallStringView import, - SourceId componentSourceId) { - if (!isCapitalLetter(typeName.front())) - return; - - auto &last = entries.emplace_back(typeId, typeName, typeName, "My Components", import); - last.moduleKind = Storage::ModuleKind::PathLibrary; - last.componentSourceId = componentSourceId; - }; - - s->selectLocalFileItemLibraryEntriesBySourceIdStatement.readCallbackWithTransaction( - fileComponentCallback, sourceId, Storage::ModuleKind::PathLibrary); + s->selectItemLibraryEntriesBySourceIdStatement.readCallbackWithTransaction(callback, sourceId); tracer.end(keyValue("item library entries", entries)); @@ -1951,6 +1935,36 @@ Storage::Info::ItemLibraryEntries ProjectStorage::allItemLibraryEntries() const return entries; } +Storage::Info::ItemLibraryEntries ProjectStorage::directoryImportsItemLibraryEntries(SourceId sourceId) const +{ + using NanotraceHR::keyValue; + NanotraceHR::Tracer tracer{"get directory import item library entries", + projectStorageCategory(), + keyValue("source id", sourceId)}; + + using Storage::Info::ItemLibraryProperties; + Storage::Info::ItemLibraryEntries entries; + + auto callback = [&](TypeId typeId, + Utils::SmallStringView typeName, + Utils::SmallStringView import, + SourceId componentSourceId) { + if (!isCapitalLetter(typeName.front())) + return; + + auto &last = entries.emplace_back(typeId, typeName, typeName, "My Components", import); + last.moduleKind = Storage::ModuleKind::PathLibrary; + last.componentSourceId = componentSourceId; + }; + + s->selectDirectoryImportsItemLibraryEntriesBySourceIdStatement + .readCallbackWithTransaction(callback, sourceId, Storage::ModuleKind::PathLibrary); + + tracer.end(keyValue("item library entries", entries)); + + return entries; +} + std::vector ProjectStorage::signalDeclarationNames(TypeId typeId) const { using NanotraceHR::keyValue; diff --git a/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.h b/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.h index 41368208a69..7751338403e 100644 --- a/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.h +++ b/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorage.h @@ -110,12 +110,10 @@ public: SmallSourceContextIds<64> typeAnnotationDirectoryIds() const override; Storage::Info::ItemLibraryEntries itemLibraryEntries(TypeId typeId) const override; - Storage::Info::ItemLibraryEntries itemLibraryEntries(ImportId importId) const; - Storage::Info::ItemLibraryEntries itemLibraryEntries(SourceId sourceId) const override; - Storage::Info::ItemLibraryEntries allItemLibraryEntries() const override; + Storage::Info::ItemLibraryEntries directoryImportsItemLibraryEntries(SourceId sourceId) const override; std::vector signalDeclarationNames(TypeId typeId) const override; diff --git a/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorageinterface.h b/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorageinterface.h index cc04ca19f18..f2aa26f44b9 100644 --- a/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorageinterface.h +++ b/src/plugins/qmldesigner/libs/designercore/projectstorage/projectstorageinterface.h @@ -65,6 +65,7 @@ public: virtual Storage::Info::ItemLibraryEntries itemLibraryEntries(TypeId typeId) const = 0; virtual Storage::Info::ItemLibraryEntries itemLibraryEntries(SourceId sourceId) const = 0; virtual Storage::Info::ItemLibraryEntries allItemLibraryEntries() const = 0; + virtual Storage::Info::ItemLibraryEntries directoryImportsItemLibraryEntries(SourceId sourceId) const = 0; virtual std::vector<::Utils::SmallString> signalDeclarationNames(TypeId typeId) const = 0; virtual std::vector<::Utils::SmallString> functionDeclarationNames(TypeId typeId) const = 0; virtual std::optional<::Utils::SmallString> diff --git a/tests/unit/tests/mocks/projectstoragemock.cpp b/tests/unit/tests/mocks/projectstoragemock.cpp index 150930db1ab..9d5b89f7743 100644 --- a/tests/unit/tests/mocks/projectstoragemock.cpp +++ b/tests/unit/tests/mocks/projectstoragemock.cpp @@ -243,6 +243,13 @@ void ProjectStorageMock::setItemLibraryEntries( ON_CALL(*this, itemLibraryEntries(TypedEq(sourceId))).WillByDefault(Return(entries)); } +void ProjectStorageMock::setDirectoryImportsItemLibraryEntries( + QmlDesigner::SourceId sourceId, const QmlDesigner::Storage::Info::ItemLibraryEntries &entries) +{ + ON_CALL(*this, directoryImportsItemLibraryEntries(TypedEq(sourceId))) + .WillByDefault(Return(entries)); +} + namespace { void addBaseProperties(TypeId typeId, const QmlDesigner::SmallTypeIds<16> &baseTypeIds, diff --git a/tests/unit/tests/mocks/projectstoragemock.h b/tests/unit/tests/mocks/projectstoragemock.h index 1d64b0cc1e8..ac1bbfb2a4a 100644 --- a/tests/unit/tests/mocks/projectstoragemock.h +++ b/tests/unit/tests/mocks/projectstoragemock.h @@ -114,6 +114,8 @@ public: const QmlDesigner::Storage::Info::ItemLibraryEntries &entries); void setItemLibraryEntries(QmlDesigner::SourceId sourceId, const QmlDesigner::Storage::Info::ItemLibraryEntries &entries); + void setDirectoryImportsItemLibraryEntries( + QmlDesigner::SourceId sourceId, const QmlDesigner::Storage::Info::ItemLibraryEntries &entries); MOCK_METHOD(void, synchronize, @@ -228,6 +230,10 @@ public: allItemLibraryEntries, (), (const, override)); + MOCK_METHOD(QmlDesigner::Storage::Info::ItemLibraryEntries, + directoryImportsItemLibraryEntries, + (QmlDesigner::SourceId sourceId), + (const, override)); MOCK_METHOD(std::vector<::Utils::SmallString>, signalDeclarationNames, (QmlDesigner::TypeId typeId), diff --git a/tests/unit/tests/unittests/model/model-test.cpp b/tests/unit/tests/unittests/model/model-test.cpp index dff3a7904bd..8207ee4f245 100644 --- a/tests/unit/tests/unittests/model/model-test.cpp +++ b/tests/unit/tests/unittests/model/model-test.cpp @@ -1333,6 +1333,37 @@ TEST_F(Model_TypeAnnotation, item_library_entries) ElementsAre(u"/extra/file/path")))); } +TEST_F(Model_TypeAnnotation, directory_imports_item_library_entries) +{ + using namespace Qt::StringLiterals; + QmlDesigner::Storage::Info::ItemLibraryEntries storageEntries{{itemTypeId, + "Item", + "Item", + "/path/to/icon", + "basic category", + "QtQuick", + "It's a item", + "/path/to/template"}}; + storageEntries.front().properties.emplace_back("x", "double", Sqlite::ValueView::create(1)); + storageEntries.front().extraFilePaths.emplace_back("/extra/file/path"); + projectStorageMock.setDirectoryImportsItemLibraryEntries(pathCacheMock.sourceId, storageEntries); + + auto entries = model.directoryImportsItemLibraryEntries(); + + ASSERT_THAT(entries, + ElementsAre( + IsItemLibraryEntry(itemTypeId, + "Item", + u"Item", + u"/path/to/icon", + u"basic category", + u"QtQuick", + u"It's a item", + u"/path/to/template", + ElementsAre(IsItemLibraryProperty("x", "double"_L1, QVariant{1})), + ElementsAre(u"/extra/file/path")))); +} + class Model_ViewManagement : public Model { protected: diff --git a/tests/unit/tests/unittests/projectstorage/projectstorage-test.cpp b/tests/unit/tests/unittests/projectstorage/projectstorage-test.cpp index b948fb91ce1..79c75478bc4 100644 --- a/tests/unit/tests/unittests/projectstorage/projectstorage-test.cpp +++ b/tests/unit/tests/unittests/projectstorage/projectstorage-test.cpp @@ -8890,14 +8890,14 @@ TEST_F(ProjectStorage, get_item_library_entries_by_source_id) IsEmpty()))); } -TEST_F(ProjectStorage, get_local_file_item_library_entries_by_source_id) +TEST_F(ProjectStorage, get_directory_imports_item_library_entries_by_source_id) { auto package{createSimpleSynchronizationPackage()}; package.imports.emplace_back(pathToModuleId, Storage::Version{}, sourceId2); package.types[1].exportedTypes.emplace_back(pathToModuleId, "Object"); storage.synchronize(package); - auto entries = storage.itemLibraryEntries(sourceId2); + auto entries = storage.directoryImportsItemLibraryEntries(sourceId2); ASSERT_THAT(entries, UnorderedElementsAre(IsItemLibraryEntry(fetchTypeId(sourceId2, "QObject"),