Commit dc64b72c authored by Denis Steckelmacher's avatar Denis Steckelmacher

Handle the "parent" identifier in QML files

REVIEW: 118700
parent 3bdf0da4
......@@ -202,6 +202,17 @@ void QmlCompletionTest::testContainsDeclaration_data()
" %INVOKE\n"
" }\n"
"}" << "%CURSOR" << "prop" << true;
// QML parent
QTest::newRow("qml_parent") <<
"Item {\n"
" id: a\n"
" property var prop\n"
" Item {\n"
" id: b\n"
" %INVOKE\n"
" }\n"
"}\n" << "onTest: parent.%CURSOR" << "prop" << true;
}
void QmlCompletionTest::testDoesNotContainDeclaration()
......
......@@ -196,6 +196,24 @@ void ExpressionVisitor::encounter(IntegralType::CommonIntegralTypes type)
void ExpressionVisitor::encounter(const QString& declaration, KDevelop::DUContext* context)
{
DUChainReadLocker lock;
// Special-case some special names (this, parent, etc)
if (declaration == QLatin1String("parent") &&
ParseSession::guessLanguageFromSuffix(m_context->topContext()->url().str()) == QmlJS::Language::Qml) {
DUContext* parent = m_context->parentContext();
if (parent &&
parent->owner() &&
parent->owner()->abstractType()) {
// Parent is difficult to resolve, but most of the time the enclosing QML
// component can be used as parent.
encounterLvalue(DeclarationPointer(m_context->parentContext()->owner()));
return;
}
}
const QualifiedIdentifier name(declaration);
DeclarationPointer dec = QmlJS::getDeclarationOrSignal(name, context ? context : m_context);
......
......@@ -52,7 +52,7 @@ bool isSorted(const QList<QmlJS::AST::SourceLocation>& locations)
return true;
}
QmlJS::Language::Enum guessLanguageFromSuffix(const QString& path)
QmlJS::Language::Enum ParseSession::guessLanguageFromSuffix(const QString& path)
{
if (path.endsWith(".js")) {
return QmlJS::Language::JavaScript;
......
......@@ -47,6 +47,11 @@ public:
*/
static KDevelop::IndexedString languageString();
/**
* @return the QML/JS language corresponding to a file suffix
*/
static QmlJS::Language::Enum guessLanguageFromSuffix(const QString& path);
/**
* Parse the given @p contents.
*
......
......@@ -12,7 +12,7 @@ Text {
/**
* "type" : { "toString" : "string" },
* "useCount" : 1
* "useCount" : 2
*/
property string text: "Hello"
......@@ -78,6 +78,10 @@ Text {
*/
id: bar
}
onLoad: {
console.log(parent.text);
}
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment