2010-09-24 14:05:34 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2012-01-26 18:33:46 +01:00
|
|
|
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
|
2010-09-24 14:05:34 +02:00
|
|
|
**
|
2012-07-19 12:26:56 +02:00
|
|
|
** Contact: http://www.qt-project.org/
|
2010-09-24 14:05:34 +02:00
|
|
|
**
|
|
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** This file may be used under the terms of the GNU Lesser General Public
|
|
|
|
|
** License version 2.1 as published by the Free Software Foundation and
|
|
|
|
|
** appearing in the file LICENSE.LGPL included in the packaging of this file.
|
|
|
|
|
** Please review the following information to ensure the GNU Lesser General
|
|
|
|
|
** Public License version 2.1 requirements will be met:
|
|
|
|
|
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2010-09-24 14:05:34 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
2011-04-13 08:42:33 +02:00
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
2010-12-17 16:01:08 +01:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2011-04-13 08:42:33 +02:00
|
|
|
** Other Usage
|
|
|
|
|
**
|
|
|
|
|
** Alternatively, this file may be used in accordance with the terms and
|
|
|
|
|
** conditions contained in a signed written agreement between you and Nokia.
|
|
|
|
|
**
|
2010-09-24 14:05:34 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
|
|
#include "qmljsfindreferences.h"
|
|
|
|
|
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
|
|
|
|
#include <texteditor/basefilefind.h>
|
|
|
|
|
#include <find/searchresultwindow.h>
|
|
|
|
|
#include <extensionsystem/pluginmanager.h>
|
|
|
|
|
#include <utils/filesearch.h>
|
|
|
|
|
#include <coreplugin/progressmanager/progressmanager.h>
|
|
|
|
|
#include <coreplugin/progressmanager/futureprogress.h>
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
|
|
|
|
|
#include <qmljs/qmljsmodelmanagerinterface.h>
|
|
|
|
|
#include <qmljs/qmljsbind.h>
|
|
|
|
|
#include <qmljs/qmljslink.h>
|
|
|
|
|
#include <qmljs/qmljsevaluate.h>
|
|
|
|
|
#include <qmljs/qmljsscopebuilder.h>
|
2011-05-04 11:12:45 +02:00
|
|
|
#include <qmljs/qmljsscopeastpath.h>
|
2011-07-01 13:51:53 +02:00
|
|
|
#include <qmljs/qmljscontext.h>
|
2010-09-24 14:05:34 +02:00
|
|
|
#include <qmljs/parser/qmljsastvisitor_p.h>
|
|
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2011-09-09 10:55:11 +02:00
|
|
|
#include <qmljstools/qmljsmodelmanager.h>
|
2010-09-24 14:05:34 +02:00
|
|
|
|
|
|
|
|
#include "qmljseditorconstants.h"
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QTime>
|
|
|
|
|
#include <QTimer>
|
|
|
|
|
#include <QtConcurrentRun>
|
|
|
|
|
#include <QtConcurrentMap>
|
|
|
|
|
#include <QDir>
|
|
|
|
|
#include <QApplication>
|
|
|
|
|
#include <QLabel>
|
2012-02-09 09:35:03 +01:00
|
|
|
#include <utils/runextensions.h>
|
2010-09-24 14:05:34 +02:00
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
using namespace QmlJSEditor;
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
// ### These visitors could be useful in general
|
|
|
|
|
|
|
|
|
|
class FindUsages: protected Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef QList<AST::SourceLocation> Result;
|
|
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
FindUsages(Document::Ptr doc, const ContextPtr &context)
|
2010-09-24 14:05:34 +02:00
|
|
|
: _doc(doc)
|
2011-07-12 14:55:27 +02:00
|
|
|
, _scopeChain(doc, context)
|
|
|
|
|
, _builder(&_scopeChain)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result operator()(const QString &name, const ObjectValue *scope)
|
|
|
|
|
{
|
|
|
|
|
_name = name;
|
|
|
|
|
_scope = scope;
|
|
|
|
|
_usages.clear();
|
|
|
|
|
if (_doc)
|
|
|
|
|
Node::accept(_doc->ast(), this);
|
|
|
|
|
return _usages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void accept(AST::Node *node)
|
|
|
|
|
{ AST::Node::acceptChild(node, this); }
|
|
|
|
|
|
|
|
|
|
using Visitor::visit;
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiPublicMember *node)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->name == _name
|
2011-07-12 14:55:27 +02:00
|
|
|
&& _scopeChain.qmlScopeObjects().contains(_scope)) {
|
2010-09-24 14:05:34 +02:00
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
}
|
2011-06-06 14:13:50 +02:00
|
|
|
if (AST::cast<Block *>(node->statement)) {
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->statement, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-09-24 14:05:34 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiObjectDefinition *node)
|
|
|
|
|
{
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->initializer, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiObjectBinding *node)
|
|
|
|
|
{
|
|
|
|
|
if (node->qualifiedId
|
|
|
|
|
&& !node->qualifiedId->next
|
2011-09-13 09:57:24 +02:00
|
|
|
&& node->qualifiedId->name == _name
|
2010-09-24 14:05:34 +02:00
|
|
|
&& checkQmlScope()) {
|
|
|
|
|
_usages.append(node->qualifiedId->identifierToken);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->initializer, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiScriptBinding *node)
|
|
|
|
|
{
|
|
|
|
|
if (node->qualifiedId
|
|
|
|
|
&& !node->qualifiedId->next
|
2011-09-13 09:57:24 +02:00
|
|
|
&& node->qualifiedId->name == _name
|
2010-09-24 14:05:34 +02:00
|
|
|
&& checkQmlScope()) {
|
|
|
|
|
_usages.append(node->qualifiedId->identifierToken);
|
|
|
|
|
}
|
2011-06-06 14:13:50 +02:00
|
|
|
if (AST::cast<Block *>(node->statement)) {
|
|
|
|
|
Node::accept(node->qualifiedId, this);
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->statement, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2010-09-24 14:05:34 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiArrayBinding *node)
|
|
|
|
|
{
|
|
|
|
|
if (node->qualifiedId
|
|
|
|
|
&& !node->qualifiedId->next
|
2011-09-13 09:57:24 +02:00
|
|
|
&& node->qualifiedId->name == _name
|
2010-09-24 14:05:34 +02:00
|
|
|
&& checkQmlScope()) {
|
|
|
|
|
_usages.append(node->qualifiedId->identifierToken);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::IdentifierExpression *node)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->name.isEmpty() || node->name != _name)
|
2010-09-24 14:05:34 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const ObjectValue *scope;
|
2011-07-12 14:55:27 +02:00
|
|
|
_scopeChain.lookup(_name, &scope);
|
2010-09-24 14:05:34 +02:00
|
|
|
if (!scope)
|
|
|
|
|
return false;
|
|
|
|
|
if (check(scope)) {
|
|
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the order of scopes in 'instantiatingComponents' is undefined,
|
|
|
|
|
// so it might still be a use - we just found a different value in a different scope first
|
|
|
|
|
|
|
|
|
|
// if scope is one of these, our match wasn't inside the instantiating components list
|
2011-07-12 14:55:27 +02:00
|
|
|
const ScopeChain &chain = _scopeChain;
|
|
|
|
|
if (chain.jsScopes().contains(scope)
|
|
|
|
|
|| chain.qmlScopeObjects().contains(scope)
|
|
|
|
|
|| chain.qmlTypes() == scope
|
|
|
|
|
|| chain.globalScope() == scope)
|
2010-09-24 14:05:34 +02:00
|
|
|
return false;
|
|
|
|
|
|
2011-07-12 14:55:27 +02:00
|
|
|
if (contains(chain.qmlComponentChain().data()))
|
2010-09-24 14:05:34 +02:00
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::FieldMemberExpression *node)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->name != _name)
|
2010-09-24 14:05:34 +02:00
|
|
|
return true;
|
|
|
|
|
|
2011-07-12 14:55:27 +02:00
|
|
|
Evaluate evaluate(&_scopeChain);
|
2010-09-24 14:05:34 +02:00
|
|
|
const Value *lhsValue = evaluate(node->base);
|
|
|
|
|
if (!lhsValue)
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
if (check(lhsValue->asObjectValue())) // passing null is ok
|
|
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::FunctionDeclaration *node)
|
2010-11-24 14:42:10 +01:00
|
|
|
{
|
|
|
|
|
return visit(static_cast<FunctionExpression *>(node));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::FunctionExpression *node)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->name == _name) {
|
2010-09-24 14:05:34 +02:00
|
|
|
if (checkLookup())
|
|
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
}
|
|
|
|
|
Node::accept(node->formals, this);
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->body, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::VariableDeclaration *node)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->name == _name) {
|
2010-09-24 14:05:34 +02:00
|
|
|
if (checkLookup())
|
|
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2011-07-12 14:55:27 +02:00
|
|
|
bool contains(const QmlComponentChain *chain)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
2011-11-29 12:53:06 +01:00
|
|
|
if (!chain || !chain->document() || !chain->document()->bind())
|
2010-09-24 14:05:34 +02:00
|
|
|
return false;
|
|
|
|
|
|
2011-11-29 12:53:06 +01:00
|
|
|
const ObjectValue *idEnv = chain->document()->bind()->idEnvironment();
|
|
|
|
|
if (idEnv && idEnv->lookupMember(_name, _scopeChain.context()))
|
|
|
|
|
return idEnv == _scope;
|
2011-07-12 14:55:27 +02:00
|
|
|
const ObjectValue *root = chain->document()->bind()->rootObjectValue();
|
2011-11-29 12:53:06 +01:00
|
|
|
if (root && root->lookupMember(_name, _scopeChain.context())) {
|
2010-09-24 14:05:34 +02:00
|
|
|
return check(root);
|
|
|
|
|
}
|
|
|
|
|
|
2011-07-12 14:55:27 +02:00
|
|
|
foreach (const QmlComponentChain *parent, chain->instantiatingComponents()) {
|
2010-09-24 14:05:34 +02:00
|
|
|
if (contains(parent))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool check(const ObjectValue *s)
|
|
|
|
|
{
|
|
|
|
|
if (!s)
|
|
|
|
|
return false;
|
2010-11-17 14:20:06 +01:00
|
|
|
const ObjectValue *definingObject;
|
2011-07-12 14:55:27 +02:00
|
|
|
s->lookupMember(_name, _scopeChain.context(), &definingObject);
|
2010-11-17 14:20:06 +01:00
|
|
|
return definingObject == _scope;
|
2010-09-24 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkQmlScope()
|
|
|
|
|
{
|
2011-07-12 14:55:27 +02:00
|
|
|
foreach (const ObjectValue *s, _scopeChain.qmlScopeObjects()) {
|
2010-09-24 14:05:34 +02:00
|
|
|
if (check(s))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkLookup()
|
|
|
|
|
{
|
|
|
|
|
const ObjectValue *scope = 0;
|
2011-07-12 14:55:27 +02:00
|
|
|
_scopeChain.lookup(_name, &scope);
|
2010-09-24 14:05:34 +02:00
|
|
|
return check(scope);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result _usages;
|
|
|
|
|
|
|
|
|
|
Document::Ptr _doc;
|
2011-07-12 14:55:27 +02:00
|
|
|
ScopeChain _scopeChain;
|
2010-09-24 14:05:34 +02:00
|
|
|
ScopeBuilder _builder;
|
|
|
|
|
|
|
|
|
|
QString _name;
|
|
|
|
|
const ObjectValue *_scope;
|
|
|
|
|
};
|
|
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
class FindTypeUsages: protected Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef QList<AST::SourceLocation> Result;
|
|
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
FindTypeUsages(Document::Ptr doc, const ContextPtr &context)
|
2011-06-06 11:17:12 +02:00
|
|
|
: _doc(doc)
|
|
|
|
|
, _context(context)
|
2011-07-12 14:55:27 +02:00
|
|
|
, _scopeChain(doc, context)
|
|
|
|
|
, _builder(&_scopeChain)
|
2011-06-06 11:17:12 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result operator()(const QString &name, const ObjectValue *typeValue)
|
|
|
|
|
{
|
|
|
|
|
_name = name;
|
|
|
|
|
_typeValue = typeValue;
|
|
|
|
|
_usages.clear();
|
|
|
|
|
if (_doc)
|
|
|
|
|
Node::accept(_doc->ast(), this);
|
|
|
|
|
return _usages;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void accept(AST::Node *node)
|
|
|
|
|
{ AST::Node::acceptChild(node, this); }
|
|
|
|
|
|
|
|
|
|
using Visitor::visit;
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiPublicMember *node)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->memberType == _name){
|
2011-06-06 11:17:12 +02:00
|
|
|
const ObjectValue * tVal = _context->lookupType(_doc.data(), QStringList(_name));
|
|
|
|
|
if (tVal == _typeValue)
|
|
|
|
|
_usages.append(node->typeToken);
|
|
|
|
|
}
|
2011-06-06 14:13:50 +02:00
|
|
|
if (AST::cast<Block *>(node->statement)) {
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->statement, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2011-06-06 11:17:12 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiObjectDefinition *node)
|
|
|
|
|
{
|
|
|
|
|
checkTypeName(node->qualifiedTypeNameId);
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->initializer, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::UiObjectBinding *node)
|
|
|
|
|
{
|
|
|
|
|
checkTypeName(node->qualifiedTypeNameId);
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->initializer, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-06 14:13:50 +02:00
|
|
|
virtual bool visit(AST::UiScriptBinding *node)
|
|
|
|
|
{
|
|
|
|
|
if (AST::cast<Block *>(node->statement)) {
|
|
|
|
|
Node::accept(node->qualifiedId, this);
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->statement, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
virtual bool visit(AST::IdentifierExpression *node)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->name != _name)
|
2011-06-06 11:17:12 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
const ObjectValue *scope;
|
2011-07-12 14:55:27 +02:00
|
|
|
const Value *objV = _scopeChain.lookup(_name, &scope);
|
2011-06-06 11:17:12 +02:00
|
|
|
if (objV == _typeValue)
|
|
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::FieldMemberExpression *node)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (node->name != _name)
|
2011-06-06 11:17:12 +02:00
|
|
|
return true;
|
2011-07-12 14:55:27 +02:00
|
|
|
Evaluate evaluate(&_scopeChain);
|
2011-06-06 11:17:12 +02:00
|
|
|
const Value *lhsValue = evaluate(node->base);
|
|
|
|
|
if (!lhsValue)
|
|
|
|
|
return true;
|
|
|
|
|
const ObjectValue *lhsObj = lhsValue->asObjectValue();
|
|
|
|
|
if (lhsObj && lhsObj->lookupMember(_name, _context) == _typeValue)
|
|
|
|
|
_usages.append(node->identifierToken);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::FunctionDeclaration *node)
|
|
|
|
|
{
|
|
|
|
|
return visit(static_cast<FunctionExpression *>(node));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::FunctionExpression *node)
|
|
|
|
|
{
|
|
|
|
|
Node::accept(node->formals, this);
|
|
|
|
|
_builder.push(node);
|
|
|
|
|
Node::accept(node->body, this);
|
|
|
|
|
_builder.pop();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(AST::VariableDeclaration *node)
|
|
|
|
|
{
|
|
|
|
|
Node::accept(node->expression, this);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(UiImport *ast)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast && ast->importId == _name) {
|
2011-06-06 11:17:12 +02:00
|
|
|
const Imports *imp = _context->imports(_doc.data());
|
|
|
|
|
if (!imp)
|
|
|
|
|
return false;
|
|
|
|
|
if (_context->lookupType(_doc.data(), QStringList(_name)) == _typeValue)
|
|
|
|
|
_usages.append(ast->importIdToken);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool checkTypeName(UiQualifiedId *id)
|
|
|
|
|
{
|
|
|
|
|
for (UiQualifiedId *att = id; att; att = att->next){
|
2011-09-13 09:57:24 +02:00
|
|
|
if (att->name == _name) {
|
2011-06-06 11:17:12 +02:00
|
|
|
const ObjectValue *objectValue = _context->lookupType(_doc.data(), id, att->next);
|
|
|
|
|
if (_typeValue == objectValue){
|
|
|
|
|
_usages.append(att->identifierToken);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Result _usages;
|
|
|
|
|
|
|
|
|
|
Document::Ptr _doc;
|
2011-07-13 15:04:27 +02:00
|
|
|
ContextPtr _context;
|
2011-07-12 14:55:27 +02:00
|
|
|
ScopeChain _scopeChain;
|
2011-06-06 11:17:12 +02:00
|
|
|
ScopeBuilder _builder;
|
|
|
|
|
|
|
|
|
|
QString _name;
|
|
|
|
|
const ObjectValue *_typeValue;
|
|
|
|
|
};
|
|
|
|
|
|
2010-09-24 14:05:34 +02:00
|
|
|
class FindTargetExpression: protected Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2011-06-06 11:17:12 +02:00
|
|
|
enum Kind {
|
|
|
|
|
ExpKind,
|
|
|
|
|
TypeKind
|
|
|
|
|
};
|
|
|
|
|
|
2011-07-12 14:55:27 +02:00
|
|
|
FindTargetExpression(Document::Ptr doc, const ScopeChain *scopeChain)
|
|
|
|
|
: _doc(doc), _scopeChain(scopeChain)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-17 14:43:08 +01:00
|
|
|
void operator()(quint32 offset)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
2011-04-19 15:42:14 +02:00
|
|
|
_name.clear();
|
2011-03-17 14:43:08 +01:00
|
|
|
_scope = 0;
|
|
|
|
|
_objectNode = 0;
|
2010-09-24 14:05:34 +02:00
|
|
|
_offset = offset;
|
2011-06-06 11:17:12 +02:00
|
|
|
_typeKind = ExpKind;
|
2010-09-24 14:05:34 +02:00
|
|
|
if (_doc)
|
|
|
|
|
Node::accept(_doc->ast(), this);
|
2011-03-17 14:43:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString name() const
|
|
|
|
|
{ return _name; }
|
|
|
|
|
|
|
|
|
|
const ObjectValue *scope()
|
|
|
|
|
{
|
|
|
|
|
if (!_scope)
|
2011-07-12 14:55:27 +02:00
|
|
|
_scopeChain->lookup(_name, &_scope);
|
2011-03-17 14:43:08 +01:00
|
|
|
return _scope;
|
2010-09-24 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
Kind typeKind(){
|
|
|
|
|
return _typeKind;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const Value *targetValue(){
|
|
|
|
|
return _targetValue;
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 14:05:34 +02:00
|
|
|
protected:
|
|
|
|
|
void accept(AST::Node *node)
|
|
|
|
|
{ AST::Node::acceptChild(node, this); }
|
|
|
|
|
|
|
|
|
|
using Visitor::visit;
|
|
|
|
|
|
|
|
|
|
virtual bool preVisit(Node *node)
|
|
|
|
|
{
|
|
|
|
|
if (Statement *stmt = node->statementCast()) {
|
|
|
|
|
return containsOffset(stmt->firstSourceLocation(), stmt->lastSourceLocation());
|
|
|
|
|
} else if (ExpressionNode *exp = node->expressionCast()) {
|
|
|
|
|
return containsOffset(exp->firstSourceLocation(), exp->lastSourceLocation());
|
|
|
|
|
} else if (UiObjectMember *ui = node->uiObjectMemberCast()) {
|
|
|
|
|
return containsOffset(ui->firstSourceLocation(), ui->lastSourceLocation());
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(IdentifierExpression *node)
|
|
|
|
|
{
|
2011-06-06 11:17:12 +02:00
|
|
|
if (containsOffset(node->identifierToken)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
_name = node->name.toString();
|
2011-06-06 11:17:12 +02:00
|
|
|
if ((!_name.isEmpty()) && _name.at(0).isUpper()) {
|
|
|
|
|
// a possible type
|
2011-07-12 14:55:27 +02:00
|
|
|
_targetValue = _scopeChain->lookup(_name, &_scope);
|
2011-10-10 10:55:37 +02:00
|
|
|
if (value_cast<ObjectValue>(_targetValue))
|
2011-06-06 14:13:50 +02:00
|
|
|
_typeKind = TypeKind;
|
2011-06-06 11:17:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 14:05:34 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(FieldMemberExpression *node)
|
|
|
|
|
{
|
|
|
|
|
if (containsOffset(node->identifierToken)) {
|
2011-03-17 14:43:08 +01:00
|
|
|
setScope(node->base);
|
2011-09-13 09:57:24 +02:00
|
|
|
_name = node->name.toString();
|
2011-06-06 11:17:12 +02:00
|
|
|
if ((!_name.isEmpty()) && _name.at(0).isUpper()) {
|
|
|
|
|
// a possible type
|
2011-07-12 14:55:27 +02:00
|
|
|
Evaluate evaluate(_scopeChain);
|
2011-06-06 11:17:12 +02:00
|
|
|
const Value *lhsValue = evaluate(node->base);
|
|
|
|
|
if (!lhsValue)
|
|
|
|
|
return true;
|
|
|
|
|
const ObjectValue *lhsObj = lhsValue->asObjectValue();
|
|
|
|
|
if (lhsObj) {
|
|
|
|
|
_scope = lhsObj;
|
2011-07-12 14:55:27 +02:00
|
|
|
_targetValue = lhsObj->lookupMember(_name, _scopeChain->context());
|
2011-06-06 11:17:12 +02:00
|
|
|
_typeKind = TypeKind;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-09-24 14:05:34 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(UiScriptBinding *node)
|
|
|
|
|
{
|
|
|
|
|
return !checkBindingName(node->qualifiedId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(UiArrayBinding *node)
|
|
|
|
|
{
|
|
|
|
|
return !checkBindingName(node->qualifiedId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(UiObjectBinding *node)
|
|
|
|
|
{
|
2011-06-06 11:17:12 +02:00
|
|
|
if ((!checkTypeName(node->qualifiedTypeNameId)) &&
|
|
|
|
|
(!checkBindingName(node->qualifiedId))) {
|
2011-03-17 14:43:08 +01:00
|
|
|
Node *oldObjectNode = _objectNode;
|
|
|
|
|
_objectNode = node;
|
|
|
|
|
accept(node->initializer);
|
|
|
|
|
_objectNode = oldObjectNode;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(UiObjectDefinition *node)
|
|
|
|
|
{
|
2011-06-06 11:17:12 +02:00
|
|
|
if (!checkTypeName(node->qualifiedTypeNameId)) {
|
|
|
|
|
Node *oldObjectNode = _objectNode;
|
|
|
|
|
_objectNode = node;
|
|
|
|
|
accept(node->initializer);
|
|
|
|
|
_objectNode = oldObjectNode;
|
|
|
|
|
}
|
2011-03-17 14:43:08 +01:00
|
|
|
return false;
|
2010-09-24 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(UiPublicMember *node)
|
|
|
|
|
{
|
2011-06-06 11:17:12 +02:00
|
|
|
if (containsOffset(node->typeToken)){
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!node->memberType.isEmpty()) {
|
|
|
|
|
_name = node->memberType.toString();
|
2011-07-12 14:55:27 +02:00
|
|
|
_targetValue = _scopeChain->context()->lookupType(_doc.data(), QStringList(_name));
|
2011-06-06 11:17:12 +02:00
|
|
|
_scope = 0;
|
|
|
|
|
_typeKind = TypeKind;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
} else if (containsOffset(node->identifierToken)) {
|
2011-03-17 14:43:08 +01:00
|
|
|
_scope = _doc->bind()->findQmlObject(_objectNode);
|
2011-09-13 09:57:24 +02:00
|
|
|
_name = node->name.toString();
|
2010-09-24 14:05:34 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(FunctionDeclaration *node)
|
2010-11-24 14:42:10 +01:00
|
|
|
{
|
|
|
|
|
return visit(static_cast<FunctionExpression *>(node));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(FunctionExpression *node)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
|
|
|
|
if (containsOffset(node->identifierToken)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
_name = node->name.toString();
|
2010-09-24 14:05:34 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual bool visit(VariableDeclaration *node)
|
|
|
|
|
{
|
|
|
|
|
if (containsOffset(node->identifierToken)) {
|
2011-09-13 09:57:24 +02:00
|
|
|
_name = node->name.toString();
|
2010-09-24 14:05:34 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool containsOffset(SourceLocation start, SourceLocation end)
|
|
|
|
|
{
|
|
|
|
|
return _offset >= start.begin() && _offset <= end.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool containsOffset(SourceLocation loc)
|
|
|
|
|
{
|
|
|
|
|
return _offset >= loc.begin() && _offset <= loc.end();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool checkBindingName(UiQualifiedId *id)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (id && !id->name.isEmpty() && !id->next && containsOffset(id->identifierToken)) {
|
2011-03-17 14:43:08 +01:00
|
|
|
_scope = _doc->bind()->findQmlObject(_objectNode);
|
2011-09-13 09:57:24 +02:00
|
|
|
_name = id->name.toString();
|
2010-09-24 14:05:34 +02:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
bool checkTypeName(UiQualifiedId *id)
|
|
|
|
|
{
|
|
|
|
|
for (UiQualifiedId *att = id; att; att = att->next) {
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!att->name.isEmpty() && containsOffset(att->identifierToken)) {
|
2011-07-12 14:55:27 +02:00
|
|
|
_targetValue = _scopeChain->context()->lookupType(_doc.data(), id, att->next);
|
2011-06-06 11:17:12 +02:00
|
|
|
_scope = 0;
|
2011-09-13 09:57:24 +02:00
|
|
|
_name = att->name.toString();
|
2011-06-06 11:17:12 +02:00
|
|
|
_typeKind = TypeKind;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-17 14:43:08 +01:00
|
|
|
void setScope(Node *node)
|
|
|
|
|
{
|
2011-07-12 14:55:27 +02:00
|
|
|
Evaluate evaluate(_scopeChain);
|
2011-03-17 14:43:08 +01:00
|
|
|
const Value *v = evaluate(node);
|
|
|
|
|
if (v)
|
|
|
|
|
_scope = v->asObjectValue();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString _name;
|
|
|
|
|
const ObjectValue *_scope;
|
2011-06-06 11:17:12 +02:00
|
|
|
const Value *_targetValue;
|
2011-03-17 14:43:08 +01:00
|
|
|
Node *_objectNode;
|
2010-09-24 14:05:34 +02:00
|
|
|
Document::Ptr _doc;
|
2011-07-12 14:55:27 +02:00
|
|
|
const ScopeChain *_scopeChain;
|
2010-09-24 14:05:34 +02:00
|
|
|
quint32 _offset;
|
2011-06-06 11:17:12 +02:00
|
|
|
Kind _typeKind;
|
2010-09-24 14:05:34 +02:00
|
|
|
};
|
|
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
static QString matchingLine(unsigned position, const QString &source)
|
|
|
|
|
{
|
|
|
|
|
int start = source.lastIndexOf(QLatin1Char('\n'), position);
|
|
|
|
|
start += 1;
|
|
|
|
|
int end = source.indexOf(QLatin1Char('\n'), position);
|
|
|
|
|
|
|
|
|
|
return source.mid(start, end - start);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 14:05:34 +02:00
|
|
|
class ProcessFile: public std::unary_function<QString, QList<FindReferences::Usage> >
|
|
|
|
|
{
|
2011-07-13 15:04:27 +02:00
|
|
|
ContextPtr context;
|
2010-09-24 14:05:34 +02:00
|
|
|
typedef FindReferences::Usage Usage;
|
|
|
|
|
QString name;
|
|
|
|
|
const ObjectValue *scope;
|
2012-05-25 16:45:18 +02:00
|
|
|
QFutureInterface<Usage> *future;
|
2010-09-24 14:05:34 +02:00
|
|
|
|
|
|
|
|
public:
|
2011-07-13 15:04:27 +02:00
|
|
|
ProcessFile(const ContextPtr &context,
|
2010-09-24 14:05:34 +02:00
|
|
|
QString name,
|
2012-05-25 16:45:18 +02:00
|
|
|
const ObjectValue *scope,
|
|
|
|
|
QFutureInterface<Usage> *future)
|
|
|
|
|
: context(context), name(name), scope(scope), future(future)
|
2010-09-24 14:05:34 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
QList<Usage> operator()(const QString &fileName)
|
|
|
|
|
{
|
|
|
|
|
QList<Usage> usages;
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
|
|
|
|
if (future->isCanceled())
|
|
|
|
|
return usages;
|
2011-07-13 15:04:27 +02:00
|
|
|
Document::Ptr doc = context->snapshot().document(fileName);
|
2010-09-24 14:05:34 +02:00
|
|
|
if (!doc)
|
|
|
|
|
return usages;
|
|
|
|
|
|
|
|
|
|
// find all idenfifier expressions, try to resolve them and check if the result is in scope
|
2011-07-13 15:04:27 +02:00
|
|
|
FindUsages findUsages(doc, context);
|
2010-09-24 14:05:34 +02:00
|
|
|
FindUsages::Result results = findUsages(name, scope);
|
2011-04-19 15:42:14 +02:00
|
|
|
foreach (const AST::SourceLocation &loc, results)
|
2010-09-24 14:05:34 +02:00
|
|
|
usages.append(Usage(fileName, matchingLine(loc.offset, doc->source()), loc.startLine, loc.startColumn - 1, loc.length));
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
2010-09-24 14:05:34 +02:00
|
|
|
return usages;
|
|
|
|
|
}
|
2011-06-06 11:17:12 +02:00
|
|
|
};
|
2010-09-24 14:05:34 +02:00
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
class SearchFileForType: public std::unary_function<QString, QList<FindReferences::Usage> >
|
|
|
|
|
{
|
2011-07-13 15:04:27 +02:00
|
|
|
ContextPtr context;
|
2011-06-06 11:17:12 +02:00
|
|
|
typedef FindReferences::Usage Usage;
|
|
|
|
|
QString name;
|
|
|
|
|
const ObjectValue *scope;
|
2012-05-25 16:45:18 +02:00
|
|
|
QFutureInterface<Usage> *future;
|
2011-06-06 11:17:12 +02:00
|
|
|
|
|
|
|
|
public:
|
2011-07-13 15:04:27 +02:00
|
|
|
SearchFileForType(const ContextPtr &context,
|
2012-05-25 16:45:18 +02:00
|
|
|
QString name,
|
|
|
|
|
const ObjectValue *scope,
|
|
|
|
|
QFutureInterface<Usage> *future)
|
|
|
|
|
: context(context), name(name), scope(scope), future(future)
|
2011-06-06 11:17:12 +02:00
|
|
|
{ }
|
|
|
|
|
|
|
|
|
|
QList<Usage> operator()(const QString &fileName)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
2011-06-06 11:17:12 +02:00
|
|
|
QList<Usage> usages;
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
|
|
|
|
if (future->isCanceled())
|
|
|
|
|
return usages;
|
2011-07-13 15:04:27 +02:00
|
|
|
Document::Ptr doc = context->snapshot().document(fileName);
|
2011-06-06 11:17:12 +02:00
|
|
|
if (!doc)
|
|
|
|
|
return usages;
|
|
|
|
|
|
|
|
|
|
// find all idenfifier expressions, try to resolve them and check if the result is in scope
|
2011-07-13 15:04:27 +02:00
|
|
|
FindTypeUsages findUsages(doc, context);
|
2011-06-06 11:17:12 +02:00
|
|
|
FindTypeUsages::Result results = findUsages(name, scope);
|
|
|
|
|
foreach (const AST::SourceLocation &loc, results)
|
|
|
|
|
usages.append(Usage(fileName, matchingLine(loc.offset, doc->source()), loc.startLine, loc.startColumn - 1, loc.length));
|
2012-05-25 16:45:18 +02:00
|
|
|
if (future->isPaused())
|
|
|
|
|
future->waitForResume();
|
2011-06-06 11:17:12 +02:00
|
|
|
return usages;
|
2010-09-24 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class UpdateUI: public std::binary_function<QList<FindReferences::Usage> &, QList<FindReferences::Usage>, void>
|
|
|
|
|
{
|
|
|
|
|
typedef FindReferences::Usage Usage;
|
|
|
|
|
QFutureInterface<Usage> *future;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
UpdateUI(QFutureInterface<Usage> *future): future(future) {}
|
|
|
|
|
|
|
|
|
|
void operator()(QList<Usage> &, const QList<Usage> &usages)
|
|
|
|
|
{
|
|
|
|
|
foreach (const Usage &u, usages)
|
|
|
|
|
future->reportResult(u);
|
|
|
|
|
|
|
|
|
|
future->setProgressValue(future->progressValue() + 1);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // end of anonymous namespace
|
|
|
|
|
|
|
|
|
|
FindReferences::FindReferences(QObject *parent)
|
2011-09-09 16:10:57 +02:00
|
|
|
: QObject(parent)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
|
|
|
|
m_watcher.setPendingResultsLimit(1);
|
|
|
|
|
connect(&m_watcher, SIGNAL(resultsReadyAt(int,int)), this, SLOT(displayResults(int,int)));
|
|
|
|
|
connect(&m_watcher, SIGNAL(finished()), this, SLOT(searchFinished()));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FindReferences::~FindReferences()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void find_helper(QFutureInterface<FindReferences::Usage> &future,
|
|
|
|
|
const ModelManagerInterface::WorkingCopy workingCopy,
|
|
|
|
|
Snapshot snapshot,
|
|
|
|
|
const QString fileName,
|
2011-07-11 12:53:05 +02:00
|
|
|
quint32 offset,
|
|
|
|
|
QString replacement)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
|
|
|
|
// update snapshot from workingCopy to make sure it's up to date
|
|
|
|
|
// ### remove?
|
|
|
|
|
// ### this is a great candidate for map-reduce
|
|
|
|
|
QHashIterator< QString, QPair<QString, int> > it(workingCopy.all());
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
it.next();
|
2011-09-09 10:55:11 +02:00
|
|
|
const QString fileName = it.key();
|
|
|
|
|
Document::Ptr oldDoc = snapshot.document(fileName);
|
2010-09-24 14:05:34 +02:00
|
|
|
if (oldDoc && oldDoc->editorRevision() == it.value().second)
|
|
|
|
|
continue;
|
|
|
|
|
|
2011-09-09 10:55:11 +02:00
|
|
|
Document::Language language;
|
|
|
|
|
if (oldDoc)
|
|
|
|
|
language = oldDoc->language();
|
|
|
|
|
else
|
|
|
|
|
language = QmlJSTools::languageOfFile(fileName);
|
|
|
|
|
|
2011-11-03 13:47:03 +01:00
|
|
|
Document::MutablePtr newDoc = snapshot.documentFromSource(
|
|
|
|
|
it.value().first, fileName, language);
|
2010-09-24 14:05:34 +02:00
|
|
|
newDoc->parse();
|
|
|
|
|
snapshot.insert(newDoc);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find the scope for the name we're searching
|
|
|
|
|
Document::Ptr doc = snapshot.document(fileName);
|
|
|
|
|
if (!doc)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-06-28 12:01:56 +02:00
|
|
|
QmlJS::ModelManagerInterface *modelManager = QmlJS::ModelManagerInterface::instance();
|
|
|
|
|
|
|
|
|
|
Link link(snapshot, modelManager->importPaths(), modelManager->builtins(doc));
|
2011-07-13 15:04:27 +02:00
|
|
|
ContextPtr context = link();
|
2011-05-06 13:31:30 +02:00
|
|
|
|
2011-07-13 15:04:27 +02:00
|
|
|
ScopeChain scopeChain(doc, context);
|
2011-07-12 14:55:27 +02:00
|
|
|
ScopeBuilder builder(&scopeChain);
|
2010-09-24 14:05:34 +02:00
|
|
|
ScopeAstPath astPath(doc);
|
|
|
|
|
builder.push(astPath(offset));
|
|
|
|
|
|
2011-07-12 14:55:27 +02:00
|
|
|
FindTargetExpression findTarget(doc, &scopeChain);
|
2011-03-17 14:43:08 +01:00
|
|
|
findTarget(offset);
|
|
|
|
|
const QString &name = findTarget.name();
|
2010-09-24 14:05:34 +02:00
|
|
|
if (name.isEmpty())
|
|
|
|
|
return;
|
2011-07-11 12:53:05 +02:00
|
|
|
if (!replacement.isNull() && replacement.isEmpty())
|
|
|
|
|
replacement = name;
|
2010-09-24 14:05:34 +02:00
|
|
|
|
|
|
|
|
QStringList files;
|
|
|
|
|
foreach (const Document::Ptr &doc, snapshot) {
|
|
|
|
|
// ### skip files that don't contain the name token
|
|
|
|
|
files.append(doc->fileName());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
future.setProgressRange(0, files.size());
|
|
|
|
|
|
2011-07-11 12:53:05 +02:00
|
|
|
// report a dummy usage to indicate the search is starting
|
2011-09-07 20:28:04 +02:00
|
|
|
FindReferences::Usage searchStarting(replacement, name, 0, 0, 0);
|
2011-07-11 12:53:05 +02:00
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
if (findTarget.typeKind() == findTarget.TypeKind){
|
2011-10-10 10:55:37 +02:00
|
|
|
const ObjectValue *typeValue = value_cast<ObjectValue>(findTarget.targetValue());
|
2011-06-06 11:17:12 +02:00
|
|
|
if (!typeValue)
|
|
|
|
|
return;
|
2011-07-11 12:53:05 +02:00
|
|
|
future.reportResult(searchStarting);
|
2010-09-24 14:05:34 +02:00
|
|
|
|
2012-05-25 16:45:18 +02:00
|
|
|
SearchFileForType process(context, name, typeValue, &future);
|
2011-06-06 11:17:12 +02:00
|
|
|
UpdateUI reduce(&future);
|
2010-09-24 14:05:34 +02:00
|
|
|
|
2011-06-06 11:17:12 +02:00
|
|
|
QtConcurrent::blockingMappedReduced<QList<FindReferences::Usage> > (files, process, reduce);
|
|
|
|
|
} else {
|
|
|
|
|
const ObjectValue *scope = findTarget.scope();
|
|
|
|
|
if (!scope)
|
|
|
|
|
return;
|
2011-07-13 15:04:27 +02:00
|
|
|
scope->lookupMember(name, context, &scope);
|
2011-06-06 11:17:12 +02:00
|
|
|
if (!scope)
|
|
|
|
|
return;
|
2011-09-07 20:28:04 +02:00
|
|
|
if (!scope->className().isEmpty())
|
|
|
|
|
searchStarting.lineText.prepend(scope->className() + QLatin1Char('.'));
|
2011-07-11 12:53:05 +02:00
|
|
|
future.reportResult(searchStarting);
|
2011-06-06 11:17:12 +02:00
|
|
|
|
2012-05-25 16:45:18 +02:00
|
|
|
ProcessFile process(context, name, scope, &future);
|
2011-06-06 11:17:12 +02:00
|
|
|
UpdateUI reduce(&future);
|
|
|
|
|
|
|
|
|
|
QtConcurrent::blockingMappedReduced<QList<FindReferences::Usage> > (files, process, reduce);
|
|
|
|
|
}
|
2010-09-24 14:05:34 +02:00
|
|
|
future.setProgressValue(files.size());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindReferences::findUsages(const QString &fileName, quint32 offset)
|
|
|
|
|
{
|
2011-07-11 12:53:05 +02:00
|
|
|
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
|
|
|
|
|
|
|
|
|
|
QFuture<Usage> result = QtConcurrent::run(
|
|
|
|
|
&find_helper, modelManager->workingCopy(),
|
|
|
|
|
modelManager->snapshot(), fileName, offset,
|
|
|
|
|
QString());
|
|
|
|
|
m_watcher.setFuture(result);
|
2010-09-24 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
|
2011-07-11 12:53:05 +02:00
|
|
|
void FindReferences::renameUsages(const QString &fileName, quint32 offset,
|
|
|
|
|
const QString &replacement)
|
2010-09-24 14:05:34 +02:00
|
|
|
{
|
|
|
|
|
ModelManagerInterface *modelManager = ModelManagerInterface::instance();
|
|
|
|
|
|
2011-07-11 12:53:05 +02:00
|
|
|
// an empty non-null string asks the future to use the current name as base
|
|
|
|
|
QString newName = replacement;
|
|
|
|
|
if (newName.isNull())
|
|
|
|
|
newName = QLatin1String("");
|
2010-09-24 14:05:34 +02:00
|
|
|
|
|
|
|
|
QFuture<Usage> result = QtConcurrent::run(
|
|
|
|
|
&find_helper, modelManager->workingCopy(),
|
2011-07-11 12:53:05 +02:00
|
|
|
modelManager->snapshot(), fileName, offset,
|
|
|
|
|
newName);
|
2010-09-24 14:05:34 +02:00
|
|
|
m_watcher.setFuture(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindReferences::displayResults(int first, int last)
|
|
|
|
|
{
|
2011-03-17 15:08:25 +01:00
|
|
|
// the first usage is always a dummy to indicate we now start searching
|
|
|
|
|
if (first == 0) {
|
2011-07-11 12:53:05 +02:00
|
|
|
Usage dummy = m_watcher.future().resultAt(0);
|
2011-09-09 16:10:57 +02:00
|
|
|
const QString replacement = dummy.path;
|
|
|
|
|
const QString symbolName = dummy.lineText;
|
|
|
|
|
const QString label = tr("QML/JS Usages:");
|
2011-07-11 12:53:05 +02:00
|
|
|
|
|
|
|
|
if (replacement.isEmpty()) {
|
2011-09-09 16:10:57 +02:00
|
|
|
m_currentSearch = Find::SearchResultWindow::instance()->startNewSearch(
|
|
|
|
|
label, QString(), symbolName, Find::SearchResultWindow::SearchOnly);
|
2011-07-11 12:53:05 +02:00
|
|
|
} else {
|
2011-09-09 16:10:57 +02:00
|
|
|
m_currentSearch = Find::SearchResultWindow::instance()->startNewSearch(
|
|
|
|
|
label, QString(), symbolName, Find::SearchResultWindow::SearchAndReplace);
|
2011-09-02 11:51:31 +02:00
|
|
|
m_currentSearch->setTextToReplace(replacement);
|
|
|
|
|
connect(m_currentSearch, SIGNAL(replaceButtonClicked(QString,QList<Find::SearchResultItem>)),
|
2011-07-11 12:53:05 +02:00
|
|
|
SLOT(onReplaceButtonClicked(QString,QList<Find::SearchResultItem>)));
|
|
|
|
|
}
|
2011-09-02 11:51:31 +02:00
|
|
|
connect(m_currentSearch, SIGNAL(activated(Find::SearchResultItem)),
|
2011-03-17 15:08:25 +01:00
|
|
|
this, SLOT(openEditor(Find::SearchResultItem)));
|
2011-09-09 08:46:51 +02:00
|
|
|
connect(m_currentSearch, SIGNAL(cancelled()), this, SLOT(cancel()));
|
2012-05-25 16:45:18 +02:00
|
|
|
connect(m_currentSearch, SIGNAL(paused(bool)), this, SLOT(setPaused(bool)));
|
2012-09-13 15:50:06 +02:00
|
|
|
Find::SearchResultWindow::instance()->popup(Core::IOutputPane::Flags(Core::IOutputPane::ModeSwitch | Core::IOutputPane::WithFocus));
|
2011-03-17 15:08:25 +01:00
|
|
|
|
2012-01-24 15:36:40 +01:00
|
|
|
Core::ProgressManager *progressManager = Core::ICore::progressManager();
|
2011-03-17 15:08:25 +01:00
|
|
|
Core::FutureProgress *progress = progressManager->addTask(
|
|
|
|
|
m_watcher.future(), tr("Searching"),
|
|
|
|
|
QmlJSEditor::Constants::TASK_SEARCH);
|
2012-05-25 16:45:18 +02:00
|
|
|
connect(progress, SIGNAL(clicked()), m_currentSearch, SLOT(popup()));
|
2011-03-17 15:08:25 +01:00
|
|
|
|
|
|
|
|
++first;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-09 16:10:57 +02:00
|
|
|
if (!m_currentSearch) {
|
|
|
|
|
m_watcher.cancel();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-09-24 14:05:34 +02:00
|
|
|
for (int index = first; index != last; ++index) {
|
|
|
|
|
Usage result = m_watcher.future().resultAt(index);
|
2011-09-02 11:51:31 +02:00
|
|
|
m_currentSearch->addResult(result.path,
|
2010-09-24 14:05:34 +02:00
|
|
|
result.line,
|
|
|
|
|
result.lineText,
|
|
|
|
|
result.col,
|
|
|
|
|
result.len);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void FindReferences::searchFinished()
|
|
|
|
|
{
|
2011-09-09 16:10:57 +02:00
|
|
|
if (m_currentSearch)
|
2012-05-16 15:59:16 +02:00
|
|
|
m_currentSearch->finishSearch(m_watcher.isCanceled());
|
2011-09-02 11:51:31 +02:00
|
|
|
m_currentSearch = 0;
|
2010-09-24 14:05:34 +02:00
|
|
|
emit changed();
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-09 08:46:51 +02:00
|
|
|
void FindReferences::cancel()
|
|
|
|
|
{
|
|
|
|
|
m_watcher.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-25 16:45:18 +02:00
|
|
|
void FindReferences::setPaused(bool paused)
|
|
|
|
|
{
|
|
|
|
|
if (!paused || m_watcher.isRunning()) // guard against pausing when the search is finished
|
|
|
|
|
m_watcher.setPaused(paused);
|
|
|
|
|
}
|
|
|
|
|
|
2010-09-24 14:05:34 +02:00
|
|
|
void FindReferences::openEditor(const Find::SearchResultItem &item)
|
|
|
|
|
{
|
|
|
|
|
if (item.path.size() > 0) {
|
2012-03-28 23:18:20 +02:00
|
|
|
TextEditor::BaseTextEditorWidget::openEditorAt(QDir::fromNativeSeparators(item.path.first()),
|
|
|
|
|
item.lineNumber, item.textMarkPos, Core::Id(),
|
|
|
|
|
Core::EditorManager::ModeSwitch);
|
2010-09-24 14:05:34 +02:00
|
|
|
} else {
|
2012-05-08 09:43:14 +02:00
|
|
|
Core::EditorManager::openEditor(QDir::fromNativeSeparators(item.text),
|
|
|
|
|
Core::Id(), Core::EditorManager::ModeSwitch);
|
2010-09-24 14:05:34 +02:00
|
|
|
}
|
|
|
|
|
}
|
2011-07-11 12:53:05 +02:00
|
|
|
|
|
|
|
|
void FindReferences::onReplaceButtonClicked(const QString &text, const QList<Find::SearchResultItem> &items)
|
|
|
|
|
{
|
|
|
|
|
const QStringList fileNames = TextEditor::BaseFileFind::replaceAll(text, items);
|
|
|
|
|
|
|
|
|
|
// files that are opened in an editor are changed, but not saved
|
|
|
|
|
QStringList changedOnDisk;
|
|
|
|
|
QStringList changedUnsavedEditors;
|
|
|
|
|
Core::EditorManager *editorManager = Core::EditorManager::instance();
|
|
|
|
|
foreach (const QString &fileName, fileNames) {
|
|
|
|
|
if (editorManager->editorsForFileName(fileName).isEmpty())
|
|
|
|
|
changedOnDisk += fileName;
|
|
|
|
|
else
|
|
|
|
|
changedUnsavedEditors += fileName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!changedOnDisk.isEmpty())
|
|
|
|
|
QmlJS::ModelManagerInterface::instance()->updateSourceFiles(changedOnDisk, true);
|
|
|
|
|
if (!changedUnsavedEditors.isEmpty())
|
|
|
|
|
QmlJS::ModelManagerInterface::instance()->updateSourceFiles(changedUnsavedEditors, false);
|
|
|
|
|
|
2011-09-02 11:51:31 +02:00
|
|
|
Find::SearchResultWindow::instance()->hide();
|
2011-07-11 12:53:05 +02:00
|
|
|
}
|