forked from qt-creator/qt-creator
QmlDesigner: Add singleton support to QmlDocumentParser
Task-number: QDS-13602 Change-Id: I0af54c5949b20e840c0b07f13ec213db8dc40d7e Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
@@ -30,6 +30,8 @@ using Tracer = ProjectStorageTracing::Category::TracerType;
|
|||||||
namespace QmlDom = QQmlJS::Dom;
|
namespace QmlDom = QQmlJS::Dom;
|
||||||
namespace Synchronization = Storage::Synchronization;
|
namespace Synchronization = Storage::Synchronization;
|
||||||
|
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
using QualifiedImports = std::map<QString, Storage::Import>;
|
using QualifiedImports = std::map<QString, Storage::Import>;
|
||||||
@@ -297,12 +299,21 @@ void addEnumeraton(Storage::Synchronization::Type &type, const QmlDom::Component
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Storage::TypeTraits createTypeTraits()
|
bool isSingleton(const QmlDom::QmlFile *qmlFile)
|
||||||
|
{
|
||||||
|
const auto &pragmas = qmlFile->pragmas();
|
||||||
|
|
||||||
|
return std::ranges::find(pragmas, "Singleton"_L1, &QQmlJS::Dom::Pragma::name) != pragmas.end();
|
||||||
|
}
|
||||||
|
|
||||||
|
Storage::TypeTraits createTypeTraits(const QmlDom::QmlFile *qmlFile)
|
||||||
{
|
{
|
||||||
Storage::TypeTraits traits = Storage::TypeTraitsKind::Reference;
|
Storage::TypeTraits traits = Storage::TypeTraitsKind::Reference;
|
||||||
|
|
||||||
traits.isFileComponent = true;
|
traits.isFileComponent = true;
|
||||||
|
|
||||||
|
traits.isSingleton = isSingleton(qmlFile);
|
||||||
|
|
||||||
return traits;
|
return traits;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -353,7 +364,7 @@ Storage::Synchronization::Type QmlDocumentParser::parse(const QString &sourceCon
|
|||||||
QmlDom::DomItem file = items.field(QmlDom::Fields::currentItem);
|
QmlDom::DomItem file = items.field(QmlDom::Fields::currentItem);
|
||||||
const QmlDom::QmlFile *qmlFile = file.as<QmlDom::QmlFile>();
|
const QmlDom::QmlFile *qmlFile = file.as<QmlDom::QmlFile>();
|
||||||
const auto &components = qmlFile->components();
|
const auto &components = qmlFile->components();
|
||||||
|
qmlFile->pragmas();
|
||||||
if (components.empty())
|
if (components.empty())
|
||||||
return type;
|
return type;
|
||||||
|
|
||||||
@@ -372,7 +383,7 @@ Storage::Synchronization::Type QmlDocumentParser::parse(const QString &sourceCon
|
|||||||
directoryPath,
|
directoryPath,
|
||||||
m_storage);
|
m_storage);
|
||||||
|
|
||||||
type.traits = createTypeTraits();
|
type.traits = createTypeTraits(qmlFile);
|
||||||
type.prototype = createImportedTypeName(qmlObject.name(), qualifiedImports);
|
type.prototype = createImportedTypeName(qmlObject.name(), qualifiedImports);
|
||||||
type.defaultPropertyName = qmlObject.localDefaultPropertyName();
|
type.defaultPropertyName = qmlObject.localDefaultPropertyName();
|
||||||
addImports(imports, qmlFile->imports(), sourceId, directoryPath, m_storage);
|
addImports(imports, qmlFile->imports(), sourceId, directoryPath, m_storage);
|
||||||
|
@@ -543,4 +543,25 @@ TEST_F(QmlDocumentParser, has_is_reference_trait)
|
|||||||
ASSERT_THAT(type.traits.kind, QmlDesigner::Storage::TypeTraitsKind::Reference);
|
ASSERT_THAT(type.traits.kind, QmlDesigner::Storage::TypeTraitsKind::Reference);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(QmlDocumentParser, is_singleton)
|
||||||
|
{
|
||||||
|
QString component = R"(pragma Singleton
|
||||||
|
Item{
|
||||||
|
})";
|
||||||
|
|
||||||
|
auto type = parser.parse(component, imports, qmlFileSourceId, directoryPath);
|
||||||
|
|
||||||
|
ASSERT_TRUE(type.traits.isSingleton);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(QmlDocumentParser, is_not_singleton)
|
||||||
|
{
|
||||||
|
QString component = R"(Item{
|
||||||
|
})";
|
||||||
|
|
||||||
|
auto type = parser.parse(component, imports, qmlFileSourceId, directoryPath);
|
||||||
|
|
||||||
|
ASSERT_FALSE(type.traits.isSingleton);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
Reference in New Issue
Block a user