2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2010-11-16 13:54:50 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2010-11-16 13:54:50 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2010-11-16 13:54:50 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2016-01-15 14:57:40 +01:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2010-11-16 13:54:50 +01:00
|
|
|
**
|
2016-01-15 14:57:40 +01:00
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2010-12-17 17:14:20 +01:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2010-11-16 13:54:50 +01:00
|
|
|
|
|
|
|
|
#include "qmljslocatordata.h"
|
|
|
|
|
|
2018-05-08 13:40:45 +02:00
|
|
|
#include <projectexplorer/project.h>
|
|
|
|
|
#include <projectexplorer/session.h>
|
|
|
|
|
|
2010-11-16 13:54:50 +01:00
|
|
|
#include <qmljs/qmljsmodelmanagerinterface.h>
|
2011-10-07 14:04:06 +02:00
|
|
|
#include <qmljs/qmljsutils.h>
|
2011-07-01 13:51:53 +02:00
|
|
|
//#include <qmljs/qmljsinterpreter.h>
|
2010-11-16 13:54:50 +01:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
|
|
|
|
|
2020-02-28 17:51:32 +01:00
|
|
|
#include <QDebug>
|
2014-09-19 13:57:20 +02:00
|
|
|
#include <QMutexLocker>
|
2011-10-07 14:04:06 +02:00
|
|
|
|
2010-11-16 13:54:50 +01:00
|
|
|
using namespace QmlJSTools::Internal;
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
|
2018-02-08 10:40:29 +01:00
|
|
|
LocatorData::LocatorData()
|
2010-11-16 13:54:50 +01:00
|
|
|
{
|
2015-02-03 23:48:57 +02:00
|
|
|
ModelManagerInterface *manager = ModelManagerInterface::instance();
|
2010-11-16 13:54:50 +01:00
|
|
|
|
2018-05-08 13:40:45 +02:00
|
|
|
// Force the updating of source file when updating a project (they could be cached, in such
|
|
|
|
|
// case LocatorData::onDocumentUpdated will not be called.
|
|
|
|
|
connect(manager, &ModelManagerInterface::projectInfoUpdated,
|
|
|
|
|
[manager](const ModelManagerInterface::ProjectInfo &info) {
|
|
|
|
|
QStringList files;
|
2019-05-28 13:49:26 +02:00
|
|
|
for (const Utils::FilePath &f: info.project->files(ProjectExplorer::Project::SourceFiles))
|
2018-05-08 13:40:45 +02:00
|
|
|
files << f.toString();
|
|
|
|
|
manager->updateSourceFiles(files, true);
|
|
|
|
|
});
|
|
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
connect(manager, &ModelManagerInterface::documentUpdated,
|
2015-01-30 11:02:24 +01:00
|
|
|
this, &LocatorData::onDocumentUpdated);
|
2015-02-03 23:48:57 +02:00
|
|
|
connect(manager, &ModelManagerInterface::aboutToRemoveFiles,
|
2015-01-30 11:02:24 +01:00
|
|
|
this, &LocatorData::onAboutToRemoveFiles);
|
2018-05-08 13:40:45 +02:00
|
|
|
|
|
|
|
|
ProjectExplorer::SessionManager *session = ProjectExplorer::SessionManager::instance();
|
|
|
|
|
if (session)
|
|
|
|
|
connect(session, &ProjectExplorer::SessionManager::projectRemoved,
|
|
|
|
|
[this] (ProjectExplorer::Project*) { m_entries.clear(); });
|
2010-11-16 13:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
2018-11-24 10:22:37 +01:00
|
|
|
LocatorData::~LocatorData() = default;
|
2010-11-16 13:54:50 +01:00
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
class FunctionFinder : protected AST::Visitor
|
|
|
|
|
{
|
|
|
|
|
QList<LocatorData::Entry> m_entries;
|
|
|
|
|
Document::Ptr m_doc;
|
|
|
|
|
QString m_context;
|
|
|
|
|
QString m_documentContext;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
QList<LocatorData::Entry> run(const Document::Ptr &doc)
|
|
|
|
|
{
|
|
|
|
|
m_doc = doc;
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
if (!doc->componentName().isEmpty())
|
2010-11-16 13:54:50 +01:00
|
|
|
m_documentContext = doc->componentName();
|
Remove braces for single lines of conditions
#!/usr/bin/env ruby
Dir.glob('**/*.cpp') { |file|
# skip ast (excluding paste, astpath, and canv'ast'imer)
next if file =~ /ast[^eip]|keywords\.|qualifiers|preprocessor|names.cpp/i
s = File.read(file)
next if s.include?('qlalr')
orig = s.dup
s.gsub!(/\n *if [^\n]*{\n[^\n]*\n\s+}(\s+else if [^\n]* {\n[^\n]*\n\s+})*(\s+else {\n[^\n]*\n\s+})?\n/m) { |m|
res = $&
if res =~ /^\s*(\/\/|[A-Z_]{3,})/ # C++ comment or macro (Q_UNUSED, SDEBUG), do not touch braces
res
else
res.gsub!('} else', 'else')
res.gsub!(/\n +} *\n/m, "\n")
res.gsub(/ *{$/, '')
end
}
s.gsub!(/ *$/, '')
File.open(file, 'wb').write(s) if s != orig
}
Change-Id: I3b30ee60df0986f66c02132c65fc38a3fbb6bbdc
Reviewed-by: hjk <qthjk@ovi.com>
2013-01-08 03:32:53 +02:00
|
|
|
else
|
2019-05-28 13:49:26 +02:00
|
|
|
m_documentContext = Utils::FilePath::fromString(doc->fileName()).fileName();
|
2010-11-16 13:54:50 +01:00
|
|
|
accept(doc->ast(), m_documentContext);
|
|
|
|
|
return m_entries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
QString contextString(const QString &extra)
|
|
|
|
|
{
|
2012-11-21 22:43:22 +02:00
|
|
|
return QString::fromLatin1("%1, %2").arg(extra, m_documentContext);
|
2010-11-16 13:54:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LocatorData::Entry basicEntry(SourceLocation loc)
|
|
|
|
|
{
|
|
|
|
|
LocatorData::Entry entry;
|
|
|
|
|
entry.type = LocatorData::Function;
|
|
|
|
|
entry.extraInfo = m_context;
|
|
|
|
|
entry.fileName = m_doc->fileName();
|
|
|
|
|
entry.line = loc.startLine;
|
|
|
|
|
entry.column = loc.startColumn - 1;
|
|
|
|
|
return entry;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void accept(Node *ast, const QString &context)
|
|
|
|
|
{
|
|
|
|
|
const QString old = m_context;
|
|
|
|
|
m_context = context;
|
|
|
|
|
Node::accept(ast, this);
|
|
|
|
|
m_context = old;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 10:22:37 +01:00
|
|
|
bool visit(FunctionDeclaration *ast) override
|
2010-11-16 13:54:50 +01:00
|
|
|
{
|
|
|
|
|
return visit(static_cast<FunctionExpression *>(ast));
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 10:22:37 +01:00
|
|
|
bool visit(FunctionExpression *ast) override
|
2010-11-16 13:54:50 +01:00
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast->name.isEmpty())
|
2010-11-16 13:54:50 +01:00
|
|
|
return true;
|
|
|
|
|
|
|
|
|
|
LocatorData::Entry entry = basicEntry(ast->identifierToken);
|
|
|
|
|
|
|
|
|
|
entry.type = LocatorData::Function;
|
2011-09-13 09:57:24 +02:00
|
|
|
entry.displayName = ast->name.toString();
|
2010-11-16 13:54:50 +01:00
|
|
|
entry.displayName += QLatin1Char('(');
|
|
|
|
|
for (FormalParameterList *it = ast->formals; it; it = it->next) {
|
|
|
|
|
if (it != ast->formals)
|
|
|
|
|
entry.displayName += QLatin1String(", ");
|
2018-10-16 15:32:58 +02:00
|
|
|
if (!it->element->bindingIdentifier.isEmpty())
|
|
|
|
|
entry.displayName += it->element->bindingIdentifier.toString();
|
2010-11-16 13:54:50 +01:00
|
|
|
}
|
|
|
|
|
entry.displayName += QLatin1Char(')');
|
|
|
|
|
entry.symbolName = entry.displayName;
|
|
|
|
|
|
|
|
|
|
m_entries += entry;
|
|
|
|
|
|
2012-11-21 22:43:22 +02:00
|
|
|
accept(ast->body, contextString(QString::fromLatin1("function %1").arg(entry.displayName)));
|
2010-11-16 13:54:50 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 10:22:37 +01:00
|
|
|
bool visit(UiScriptBinding *ast) override
|
2010-11-16 13:54:50 +01:00
|
|
|
{
|
|
|
|
|
if (!ast->qualifiedId)
|
|
|
|
|
return true;
|
2011-10-07 14:04:06 +02:00
|
|
|
const QString qualifiedIdString = toString(ast->qualifiedId);
|
2010-11-16 13:54:50 +01:00
|
|
|
|
|
|
|
|
if (cast<Block *>(ast->statement)) {
|
|
|
|
|
LocatorData::Entry entry = basicEntry(ast->qualifiedId->identifierToken);
|
|
|
|
|
entry.displayName = qualifiedIdString;
|
|
|
|
|
entry.symbolName = qualifiedIdString;
|
|
|
|
|
m_entries += entry;
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-07 14:04:06 +02:00
|
|
|
accept(ast->statement, contextString(toString(ast->qualifiedId)));
|
2010-11-16 13:54:50 +01:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 10:22:37 +01:00
|
|
|
bool visit(UiObjectBinding *ast) override
|
2010-11-16 13:54:50 +01:00
|
|
|
{
|
|
|
|
|
if (!ast->qualifiedTypeNameId)
|
|
|
|
|
return true;
|
|
|
|
|
|
2011-10-07 14:04:06 +02:00
|
|
|
QString context = toString(ast->qualifiedTypeNameId);
|
2011-10-24 09:28:02 +02:00
|
|
|
const QString id = idOfObject(ast);
|
2010-11-16 13:54:50 +01:00
|
|
|
if (!id.isEmpty())
|
2012-11-21 22:43:22 +02:00
|
|
|
context = QString::fromLatin1("%1 (%2)").arg(id, context);
|
2010-11-16 13:54:50 +01:00
|
|
|
accept(ast->initializer, contextString(context));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-11-24 10:22:37 +01:00
|
|
|
bool visit(UiObjectDefinition *ast) override
|
2010-11-16 13:54:50 +01:00
|
|
|
{
|
|
|
|
|
if (!ast->qualifiedTypeNameId)
|
|
|
|
|
return true;
|
|
|
|
|
|
2011-10-07 14:04:06 +02:00
|
|
|
QString context = toString(ast->qualifiedTypeNameId);
|
2011-10-24 09:28:02 +02:00
|
|
|
const QString id = idOfObject(ast);
|
2010-11-16 13:54:50 +01:00
|
|
|
if (!id.isEmpty())
|
2012-11-21 22:43:22 +02:00
|
|
|
context = QString::fromLatin1("%1 (%2)").arg(id, context);
|
2010-11-16 13:54:50 +01:00
|
|
|
accept(ast->initializer, contextString(context));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2014-12-14 21:13:35 -05:00
|
|
|
|
2018-11-24 10:22:37 +01:00
|
|
|
bool visit(AST::BinaryExpression *ast) override
|
2014-12-14 21:13:35 -05:00
|
|
|
{
|
|
|
|
|
auto fieldExpr = AST::cast<AST::FieldMemberExpression *>(ast->left);
|
|
|
|
|
auto funcExpr = AST::cast<AST::FunctionExpression *>(ast->right);
|
|
|
|
|
|
|
|
|
|
if (fieldExpr && funcExpr && funcExpr->body && (ast->op == QSOperator::Assign)) {
|
|
|
|
|
LocatorData::Entry entry = basicEntry(ast->operatorToken);
|
|
|
|
|
|
|
|
|
|
entry.type = LocatorData::Function;
|
|
|
|
|
entry.displayName = fieldExpr->name.toString();
|
|
|
|
|
while (fieldExpr) {
|
|
|
|
|
if (auto field = AST::cast<AST::FieldMemberExpression *>(fieldExpr->base)) {
|
|
|
|
|
entry.displayName.prepend(field->name.toString() + QLatin1Char('.'));
|
|
|
|
|
fieldExpr = field;
|
|
|
|
|
} else {
|
|
|
|
|
if (auto ident = AST::cast<AST::IdentifierExpression *>(fieldExpr->base))
|
|
|
|
|
entry.displayName.prepend(ident->name.toString() + QLatin1Char('.'));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
entry.displayName += QLatin1Char('(');
|
|
|
|
|
for (FormalParameterList *it = funcExpr->formals; it; it = it->next) {
|
|
|
|
|
if (it != funcExpr->formals)
|
|
|
|
|
entry.displayName += QLatin1String(", ");
|
2018-10-16 15:32:58 +02:00
|
|
|
if (!it->element->bindingIdentifier.isEmpty())
|
|
|
|
|
entry.displayName += it->element->bindingIdentifier.toString();
|
2014-12-14 21:13:35 -05:00
|
|
|
}
|
|
|
|
|
entry.displayName += QLatin1Char(')');
|
|
|
|
|
entry.symbolName = entry.displayName;
|
|
|
|
|
|
|
|
|
|
m_entries += entry;
|
|
|
|
|
|
|
|
|
|
accept(funcExpr->body, contextString(QString::fromLatin1("function %1").arg(entry.displayName)));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2020-02-28 17:51:32 +01:00
|
|
|
|
|
|
|
|
void throwRecursionDepthError() override
|
|
|
|
|
{
|
|
|
|
|
qWarning("Warning: Hit maximum recursion limit visiting AST in FunctionFinder.");
|
|
|
|
|
}
|
2010-11-16 13:54:50 +01:00
|
|
|
};
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
QHash<QString, QList<LocatorData::Entry> > LocatorData::entries() const
|
|
|
|
|
{
|
2014-09-19 13:57:20 +02:00
|
|
|
QMutexLocker l(&m_mutex);
|
2010-11-16 13:54:50 +01:00
|
|
|
return m_entries;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-03 23:48:57 +02:00
|
|
|
void LocatorData::onDocumentUpdated(const Document::Ptr &doc)
|
2010-11-16 13:54:50 +01:00
|
|
|
{
|
|
|
|
|
QList<Entry> entries = FunctionFinder().run(doc);
|
2014-09-19 13:57:20 +02:00
|
|
|
QMutexLocker l(&m_mutex);
|
2010-11-16 13:54:50 +01:00
|
|
|
m_entries.insert(doc->fileName(), entries);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void LocatorData::onAboutToRemoveFiles(const QStringList &files)
|
|
|
|
|
{
|
2014-09-19 13:57:20 +02:00
|
|
|
QMutexLocker l(&m_mutex);
|
2010-11-16 13:54:50 +01:00
|
|
|
foreach (const QString &file, files) {
|
|
|
|
|
m_entries.remove(file);
|
|
|
|
|
}
|
|
|
|
|
}
|