1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
|
/*************************************************************************************
* Copyright (C) 2013 by Andrea Scarpino <scarpino@kde.org> *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA *
*************************************************************************************/
#include "usebuilder.h"
#include "expressionvisitor.h"
#include "helper.h"
#include "parsesession.h"
using namespace KDevelop;
UseBuilder::UseBuilder(ParseSession* session)
: UseBuilderBase()
{
m_session = session;
m_nodesThatOpenedContexts.push(nullptr); // One push here, a thousand isEmpty() calls avoided later
}
bool UseBuilder::preVisit(QmlJS::AST::Node* node)
{
DUContext* ctx = contextFromNode(node);
if (ctx && currentContext() != ctx) {
openContext(ctx);
m_nodesThatOpenedContexts.push(node);
}
return true;
}
void UseBuilder::postVisit(QmlJS::AST::Node* node)
{
if (m_nodesThatOpenedContexts.top() == node) {
closeContext();
m_nodesThatOpenedContexts.pop();
}
}
bool UseBuilder::visit(QmlJS::AST::FieldMemberExpression* node)
{
useForExpression(node, m_session->locationToRange(node->identifierToken));
return UseBuilderBase::visit(node);
}
bool UseBuilder::visit(QmlJS::AST::IdentifierExpression* node)
{
useForExpression(node);
return UseBuilderBase::visit(node);
}
bool UseBuilder::visit(QmlJS::AST::UiQualifiedId* node)
{
useForExpression(node);
return false;
}
bool UseBuilder::visit(QmlJS::AST::UiImport* node)
{
Q_UNUSED(node);
return false; // Don't highlight the identifiers that appear in import statements
}
bool UseBuilder::visit(QmlJS::AST::UiScriptBinding* node)
{
QString propertyName = node->qualifiedId->name.toString();
if (propertyName == QLatin1String("name") ||
propertyName == QLatin1String("type") ||
propertyName == QLatin1String("exports") ||
propertyName == QLatin1String("prototype")) {
// Ignore plugin.qmltypes-specific property names. They appear a huge
// number of time and are never declared anywhere.
return false;
}
return true;
}
bool UseBuilder::visit(QmlJS::AST::UiPublicMember* node)
{
// node->memberType can contain a type name (if node is a property), use it
DeclarationPointer decl = QmlJS::getDeclaration(
QualifiedIdentifier(node->memberTypeName().toString()),
currentContext()
);
newUse(
m_session->locationToRange(node->typeToken),
decl
);
return true;
}
void UseBuilder::useForExpression(QmlJS::AST::Node* node, const KDevelop::RangeInRevision &range)
{
// ExpressionVisitor can find the type and corresponding declaration of many
// kinds of expressions (identifiers, field members, special identifiers like
// this or parent, etc).
ExpressionVisitor visitor(currentContext());
node->accept(&visitor);
if (visitor.lastDeclaration()) {
newUse(
range.isValid() ? range : m_session->locationsToRange(
node->firstSourceLocation(),
node->lastSourceLocation()
),
visitor.lastDeclaration()
);
}
}
|