From 562a619721a3c2f8065b21450d780bc336405dad Mon Sep 17 00:00:00 2001 From: Christian Kamm Date: Thu, 17 Mar 2011 14:43:08 +0100 Subject: QmlJS: Fix find usages if id and property name conflict. Task-number: QTCREATORBUG-4097 --- src/plugins/qmljseditor/qmljsfindreferences.cpp | 83 +++++++++++++++++-------- 1 file changed, 57 insertions(+), 26 deletions(-) (limited to 'src/plugins/qmljseditor') diff --git a/src/plugins/qmljseditor/qmljsfindreferences.cpp b/src/plugins/qmljseditor/qmljsfindreferences.cpp index 7197666a8f..069edae3d4 100644 --- a/src/plugins/qmljseditor/qmljsfindreferences.cpp +++ b/src/plugins/qmljseditor/qmljsfindreferences.cpp @@ -363,18 +363,29 @@ private: class FindTargetExpression: protected Visitor { public: - FindTargetExpression(Document::Ptr doc) - : _doc(doc) + FindTargetExpression(Document::Ptr doc, Context *context) + : _doc(doc), _context(context) { } - QPair operator()(quint32 offset) + void operator()(quint32 offset) { - _result = qMakePair((Node *)0, QString()); + _name = QString::null; + _scope = 0; + _objectNode = 0; _offset = offset; if (_doc) Node::accept(_doc->ast(), this); - return _result; + } + + QString name() const + { return _name; } + + const ObjectValue *scope() + { + if (!_scope) + _context->lookup(_name, &_scope); + return _scope; } protected: @@ -398,15 +409,15 @@ protected: virtual bool visit(IdentifierExpression *node) { if (containsOffset(node->identifierToken)) - _result.second = node->name->asString(); + _name = node->name->asString(); return true; } virtual bool visit(FieldMemberExpression *node) { if (containsOffset(node->identifierToken)) { - _result.first = node->base; - _result.second = node->name->asString(); + setScope(node->base); + _name = node->name->asString(); return false; } return true; @@ -424,13 +435,29 @@ protected: virtual bool visit(UiObjectBinding *node) { - return !checkBindingName(node->qualifiedId); + if (!checkBindingName(node->qualifiedId)) { + Node *oldObjectNode = _objectNode; + _objectNode = node; + accept(node->initializer); + _objectNode = oldObjectNode; + } + return false; + } + + virtual bool visit(UiObjectDefinition *node) + { + Node *oldObjectNode = _objectNode; + _objectNode = node; + accept(node->initializer); + _objectNode = oldObjectNode; + return false; } virtual bool visit(UiPublicMember *node) { if (containsOffset(node->identifierToken)) { - _result.second = node->name->asString(); + _scope = _doc->bind()->findQmlObject(_objectNode); + _name = node->name->asString(); return false; } return true; @@ -444,7 +471,7 @@ protected: virtual bool visit(FunctionExpression *node) { if (containsOffset(node->identifierToken)) { - _result.second = node->name->asString(); + _name = node->name->asString(); return false; } return true; @@ -453,7 +480,7 @@ protected: virtual bool visit(VariableDeclaration *node) { if (containsOffset(node->identifierToken)) { - _result.second = node->name->asString(); + _name = node->name->asString(); return false; } return true; @@ -473,14 +500,26 @@ private: bool checkBindingName(UiQualifiedId *id) { if (id && id->name && !id->next && containsOffset(id->identifierToken)) { - _result.second = id->name->asString(); + _scope = _doc->bind()->findQmlObject(_objectNode); + _name = id->name->asString(); return true; } return false; } - QPair _result; + void setScope(Node *node) + { + Evaluate evaluate(_context); + const Value *v = evaluate(node); + if (v) + _scope = v->asObjectValue(); + } + + QString _name; + const ObjectValue *_scope; + Node *_objectNode; Document::Ptr _doc; + Context *_context; quint32 _offset; }; @@ -593,21 +632,13 @@ static void find_helper(QFutureInterface &future, ScopeAstPath astPath(doc); builder.push(astPath(offset)); - FindTargetExpression findTarget(doc); - QPair target = findTarget(offset); - const QString &name = target.second; + FindTargetExpression findTarget(doc, &context); + findTarget(offset); + const QString &name = findTarget.name(); if (name.isEmpty()) return; - const ObjectValue *scope = 0; - if (target.first) { - Evaluate evaluate(&context); - const Value *v = evaluate(target.first); - if (v) - scope = v->asObjectValue(); - } else { - context.lookup(name, &scope); - } + const ObjectValue *scope = findTarget.scope(); if (!scope) return; scope->lookupMember(name, &context, &scope); -- cgit v1.2.1