aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSami Shalayel <sami.shalayel@qt.io>2023-12-19 12:05:42 +0100
committerSami Shalayel <sami.shalayel@qt.io>2024-01-16 08:06:54 +0100
commitef635e7bcf292e2525c9ed90d1a8370fa8188361 (patch)
treed617e1c7259876ff2c295c0744040d2190046a5d /src
parent9182ef97fb720bbbadfd312bf62d476c33b5b866 (diff)
qmldom: make type a FieldMemberExpression
This commit prepares the Dom to be able to provide correct completions on binding scripts ending with a `.`. In addition to saving qualified types as a string, like `QQ.Item` for example, save them also as a FieldMemberExpression. Do this for QmlObjects, like `QQ.Item {}` for example, and type annotations, like `function f(): QQ.Item {}` for example. This will allow qmlls to suggest completions after `root.` in cases like ``` x: root. SomeQualifiedModule.Item {} ``` for example, or after `QQ.` in cases like ``` (x as QQ.Item) ``` for example. The latter magically makes the asCompletion test work (which was previously QEXPECT_FAIL'd). Prior to this commit, the Dom did not contain enough information to know where exactly the completion gets requested inside of `root.SomeQualifiedModule.Item` and therefore could not suggest any meaningful suggestion. Basically reuses a44f21f19462cc79f82080404515c4322b7728ee to model the nameIdentifiers, a FieldMemberExpression that contains the current type of the QmlObject. Adapt existing tests propertyDefinitionBinding and ignoreNonRelatedTypesForpropertyDefinitionBinding to the changes in the Dom: the qml object type in a binding, for example `Item` in `myBinding: Item {}`, is now part of the QmlObject. It was part of the Binding prior to this commit. Adapt the qmllsutils method to the change in structure. Pick-to: 6.7 Task-number: QTBUG-119839 Change-Id: Ie7bc7692f731a01467392dc1dffdf7e67c4d7c46 Reviewed-by: Ulf Hermann <ulf.hermann@qt.io>
Diffstat (limited to 'src')
-rw-r--r--src/qmldom/qqmldomastcreator.cpp96
-rw-r--r--src/qmldom/qqmldomelements.cpp10
-rw-r--r--src/qmldom/qqmldomelements_p.h4
-rw-r--r--src/qmldom/qqmldompath_p.h2
-rw-r--r--src/qmlls/qqmllsutils.cpp57
5 files changed, 128 insertions, 41 deletions
diff --git a/src/qmldom/qqmldomastcreator.cpp b/src/qmldom/qqmldomastcreator.cpp
index a748038671..76c9acc5f9 100644
--- a/src/qmldom/qqmldomastcreator.cpp
+++ b/src/qmldom/qqmldomastcreator.cpp
@@ -119,6 +119,46 @@ SourceLocation combineLocations(Node *n)
return combineLocations(n->firstSourceLocation(), n->lastSourceLocation());
}
+static ScriptElementVariant wrapIntoFieldMemberExpression(const ScriptElementVariant &left,
+ const SourceLocation &dotToken,
+ const ScriptElementVariant &right)
+{
+ SourceLocation s1, s2;
+ left.visitConst([&s1](auto &&el) { s1 = el->mainRegionLocation(); });
+ right.visitConst([&s2](auto &&el) { s2 = el->mainRegionLocation(); });
+
+ auto result = std::make_shared<ScriptElements::BinaryExpression>(s1, s2);
+ result->addLocation(OperatorTokenRegion, dotToken);
+ result->setOp(ScriptElements::BinaryExpression::FieldMemberAccess);
+ result->setLeft(left);
+ result->setRight(right);
+ return ScriptElementVariant::fromElement(result);
+};
+
+/*!
+ \internal
+ Creates a FieldMemberExpression if the qualified id has dots.
+*/
+static ScriptElementVariant fieldMemberExpressionForQualifiedId(AST::UiQualifiedId *qualifiedId)
+{
+ ScriptElementVariant bindable;
+ bool first = true;
+ for (auto exp = qualifiedId; exp; exp = exp->next) {
+ const SourceLocation identifierLoc = exp->identifierToken;
+ auto id = std::make_shared<ScriptElements::IdentifierExpression>(identifierLoc);
+ id->setName(exp->name);
+ if (first) {
+ first = false;
+ bindable = ScriptElementVariant::fromElement(id);
+ continue;
+ }
+ bindable = wrapIntoFieldMemberExpression(bindable, exp->dotToken,
+ ScriptElementVariant::fromElement(id));
+ }
+
+ return bindable;
+}
+
QQmlDomAstCreator::QmlStackElement &QQmlDomAstCreator::currentQmlObjectOrComponentEl(int idx)
{
Q_ASSERT_X(idx < nodeStack.size() && idx >= 0, "currentQmlObjectOrComponentEl",
@@ -751,6 +791,15 @@ bool QQmlDomAstCreator::visit(AST::UiObjectDefinition *el)
el->qualifiedTypeNameId->identifierToken);
}
Q_ASSERT_X(sPtr, className, "could not recover new scope");
+
+ if (m_enableScriptExpressions) {
+ auto qmlObjectType = makeGenericScriptElement(el->qualifiedTypeNameId, DomType::ScriptType);
+ qmlObjectType->insertChild(Fields::typeName,
+ fieldMemberExpressionForQualifiedId(el->qualifiedTypeNameId));
+ sPtr->setNameIdentifiers(
+ finalizeScriptExpression(ScriptElementVariant::fromElement(qmlObjectType),
+ sPathFromOwner.field(Fields::nameIdentifiers), rootMap));
+ }
pushEl(sPathFromOwner, *sPtr, el);
loadAnnotations(el);
return true;
@@ -814,6 +863,16 @@ bool QQmlDomAstCreator::visit(AST::UiObjectBinding *el)
QmlObject *objValue = bPtr->objectValue();
Q_ASSERT_X(objValue, className, "could not recover objectValue");
objValue->setName(toString(el->qualifiedTypeNameId));
+
+ if (m_enableScriptExpressions) {
+ auto qmlObjectType = makeGenericScriptElement(el->qualifiedTypeNameId, DomType::ScriptType);
+ qmlObjectType->insertChild(Fields::typeName,
+ fieldMemberExpressionForQualifiedId(el->qualifiedTypeNameId));
+ objValue->setNameIdentifiers(finalizeScriptExpression(
+ ScriptElementVariant::fromElement(qmlObjectType),
+ bPathFromOwner.field(Fields::value).field(Fields::nameIdentifiers), rootMap));
+ }
+
objValue->addPrototypePath(Paths::lookupTypePath(objValue->name()));
pushEl(bPathFromOwner.field(Fields::value), *objValue, el->initializer);
return true;
@@ -889,22 +948,6 @@ bool QQmlDomAstCreator::visit(AST::UiScriptBinding *el)
.withPath(pathFromOwner)));
}
} else {
- // Create FieldExpression if the bindable element has dots
- const auto reparentExp = [](const auto &left, const SourceLocation &dotToken,
- const auto &right) {
- SourceLocation s1, s2;
- left.visitConst([&s1](auto &&el) { s1 = el->mainRegionLocation(); });
-
- right.visitConst([&s2](auto &&el) { s2 = el->mainRegionLocation(); });
-
- auto result = std::make_shared<ScriptElements::BinaryExpression>(s1, s2);
- result->addLocation(OperatorTokenRegion, dotToken);
- result->setOp(ScriptElements::BinaryExpression::FieldMemberAccess);
- result->setLeft(left);
- result->setRight(right);
- return ScriptElementVariant::fromElement(result);
- };
-
pathFromOwner =
current<QmlObject>().addBinding(bindingV, AddOption::KeepExisting, &bindingPtr);
QmlStackElement &containingObjectEl = currentEl<QmlObject>();
@@ -916,19 +959,7 @@ bool QQmlDomAstCreator::visit(AST::UiScriptBinding *el)
el->qualifiedId->identifierToken);
FileLocations::addRegion(bindingFileLocation, ColonTokenRegion, el->colonToken);
- ScriptElementVariant bindable;
- bool first = true;
- for (auto exp = el->qualifiedId; exp; exp = exp->next) {
- const SourceLocation identifierLoc = exp->identifierToken;
- auto id = std::make_shared<ScriptElements::IdentifierExpression>(identifierLoc);
- id->setName(exp->name);
- if (first) {
- first = false;
- bindable = ScriptElementVariant::fromElement(id);
- continue;
- }
- bindable = reparentExp(bindable, exp->dotToken, ScriptElementVariant::fromElement(id));
- }
+ ScriptElementVariant bindable = fieldMemberExpressionForQualifiedId(el->qualifiedId);
bindingPtr->setBindingIdentifiers(finalizeScriptExpression(
bindable, pathFromOwner.field(Fields::bindingIdentifiers), rootMap));
@@ -1938,13 +1969,12 @@ void QQmlDomAstCreator::endVisit(AST::Type *exp)
auto current = makeGenericScriptElement(exp, DomType::ScriptType);
if (exp->typeArgument) {
- auto currentChild = scriptElementForQualifiedId(exp->typeArgument);
- current->insertChild(Fields::typeArgument, currentChild);
+ current->insertChild(Fields::typeArgumentName,
+ fieldMemberExpressionForQualifiedId(exp->typeArgument));
}
if (exp->typeId) {
- auto currentChild = scriptElementForQualifiedId(exp->typeId);
- current->insertChild(Fields::typeName, currentChild);
+ current->insertChild(Fields::typeName, fieldMemberExpressionForQualifiedId(exp->typeId));
}
pushScriptElement(current);
diff --git a/src/qmldom/qqmldomelements.cpp b/src/qmldom/qqmldomelements.cpp
index fe25087763..dd1cd0be7b 100644
--- a/src/qmldom/qqmldomelements.cpp
+++ b/src/qmldom/qqmldomelements.cpp
@@ -412,6 +412,11 @@ bool QmlObject::iterateBaseDirectSubpaths(const DomItem &self, DirectVisitor vis
[&self](const DomItem &) { return self.propertyInfoNames(); },
QLatin1String("PropertyInfo")));
});
+ if (m_nameIdentifiers) {
+ cont = cont && self.dvItemField(visitor, Fields::nameIdentifiers, [this, &self]() {
+ return self.subScriptElementWrapperItem(m_nameIdentifiers);
+ });
+ }
return cont;
}
@@ -505,6 +510,11 @@ DomItem QmlObject::field(const DomItem &self, QStringView name) const
[copiedSelf = self](const DomItem &) { return copiedSelf.propertyInfoNames(); },
QLatin1String("PropertyInfo")));
break;
+ case 15:
+ if (name == Fields::nameIdentifiers && m_nameIdentifiers) {
+ return self.subScriptElementWrapperItem(m_nameIdentifiers);
+ }
+ break;
case 19:
if (name == Fields::defaultPropertyName)
return self.subDataItem(PathEls::Field(Fields::defaultPropertyName),
diff --git a/src/qmldom/qqmldomelements_p.h b/src/qmldom/qqmldomelements_p.h
index e1afa0cff3..d71f97851c 100644
--- a/src/qmldom/qqmldomelements_p.h
+++ b/src/qmldom/qqmldomelements_p.h
@@ -914,6 +914,9 @@ public:
QQmlJSScope::ConstPtr semanticScope() const { return m_scope; }
void setSemanticScope(const QQmlJSScope::ConstPtr &scope) { m_scope = scope; }
+ ScriptElementVariant nameIdentifiers() const { return m_nameIdentifiers; }
+ void setNameIdentifiers(const ScriptElementVariant &name) { m_nameIdentifiers = name; }
+
private:
friend class QQmlDomAstCreator;
QString m_idStr;
@@ -927,6 +930,7 @@ private:
QList<QmlObject> m_children;
QList<QmlObject> m_annotations;
QQmlJSScope::ConstPtr m_scope;
+ ScriptElementVariant m_nameIdentifiers;
};
class Export
diff --git a/src/qmldom/qqmldompath_p.h b/src/qmldom/qqmldompath_p.h
index be85ebd408..32ee25122f 100644
--- a/src/qmldom/qqmldompath_p.h
+++ b/src/qmldom/qqmldompath_p.h
@@ -502,6 +502,7 @@ QMLDOM_FIELD(nCallbacks);
QMLDOM_FIELD(nLoaded);
QMLDOM_FIELD(nNotdone);
QMLDOM_FIELD(name);
+QMLDOM_FIELD(nameIdentifiers);
QMLDOM_FIELD(newlinesBefore);
QMLDOM_FIELD(nextComponent);
QMLDOM_FIELD(nextScope);
@@ -568,6 +569,7 @@ QMLDOM_FIELD(targetPropertyName);
QMLDOM_FIELD(text);
QMLDOM_FIELD(type);
QMLDOM_FIELD(typeArgument);
+QMLDOM_FIELD(typeArgumentName);
QMLDOM_FIELD(typeName);
QMLDOM_FIELD(types);
QMLDOM_FIELD(universe);
diff --git a/src/qmlls/qqmllsutils.cpp b/src/qmlls/qqmllsutils.cpp
index 99357e172c..8b636082e4 100644
--- a/src/qmlls/qqmllsutils.cpp
+++ b/src/qmlls/qqmllsutils.cpp
@@ -75,6 +75,42 @@ static bool isFieldMemberAccess(const DomItem &item)
/*!
\internal
+ Get the bits of a field member expression, like \c{a}, \c{b} and \c{c} for \c{a.b.c}.
+
+ stopAtChild can either be an FieldMemberExpression, a ScriptIdentifierExpression or a default
+ constructed DomItem: This exits early before processing Field::right of an
+ FieldMemberExpression stopAtChild, or before processing a ScriptIdentifierExpression stopAtChild.
+ No early exits if stopAtChild is default constructed.
+*/
+static QStringList fieldMemberExpressionBits(const DomItem &item, const DomItem &stopAtChild = {})
+{
+ const bool isAccess = isFieldMemberAccess(item);
+ const bool isExpression = isFieldMemberExpression(item);
+
+ // assume it is a non-qualified name
+ if (!isAccess && !isExpression)
+ return { item.value().toString() };
+
+ const DomItem stopMarker =
+ isFieldMemberExpression(stopAtChild) ? stopAtChild : stopAtChild.directParent();
+
+ QStringList result;
+ DomItem current =
+ isAccess ? item.directParent() : (isFieldMemberExpression(item) ? item : DomItem{});
+
+ for (; isFieldMemberExpression(current); current = current.field(Fields::right)) {
+ result << current.field(Fields::left).value().toString();
+
+ if (current == stopMarker)
+ return result;
+ }
+ result << current.value().toString();
+
+ return result;
+}
+
+/*!
+ \internal
The language server protocol calls "URI" what QML calls "URL".
According to RFC 3986, a URL is a special case of URI that not only
identifies a resource but also shows how to access it.
@@ -352,10 +388,7 @@ QList<QQmlLSUtilsItemLocation> QQmlLSUtils::itemsFromTextLocation(const DomItem
DomItem QQmlLSUtils::baseObject(const DomItem &object)
{
- if (!object.as<QmlObject>())
- return {};
-
- auto prototypes = object.field(QQmlJS::Dom::Fields::prototypes);
+ auto prototypes = object.qmlObject().field(QQmlJS::Dom::Fields::prototypes);
switch (prototypes.indexes()) {
case 0:
return {};
@@ -449,8 +482,15 @@ std::optional<QQmlLSUtilsLocation> QQmlLSUtils::findTypeDefinitionOf(const DomIt
[](DomType k, const DomItem &) { return k == DomType::ScriptType; },
FilterUpOptions::ReturnOuter)) {
- const QString name = type.field(Fields::typeName).value().toString();
- typeDefinition = object.path(Paths::lookupTypePath(name));
+ const QString name = fieldMemberExpressionBits(type.field(Fields::typeName)).join(u'.');
+ if (type.directParent().internalKind() == DomType::QmlObject) {
+ // is the type name of a QmlObject, like Item in `Item {...}`
+ typeDefinition = baseObject(type.directParent());
+ } else {
+ // is a type annotation, like Item in `function f(x: Item) { ... }`
+ typeDefinition = object.path(Paths::lookupTypePath(name));
+ }
+
break;
}
if (DomItem id = object.filterUp(
@@ -1358,7 +1398,7 @@ DomItem QQmlLSUtils::sourceLocationToDomItem(const DomItem &file,
static std::optional<QQmlLSUtilsLocation>
findMethodDefinitionOf(const DomItem &file, QQmlJS::SourceLocation location, const QString &name)
{
- DomItem owner = QQmlLSUtils::sourceLocationToDomItem(file, location);
+ DomItem owner = QQmlLSUtils::sourceLocationToDomItem(file, location).qmlObject();
DomItem method = owner.field(Fields::methods).key(name).index(0);
auto fileLocation = FileLocations::treeOf(method);
if (!fileLocation)
@@ -1380,7 +1420,8 @@ static std::optional<QQmlLSUtilsLocation>
findPropertyDefinitionOf(const DomItem &file, QQmlJS::SourceLocation propertyDefinitionLocation,
const QString &name)
{
- DomItem propertyOwner = QQmlLSUtils::sourceLocationToDomItem(file, propertyDefinitionLocation);
+ DomItem propertyOwner =
+ QQmlLSUtils::sourceLocationToDomItem(file, propertyDefinitionLocation).qmlObject();
DomItem propertyDefinition = propertyOwner.field(Fields::propertyDefs).key(name).index(0);
auto fileLocation = FileLocations::treeOf(propertyDefinition);
if (!fileLocation)