2012-10-02 09:12:39 +02:00
|
|
|
/****************************************************************************
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2013-01-28 17:12:19 +01:00
|
|
|
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
|
2012-10-02 09:12:39 +02:00
|
|
|
** Contact: http://www.qt-project.org/legal
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
** This file is part of Qt Creator.
|
2011-08-16 09:47:54 +02: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
|
|
|
|
|
** a written agreement between you and Digia. For licensing terms and
|
|
|
|
|
** conditions see http://qt.digia.com/licensing. For further information
|
|
|
|
|
** use the contact form at http://qt.digia.com/contact-us.
|
2011-08-16 09:47:54 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
2012-10-02 09:12:39 +02:00
|
|
|
** Alternatively, 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.
|
|
|
|
|
**
|
|
|
|
|
** In addition, as a special exception, Digia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Digia Qt LGPL Exception
|
2011-08-16 09:47:54 +02:00
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
2012-10-02 09:12:39 +02:00
|
|
|
****************************************************************************/
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
#include "qmljssemantichighlighter.h"
|
|
|
|
|
|
|
|
|
|
#include "qmljseditor.h"
|
|
|
|
|
|
|
|
|
|
#include <qmljs/qmljsdocument.h>
|
|
|
|
|
#include <qmljs/qmljsscopechain.h>
|
|
|
|
|
#include <qmljs/qmljsscopebuilder.h>
|
|
|
|
|
#include <qmljs/qmljsevaluate.h>
|
|
|
|
|
#include <qmljs/qmljscontext.h>
|
|
|
|
|
#include <qmljs/qmljsbind.h>
|
2011-10-07 14:04:06 +02:00
|
|
|
#include <qmljs/qmljsutils.h>
|
2011-08-16 09:47:54 +02:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
|
|
|
|
#include <qmljs/parser/qmljsastvisitor_p.h>
|
2013-02-07 00:10:05 +01:00
|
|
|
#include <qmljs/qmljsstaticanalysismessage.h>
|
2011-08-16 09:47:54 +02:00
|
|
|
#include <texteditor/syntaxhighlighter.h>
|
|
|
|
|
#include <texteditor/basetextdocument.h>
|
|
|
|
|
#include <texteditor/texteditorconstants.h>
|
|
|
|
|
#include <texteditor/fontsettings.h>
|
|
|
|
|
#include <utils/qtcassert.h>
|
|
|
|
|
|
2012-02-15 10:42:41 +01:00
|
|
|
#include <QThreadPool>
|
|
|
|
|
#include <QFutureInterface>
|
|
|
|
|
#include <QRunnable>
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJS::AST;
|
|
|
|
|
|
2013-06-03 19:31:32 +02:00
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
|
|
|
|
|
using namespace Internal;
|
|
|
|
|
|
2011-08-16 09:47:54 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
|
class PriorityTask :
|
|
|
|
|
public QFutureInterface<T>,
|
|
|
|
|
public QRunnable
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
typedef QFuture<T> Future;
|
|
|
|
|
|
|
|
|
|
Future start(QThread::Priority priority)
|
|
|
|
|
{
|
|
|
|
|
this->setRunnable(this);
|
|
|
|
|
this->reportStarted();
|
|
|
|
|
Future future = this->future();
|
|
|
|
|
QThreadPool::globalInstance()->start(this, priority);
|
|
|
|
|
return future;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static bool isIdScope(const ObjectValue *scope, const QList<const QmlComponentChain *> &chain)
|
|
|
|
|
{
|
|
|
|
|
foreach (const QmlComponentChain *c, chain) {
|
|
|
|
|
if (c->idScope() == scope)
|
|
|
|
|
return true;
|
|
|
|
|
if (isIdScope(scope, c->instantiatingComponents()))
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CollectStateNames : protected Visitor
|
|
|
|
|
{
|
|
|
|
|
QStringList m_stateNames;
|
|
|
|
|
bool m_inStateType;
|
|
|
|
|
ScopeChain m_scopeChain;
|
2011-10-10 10:32:33 +02:00
|
|
|
const CppComponentValue *m_statePrototype;
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
CollectStateNames(const ScopeChain &scopeChain)
|
|
|
|
|
: m_scopeChain(scopeChain)
|
|
|
|
|
{
|
2011-09-16 13:55:10 +02:00
|
|
|
m_statePrototype = scopeChain.context()->valueOwner()->cppQmlTypes().objectByCppName(QLatin1String("QDeclarativeState"));
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStringList operator()(Node *ast)
|
|
|
|
|
{
|
|
|
|
|
m_stateNames.clear();
|
|
|
|
|
if (!m_statePrototype)
|
|
|
|
|
return m_stateNames;
|
|
|
|
|
m_inStateType = false;
|
|
|
|
|
accept(ast);
|
|
|
|
|
return m_stateNames;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void accept(Node *ast)
|
|
|
|
|
{
|
|
|
|
|
if (ast)
|
|
|
|
|
ast->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool preVisit(Node *ast)
|
|
|
|
|
{
|
|
|
|
|
return ast->uiObjectMemberCast()
|
|
|
|
|
|| cast<UiProgram *>(ast)
|
|
|
|
|
|| cast<UiObjectInitializer *>(ast)
|
|
|
|
|
|| cast<UiObjectMemberList *>(ast)
|
|
|
|
|
|| cast<UiArrayMemberList *>(ast);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool hasStatePrototype(Node *ast)
|
|
|
|
|
{
|
|
|
|
|
Bind *bind = m_scopeChain.document()->bind();
|
|
|
|
|
const ObjectValue *v = bind->findQmlObject(ast);
|
|
|
|
|
if (!v)
|
|
|
|
|
return false;
|
|
|
|
|
PrototypeIterator it(v, m_scopeChain.context());
|
|
|
|
|
while (it.hasNext()) {
|
|
|
|
|
const ObjectValue *proto = it.next();
|
2011-10-10 10:55:37 +02:00
|
|
|
const CppComponentValue *qmlProto = value_cast<CppComponentValue>(proto);
|
2011-08-16 09:47:54 +02:00
|
|
|
if (!qmlProto)
|
|
|
|
|
continue;
|
|
|
|
|
if (qmlProto->metaObject() == m_statePrototype->metaObject())
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(UiObjectDefinition *ast)
|
|
|
|
|
{
|
|
|
|
|
const bool old = m_inStateType;
|
|
|
|
|
m_inStateType = hasStatePrototype(ast);
|
|
|
|
|
accept(ast->initializer);
|
|
|
|
|
m_inStateType = old;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(UiObjectBinding *ast)
|
|
|
|
|
{
|
|
|
|
|
const bool old = m_inStateType;
|
|
|
|
|
m_inStateType = hasStatePrototype(ast);
|
|
|
|
|
accept(ast->initializer);
|
|
|
|
|
m_inStateType = old;
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(UiScriptBinding *ast)
|
|
|
|
|
{
|
|
|
|
|
if (!m_inStateType)
|
|
|
|
|
return false;
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!ast->qualifiedId || ast->qualifiedId->name.isEmpty() || ast->qualifiedId->next)
|
2011-08-16 09:47:54 +02:00
|
|
|
return false;
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast->qualifiedId->name != QLatin1String("name"))
|
2011-08-16 09:47:54 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
ExpressionStatement *expStmt = cast<ExpressionStatement *>(ast->statement);
|
|
|
|
|
if (!expStmt)
|
|
|
|
|
return false;
|
|
|
|
|
StringLiteral *strLit = cast<StringLiteral *>(expStmt->expression);
|
2011-09-13 09:57:24 +02:00
|
|
|
if (!strLit || strLit->value.isEmpty())
|
2011-08-16 09:47:54 +02:00
|
|
|
return false;
|
|
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
m_stateNames += strLit->value.toString();
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
class CollectionTask :
|
|
|
|
|
public PriorityTask<SemanticHighlighter::Use>,
|
|
|
|
|
protected Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
2013-02-07 00:10:05 +01:00
|
|
|
CollectionTask(const QmlJSTools::SemanticInfo &semanticInfo,
|
|
|
|
|
SemanticHighlighter &semanticHighlighter)
|
|
|
|
|
: m_semanticInfo(semanticInfo)
|
|
|
|
|
, m_semanticHighlighter(semanticHighlighter)
|
|
|
|
|
, m_scopeChain(semanticInfo.scopeChain())
|
2011-08-16 09:47:54 +02:00
|
|
|
, m_scopeBuilder(&m_scopeChain)
|
2011-09-01 09:23:08 +02:00
|
|
|
, m_lineOfLastUse(0)
|
2013-02-07 00:10:05 +01:00
|
|
|
, m_nextExtraFormat(SemanticHighlighter::Max)
|
|
|
|
|
, m_currentDelayedUse(0)
|
2011-08-16 09:47:54 +02:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void accept(Node *ast)
|
|
|
|
|
{
|
|
|
|
|
if (ast)
|
|
|
|
|
ast->accept(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void scopedAccept(Node *ast, Node *child)
|
|
|
|
|
{
|
|
|
|
|
m_scopeBuilder.push(ast);
|
|
|
|
|
accept(child);
|
|
|
|
|
m_scopeBuilder.pop();
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
void processName(const QStringRef &name, SourceLocation location)
|
2011-08-16 09:47:54 +02:00
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (name.isEmpty())
|
2011-08-16 09:47:54 +02:00
|
|
|
return;
|
|
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
const QString &nameStr = name.toString();
|
2011-08-16 09:47:54 +02:00
|
|
|
const ObjectValue *scope = 0;
|
|
|
|
|
const Value *value = m_scopeChain.lookup(nameStr, &scope);
|
|
|
|
|
if (!value || !scope)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-09-01 09:23:08 +02:00
|
|
|
SemanticHighlighter::UseType type = SemanticHighlighter::UnknownType;
|
2011-08-16 09:47:54 +02:00
|
|
|
if (m_scopeChain.qmlTypes() == scope) {
|
2011-09-01 09:23:08 +02:00
|
|
|
type = SemanticHighlighter::QmlTypeType;
|
2011-08-16 09:47:54 +02:00
|
|
|
} else if (m_scopeChain.qmlScopeObjects().contains(scope)) {
|
2011-09-01 09:23:08 +02:00
|
|
|
type = SemanticHighlighter::ScopeObjectPropertyType;
|
2011-08-16 09:47:54 +02:00
|
|
|
} else if (m_scopeChain.jsScopes().contains(scope)) {
|
2011-09-01 09:23:08 +02:00
|
|
|
type = SemanticHighlighter::JsScopeType;
|
2011-08-16 09:47:54 +02:00
|
|
|
} else if (m_scopeChain.jsImports() == scope) {
|
2011-09-01 09:23:08 +02:00
|
|
|
type = SemanticHighlighter::JsImportType;
|
2011-08-16 09:47:54 +02:00
|
|
|
} else if (m_scopeChain.globalScope() == scope) {
|
2011-09-01 09:23:08 +02:00
|
|
|
type = SemanticHighlighter::JsGlobalType;
|
|
|
|
|
} else if (QSharedPointer<const QmlComponentChain> chain = m_scopeChain.qmlComponentChain()) {
|
|
|
|
|
if (scope == chain->idScope()) {
|
|
|
|
|
type = SemanticHighlighter::LocalIdType;
|
|
|
|
|
} else if (isIdScope(scope, chain->instantiatingComponents())) {
|
|
|
|
|
type = SemanticHighlighter::ExternalIdType;
|
|
|
|
|
} else if (scope == chain->rootObjectScope()) {
|
|
|
|
|
type = SemanticHighlighter::RootObjectPropertyType;
|
2011-09-14 09:59:05 +02:00
|
|
|
} else { // check for this?
|
|
|
|
|
type = SemanticHighlighter::ExternalObjectPropertyType;
|
2011-09-01 09:23:08 +02:00
|
|
|
}
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
2011-09-14 09:59:05 +02:00
|
|
|
if (type != SemanticHighlighter::UnknownType)
|
|
|
|
|
addUse(location, type);
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
2011-09-12 14:00:05 +02:00
|
|
|
void processTypeId(UiQualifiedId *typeId)
|
|
|
|
|
{
|
|
|
|
|
if (!typeId)
|
|
|
|
|
return;
|
|
|
|
|
if (m_scopeChain.context()->lookupType(m_scopeChain.document().data(), typeId))
|
|
|
|
|
addUse(fullLocationForQualifiedId(typeId), SemanticHighlighter::QmlTypeType);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void processBindingName(UiQualifiedId *localId)
|
|
|
|
|
{
|
|
|
|
|
if (!localId)
|
|
|
|
|
return;
|
|
|
|
|
addUse(fullLocationForQualifiedId(localId), SemanticHighlighter::BindingNameType);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-30 15:04:45 +02:00
|
|
|
bool visit(UiImport *ast)
|
|
|
|
|
{
|
|
|
|
|
processName(ast->importId, ast->importIdToken);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 09:47:54 +02:00
|
|
|
bool visit(UiObjectDefinition *ast)
|
|
|
|
|
{
|
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 (m_scopeChain.document()->bind()->isGroupedPropertyBinding(ast))
|
2011-09-12 14:00:05 +02:00
|
|
|
processBindingName(ast->qualifiedTypeNameId);
|
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
|
2011-09-12 14:00:05 +02:00
|
|
|
processTypeId(ast->qualifiedTypeNameId);
|
2011-08-16 09:47:54 +02:00
|
|
|
scopedAccept(ast, ast->initializer);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(UiObjectBinding *ast)
|
|
|
|
|
{
|
2011-09-12 14:00:05 +02:00
|
|
|
processTypeId(ast->qualifiedTypeNameId);
|
|
|
|
|
processBindingName(ast->qualifiedId);
|
2011-08-16 09:47:54 +02:00
|
|
|
scopedAccept(ast, ast->initializer);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(UiScriptBinding *ast)
|
|
|
|
|
{
|
2011-09-12 14:00:05 +02:00
|
|
|
processBindingName(ast->qualifiedId);
|
2011-08-16 09:47:54 +02:00
|
|
|
scopedAccept(ast, ast->statement);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-12 14:00:05 +02:00
|
|
|
bool visit(UiArrayBinding *ast)
|
|
|
|
|
{
|
|
|
|
|
processBindingName(ast->qualifiedId);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 09:47:54 +02:00
|
|
|
bool visit(UiPublicMember *ast)
|
|
|
|
|
{
|
2011-12-06 14:47:36 +01:00
|
|
|
if (ast->typeToken.isValid() && !ast->memberType.isEmpty()) {
|
|
|
|
|
if (m_scopeChain.context()->lookupType(m_scopeChain.document().data(), QStringList(ast->memberType.toString())))
|
|
|
|
|
addUse(ast->typeToken, SemanticHighlighter::QmlTypeType);
|
|
|
|
|
}
|
2011-09-12 14:00:05 +02:00
|
|
|
if (ast->identifierToken.isValid())
|
|
|
|
|
addUse(ast->identifierToken, SemanticHighlighter::BindingNameType);
|
2011-08-16 09:47:54 +02:00
|
|
|
scopedAccept(ast, ast->statement);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(FunctionExpression *ast)
|
|
|
|
|
{
|
|
|
|
|
processName(ast->name, ast->identifierToken);
|
|
|
|
|
scopedAccept(ast, ast->body);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(FunctionDeclaration *ast)
|
|
|
|
|
{
|
|
|
|
|
return visit(static_cast<FunctionExpression *>(ast));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(VariableDeclaration *ast)
|
|
|
|
|
{
|
|
|
|
|
processName(ast->name, ast->identifierToken);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(IdentifierExpression *ast)
|
|
|
|
|
{
|
|
|
|
|
processName(ast->name, ast->identifierToken);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(StringLiteral *ast)
|
|
|
|
|
{
|
2011-09-13 09:57:24 +02:00
|
|
|
if (ast->value.isEmpty())
|
2011-08-16 09:47:54 +02:00
|
|
|
return false;
|
|
|
|
|
|
2011-09-13 09:57:24 +02:00
|
|
|
const QString &value = ast->value.toString();
|
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 (m_stateNames.contains(value))
|
2011-09-01 09:23:08 +02:00
|
|
|
addUse(ast->literalToken, SemanticHighlighter::LocalStateNameType);
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 00:10:05 +01:00
|
|
|
void addMessages(QList<QmlJS::DiagnosticMessage> messages,
|
|
|
|
|
const Document::Ptr &doc)
|
|
|
|
|
{
|
|
|
|
|
foreach (const DiagnosticMessage &d, messages) {
|
|
|
|
|
int line = d.loc.startLine;
|
|
|
|
|
int column = qMax(1U, d.loc.startColumn);
|
|
|
|
|
int length = d.loc.length;
|
|
|
|
|
int begin = d.loc.begin();
|
|
|
|
|
|
|
|
|
|
if (d.loc.length == 0) {
|
|
|
|
|
QString source(doc->source());
|
|
|
|
|
int end = begin;
|
|
|
|
|
if (begin == source.size() || source.at(begin) == QLatin1Char('\n')
|
|
|
|
|
|| source.at(begin) == QLatin1Char('\r')) {
|
|
|
|
|
while (begin > end - column && !source.at(--begin).isSpace()) { }
|
|
|
|
|
} else {
|
|
|
|
|
while (end < source.size() && source.at(++end).isLetterOrNumber()) { }
|
|
|
|
|
}
|
|
|
|
|
column += begin - d.loc.begin();
|
|
|
|
|
length = end-begin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextCharFormat format;
|
|
|
|
|
if (d.isWarning())
|
|
|
|
|
format.setUnderlineColor(Qt::darkYellow);
|
|
|
|
|
else
|
|
|
|
|
format.setUnderlineColor(Qt::red);
|
|
|
|
|
|
|
|
|
|
format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
|
|
|
|
format.setToolTip(d.message);
|
|
|
|
|
|
|
|
|
|
collectRanges(begin, length, format);
|
|
|
|
|
addDelayedUse(SemanticHighlighter::Use(line, column, length, addFormat(format)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void addMessages(const QList<QmlJS::StaticAnalysis::Message> &messages,
|
|
|
|
|
const Document::Ptr &doc)
|
|
|
|
|
{
|
|
|
|
|
foreach (const QmlJS::StaticAnalysis::Message &d, messages) {
|
|
|
|
|
int line = d.location.startLine;
|
|
|
|
|
int column = qMax(1U, d.location.startColumn);
|
|
|
|
|
int length = d.location.length;
|
|
|
|
|
int begin = d.location.begin();
|
|
|
|
|
|
|
|
|
|
if (d.location.length == 0) {
|
|
|
|
|
QString source(doc->source());
|
|
|
|
|
int end = begin;
|
|
|
|
|
if (begin == source.size() || source.at(begin) == QLatin1Char('\n')
|
|
|
|
|
|| source.at(begin) == QLatin1Char('\r')) {
|
|
|
|
|
while (begin > end - column && !source.at(--begin).isSpace()) { }
|
|
|
|
|
} else {
|
|
|
|
|
while (end < source.size() && source.at(++end).isLetterOrNumber()) { }
|
|
|
|
|
}
|
|
|
|
|
column += begin - d.location.begin();
|
|
|
|
|
length = end-begin;
|
|
|
|
|
}
|
|
|
|
|
QTextCharFormat format;
|
2013-10-16 14:59:28 +02:00
|
|
|
if (d.severity == Severity::Warning || d.severity == Severity::MaybeWarning)
|
2013-02-07 00:10:05 +01:00
|
|
|
format.setUnderlineColor(Qt::darkYellow);
|
2013-10-16 14:59:28 +02:00
|
|
|
else if (d.severity == Severity::Error || d.severity == Severity::MaybeError)
|
2013-02-07 00:10:05 +01:00
|
|
|
format.setUnderlineColor(Qt::red);
|
2013-10-16 14:59:28 +02:00
|
|
|
else if (d.severity == Severity::Hint)
|
2013-02-07 00:10:05 +01:00
|
|
|
format.setUnderlineColor(Qt::darkGreen);
|
|
|
|
|
|
|
|
|
|
format.setUnderlineStyle(QTextCharFormat::WaveUnderline);
|
|
|
|
|
format.setToolTip(d.message);
|
|
|
|
|
|
|
|
|
|
collectRanges(begin, length, format);
|
|
|
|
|
addDelayedUse(SemanticHighlighter::Use(line, column, length, addFormat(format)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 09:47:54 +02:00
|
|
|
private:
|
|
|
|
|
void run()
|
|
|
|
|
{
|
2013-03-05 14:35:15 +01:00
|
|
|
int nMessages = 0;
|
|
|
|
|
if (Document::isFullySupportedLanguage(m_scopeChain.document()->language())) {
|
|
|
|
|
nMessages = m_scopeChain.document()->diagnosticMessages().size()
|
|
|
|
|
+ m_semanticInfo.semanticMessages.size()
|
|
|
|
|
+ m_semanticInfo.staticAnalysisMessages.size();
|
|
|
|
|
m_delayedUses.reserve(nMessages);
|
|
|
|
|
m_diagnosticRanges.reserve(nMessages);
|
|
|
|
|
m_extraFormats.reserve(nMessages);
|
|
|
|
|
addMessages(m_scopeChain.document()->diagnosticMessages(), m_scopeChain.document());
|
|
|
|
|
addMessages(m_semanticInfo.semanticMessages, m_semanticInfo.document);
|
|
|
|
|
addMessages(m_semanticInfo.staticAnalysisMessages, m_semanticInfo.document);
|
|
|
|
|
|
|
|
|
|
qSort(m_delayedUses.begin(), m_delayedUses.end(), sortByLinePredicate);
|
|
|
|
|
}
|
2013-02-07 00:10:05 +01:00
|
|
|
m_currentDelayedUse = 0;
|
|
|
|
|
|
|
|
|
|
m_semanticHighlighter.reportMessagesInfo(m_diagnosticRanges, m_extraFormats);
|
|
|
|
|
|
2011-08-16 09:47:54 +02:00
|
|
|
Node *root = m_scopeChain.document()->ast();
|
|
|
|
|
m_stateNames = CollectStateNames(m_scopeChain)(root);
|
|
|
|
|
accept(root);
|
2013-02-07 00:10:05 +01:00
|
|
|
while (m_currentDelayedUse < m_delayedUses.size())
|
|
|
|
|
m_uses.append(m_delayedUses.value(m_currentDelayedUse++));
|
2011-09-01 09:23:08 +02:00
|
|
|
flush();
|
2011-08-16 09:47:54 +02:00
|
|
|
reportFinished();
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-01 09:23:08 +02:00
|
|
|
void addUse(const SourceLocation &location, SemanticHighlighter::UseType type)
|
|
|
|
|
{
|
|
|
|
|
addUse(SemanticHighlighter::Use(location.startLine, location.startColumn, location.length, type));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static const int chunkSize = 50;
|
|
|
|
|
|
|
|
|
|
void addUse(const SemanticHighlighter::Use &use)
|
|
|
|
|
{
|
2013-02-07 00:10:05 +01:00
|
|
|
while (m_currentDelayedUse < m_delayedUses.size()
|
|
|
|
|
&& m_delayedUses.value(m_currentDelayedUse).line < use.line)
|
|
|
|
|
m_uses.append(m_delayedUses.value(m_currentDelayedUse++));
|
|
|
|
|
|
2011-09-01 09:23:08 +02:00
|
|
|
if (m_uses.size() >= chunkSize) {
|
|
|
|
|
if (use.line > m_lineOfLastUse)
|
|
|
|
|
flush();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_lineOfLastUse = qMax(m_lineOfLastUse, use.line);
|
|
|
|
|
m_uses.append(use);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 00:10:05 +01:00
|
|
|
void addDelayedUse(const SemanticHighlighter::Use &use)
|
|
|
|
|
{
|
|
|
|
|
m_delayedUses.append(use);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int addFormat(const QTextCharFormat &format)
|
|
|
|
|
{
|
|
|
|
|
int res = m_nextExtraFormat++;
|
|
|
|
|
m_extraFormats.insert(res, format);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void collectRanges(int start, int length, const QTextCharFormat &format) {
|
|
|
|
|
QTextLayout::FormatRange range;
|
|
|
|
|
range.start = start;
|
|
|
|
|
range.length = length;
|
|
|
|
|
range.format = format;
|
|
|
|
|
m_diagnosticRanges.append(range);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-01 09:23:08 +02:00
|
|
|
static bool sortByLinePredicate(const SemanticHighlighter::Use &lhs, const SemanticHighlighter::Use &rhs)
|
|
|
|
|
{
|
|
|
|
|
return lhs.line < rhs.line;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void flush()
|
|
|
|
|
{
|
|
|
|
|
m_lineOfLastUse = 0;
|
|
|
|
|
|
|
|
|
|
if (m_uses.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
qSort(m_uses.begin(), m_uses.end(), sortByLinePredicate);
|
|
|
|
|
reportResults(m_uses);
|
|
|
|
|
m_uses.clear();
|
|
|
|
|
m_uses.reserve(chunkSize);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 00:10:05 +01:00
|
|
|
const QmlJSTools::SemanticInfo &m_semanticInfo;
|
|
|
|
|
SemanticHighlighter &m_semanticHighlighter;
|
2011-08-16 09:47:54 +02:00
|
|
|
ScopeChain m_scopeChain;
|
|
|
|
|
ScopeBuilder m_scopeBuilder;
|
|
|
|
|
QStringList m_stateNames;
|
2011-09-01 09:23:08 +02:00
|
|
|
QVector<SemanticHighlighter::Use> m_uses;
|
|
|
|
|
unsigned m_lineOfLastUse;
|
2013-02-07 00:10:05 +01:00
|
|
|
QVector<SemanticHighlighter::Use> m_delayedUses;
|
|
|
|
|
int m_nextExtraFormat;
|
|
|
|
|
int m_currentDelayedUse;
|
|
|
|
|
QHash<int, QTextCharFormat> m_extraFormats;
|
|
|
|
|
QVector<QTextLayout::FormatRange> m_diagnosticRanges;
|
2011-08-16 09:47:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
|
|
SemanticHighlighter::SemanticHighlighter(QmlJSTextEditorWidget *editor)
|
|
|
|
|
: QObject(editor)
|
|
|
|
|
, m_editor(editor)
|
|
|
|
|
, m_startRevision(0)
|
|
|
|
|
{
|
|
|
|
|
connect(&m_watcher, SIGNAL(resultsReadyAt(int,int)),
|
|
|
|
|
this, SLOT(applyResults(int,int)));
|
|
|
|
|
connect(&m_watcher, SIGNAL(finished()),
|
|
|
|
|
this, SLOT(finished()));
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-07 00:10:05 +01:00
|
|
|
void SemanticHighlighter::rerun(const QmlJSTools::SemanticInfo &semanticInfo)
|
2011-08-16 09:47:54 +02:00
|
|
|
{
|
|
|
|
|
m_watcher.cancel();
|
|
|
|
|
|
|
|
|
|
// this does not simply use QtConcurrentRun because we want a low-priority future
|
|
|
|
|
// the thread pool deletes the task when it is done
|
2013-02-07 00:10:05 +01:00
|
|
|
CollectionTask::Future f = (new CollectionTask(semanticInfo, *this))->start(QThread::LowestPriority);
|
2011-08-16 09:47:54 +02:00
|
|
|
m_startRevision = m_editor->editorRevision();
|
|
|
|
|
m_watcher.setFuture(f);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-12 12:58:59 +02:00
|
|
|
void SemanticHighlighter::cancel()
|
|
|
|
|
{
|
|
|
|
|
m_watcher.cancel();
|
|
|
|
|
}
|
|
|
|
|
|
2011-08-16 09:47:54 +02:00
|
|
|
void SemanticHighlighter::applyResults(int from, int to)
|
|
|
|
|
{
|
|
|
|
|
if (m_watcher.isCanceled())
|
|
|
|
|
return;
|
|
|
|
|
if (m_startRevision != m_editor->editorRevision())
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-09 13:31:15 +08:00
|
|
|
TextEditor::BaseTextDocument *baseTextDocument = m_editor->baseTextDocument().data();
|
2011-08-16 09:47:54 +02:00
|
|
|
QTC_ASSERT(baseTextDocument, return);
|
|
|
|
|
TextEditor::SyntaxHighlighter *highlighter = qobject_cast<TextEditor::SyntaxHighlighter *>(baseTextDocument->syntaxHighlighter());
|
|
|
|
|
QTC_ASSERT(highlighter, return);
|
|
|
|
|
|
|
|
|
|
TextEditor::SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
|
2013-02-07 00:10:05 +01:00
|
|
|
highlighter, m_watcher.future(), from, to, m_extraFormats);
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticHighlighter::finished()
|
|
|
|
|
{
|
|
|
|
|
if (m_watcher.isCanceled())
|
|
|
|
|
return;
|
|
|
|
|
if (m_startRevision != m_editor->editorRevision())
|
|
|
|
|
return;
|
|
|
|
|
|
2013-01-09 13:31:15 +08:00
|
|
|
TextEditor::BaseTextDocument *baseTextDocument = m_editor->baseTextDocument().data();
|
2011-08-16 09:47:54 +02:00
|
|
|
QTC_ASSERT(baseTextDocument, return);
|
|
|
|
|
TextEditor::SyntaxHighlighter *highlighter = qobject_cast<TextEditor::SyntaxHighlighter *>(baseTextDocument->syntaxHighlighter());
|
|
|
|
|
QTC_ASSERT(highlighter, return);
|
2013-02-07 00:10:05 +01:00
|
|
|
m_editor->m_diagnosticRanges = m_diagnosticRanges;
|
2011-08-16 09:47:54 +02:00
|
|
|
|
|
|
|
|
TextEditor::SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
|
|
|
|
|
highlighter, m_watcher.future());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void SemanticHighlighter::updateFontSettings(const TextEditor::FontSettings &fontSettings)
|
|
|
|
|
{
|
2012-04-26 14:17:42 +02:00
|
|
|
m_formats[LocalIdType] = fontSettings.toTextCharFormat(TextEditor::C_QML_LOCAL_ID);
|
|
|
|
|
m_formats[ExternalIdType] = fontSettings.toTextCharFormat(TextEditor::C_QML_EXTERNAL_ID);
|
|
|
|
|
m_formats[QmlTypeType] = fontSettings.toTextCharFormat(TextEditor::C_QML_TYPE_ID);
|
|
|
|
|
m_formats[RootObjectPropertyType] = fontSettings.toTextCharFormat(TextEditor::C_QML_ROOT_OBJECT_PROPERTY);
|
|
|
|
|
m_formats[ScopeObjectPropertyType] = fontSettings.toTextCharFormat(TextEditor::C_QML_SCOPE_OBJECT_PROPERTY);
|
|
|
|
|
m_formats[ExternalObjectPropertyType] = fontSettings.toTextCharFormat(TextEditor::C_QML_EXTERNAL_OBJECT_PROPERTY);
|
|
|
|
|
m_formats[JsScopeType] = fontSettings.toTextCharFormat(TextEditor::C_JS_SCOPE_VAR);
|
|
|
|
|
m_formats[JsImportType] = fontSettings.toTextCharFormat(TextEditor::C_JS_IMPORT_VAR);
|
|
|
|
|
m_formats[JsGlobalType] = fontSettings.toTextCharFormat(TextEditor::C_JS_GLOBAL_VAR);
|
|
|
|
|
m_formats[LocalStateNameType] = fontSettings.toTextCharFormat(TextEditor::C_QML_STATE_NAME);
|
|
|
|
|
m_formats[BindingNameType] = fontSettings.toTextCharFormat(TextEditor::C_BINDING);
|
|
|
|
|
m_formats[FieldType] = fontSettings.toTextCharFormat(TextEditor::C_FIELD);
|
2011-08-16 09:47:54 +02:00
|
|
|
}
|
|
|
|
|
|
2013-02-07 00:10:05 +01:00
|
|
|
void SemanticHighlighter::reportMessagesInfo(const QVector<QTextLayout::FormatRange> &diagnosticRanges,
|
|
|
|
|
const QHash<int,QTextCharFormat> &formats)
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
// tricky usage of m_extraFormats and diagnosticMessages we call this in another thread...
|
|
|
|
|
// but will use them only after a signal sent by that same thread, maybe we should transfer
|
|
|
|
|
// them more explicitly
|
|
|
|
|
m_extraFormats = formats;
|
|
|
|
|
m_extraFormats.unite(m_formats);
|
|
|
|
|
m_diagnosticRanges = diagnosticRanges;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-12 12:58:59 +02:00
|
|
|
int SemanticHighlighter::startRevision() const
|
|
|
|
|
{
|
|
|
|
|
return m_startRevision;
|
|
|
|
|
}
|
2013-06-03 19:31:32 +02:00
|
|
|
|
|
|
|
|
} // namespace QmlJSEditor
|