2010-07-12 09:44:42 +02:00
|
|
|
#include "qmloutlinemodel.h"
|
|
|
|
|
#include <qmljs/parser/qmljsastvisitor_p.h>
|
2010-07-13 17:05:47 +02:00
|
|
|
#include <qmljs/qmljsinterpreter.h>
|
|
|
|
|
#include <qmljs/qmljslookupcontext.h>
|
2010-07-12 09:44:42 +02:00
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
#include <coreplugin/icore.h>
|
2010-07-12 09:44:42 +02:00
|
|
|
#include <QtCore/QDebug>
|
2010-07-13 17:05:47 +02:00
|
|
|
#include <QtCore/QTime>
|
2010-07-12 09:44:42 +02:00
|
|
|
#include <typeinfo>
|
|
|
|
|
|
|
|
|
|
using namespace QmlJS;
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
|
|
|
|
|
|
|
|
|
enum {
|
|
|
|
|
debug = false
|
|
|
|
|
};
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
namespace QmlJSEditor {
|
|
|
|
|
namespace Internal {
|
2010-07-12 09:44:42 +02:00
|
|
|
|
|
|
|
|
class QmlOutlineModelSync : protected AST::Visitor
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
QmlOutlineModelSync(QmlOutlineModel *model) :
|
|
|
|
|
m_model(model),
|
|
|
|
|
indent(0)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
void operator()(Document::Ptr doc, const Snapshot &snapshot)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
|
|
|
|
m_nodeToIndex.clear();
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
// Set up lookup context once to do the element type lookup
|
|
|
|
|
//
|
|
|
|
|
// We're simplifying here by using the root context everywhere
|
|
|
|
|
// (empty node list). However, creating the LookupContext is quite expensive (about 3ms),
|
|
|
|
|
// and there is AFAIK no way to introduce new type names in a sub-context.
|
|
|
|
|
m_context = LookupContext::create(doc, snapshot, QList<AST::Node*>());
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel ------";
|
|
|
|
|
if (doc && doc->ast())
|
|
|
|
|
doc->ast()->accept(this);
|
2010-07-13 17:05:47 +02:00
|
|
|
|
|
|
|
|
m_context.clear();
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
bool preVisit(AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
if (!node)
|
|
|
|
|
return false;
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel -" << QByteArray(indent++, '-').constData() << node << typeid(*node).name();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void postVisit(AST::Node *)
|
|
|
|
|
{
|
|
|
|
|
indent--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QString asString(AST::UiQualifiedId *id)
|
|
|
|
|
{
|
|
|
|
|
QString text;
|
|
|
|
|
for (; id; id = id->next) {
|
|
|
|
|
if (id->name)
|
|
|
|
|
text += id->name->asString();
|
|
|
|
|
else
|
|
|
|
|
text += QLatin1Char('?');
|
|
|
|
|
|
|
|
|
|
if (id->next)
|
|
|
|
|
text += QLatin1Char('.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 15:51:26 +02:00
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
typedef QPair<QString,QString> ElementType;
|
2010-07-12 09:44:42 +02:00
|
|
|
bool visit(AST::UiObjectDefinition *objDef)
|
|
|
|
|
{
|
2010-07-13 12:21:41 +02:00
|
|
|
if (!validElement(objDef)) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
AST::SourceLocation location;
|
|
|
|
|
location.offset = objDef->firstSourceLocation().offset;
|
|
|
|
|
location.length = objDef->lastSourceLocation().offset
|
|
|
|
|
- objDef->firstSourceLocation().offset
|
|
|
|
|
+ objDef->lastSourceLocation().length;
|
|
|
|
|
|
2010-07-12 15:51:26 +02:00
|
|
|
const QString typeName = asString(objDef->qualifiedTypeNameId);
|
2010-07-13 17:05:47 +02:00
|
|
|
|
|
|
|
|
if (!m_typeToIcon.contains(typeName)) {
|
|
|
|
|
m_typeToIcon.insert(typeName, getIcon(objDef));
|
|
|
|
|
}
|
|
|
|
|
const QIcon icon = m_typeToIcon.value(typeName);
|
|
|
|
|
QString id = getId(objDef);
|
|
|
|
|
|
|
|
|
|
QModelIndex index = m_model->enterElement(typeName, id, icon, location);
|
2010-07-12 09:44:42 +02:00
|
|
|
m_nodeToIndex.insert(objDef, index);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 12:21:41 +02:00
|
|
|
void endVisit(AST::UiObjectDefinition *objDef)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
2010-07-13 12:21:41 +02:00
|
|
|
if (!validElement(objDef)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2010-07-12 09:44:42 +02:00
|
|
|
m_model->leaveElement();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool visit(AST::UiScriptBinding *scriptBinding)
|
|
|
|
|
{
|
|
|
|
|
AST::SourceLocation location;
|
|
|
|
|
location.offset = scriptBinding->firstSourceLocation().offset;
|
|
|
|
|
location.length = scriptBinding->lastSourceLocation().offset
|
|
|
|
|
- scriptBinding->firstSourceLocation().offset
|
|
|
|
|
+ scriptBinding->lastSourceLocation().length;
|
|
|
|
|
|
|
|
|
|
QModelIndex index = m_model->enterProperty(asString(scriptBinding->qualifiedId), location);
|
|
|
|
|
m_nodeToIndex.insert(scriptBinding, index);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void endVisit(AST::UiScriptBinding * /*scriptBinding*/)
|
|
|
|
|
{
|
|
|
|
|
m_model->leaveProperty();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 12:21:41 +02:00
|
|
|
bool validElement(AST::UiObjectDefinition *objDef) {
|
2010-07-13 17:05:47 +02:00
|
|
|
// For 'Rectangle { id }', id is parsed as UiObjectDefinition ... Filter this out.
|
2010-07-13 12:21:41 +02:00
|
|
|
return objDef->qualifiedTypeNameId->name->asString().at(0).isUpper();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
QIcon getIcon(AST::UiObjectDefinition *objDef) {
|
|
|
|
|
const QmlJS::Interpreter::Value *value = m_context->evaluate(objDef->qualifiedTypeNameId);
|
|
|
|
|
|
|
|
|
|
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
|
|
|
|
|
do {
|
|
|
|
|
QString module;
|
|
|
|
|
QString typeName;
|
|
|
|
|
if (const Interpreter::QmlObjectValue *qmlObjectValue =
|
|
|
|
|
dynamic_cast<const Interpreter::QmlObjectValue*>(objectValue)) {
|
|
|
|
|
module = qmlObjectValue->packageName();
|
|
|
|
|
}
|
|
|
|
|
typeName = objectValue->className();
|
|
|
|
|
|
|
|
|
|
QIcon icon = m_model->m_icons->icon(module, typeName);
|
|
|
|
|
if (! icon.isNull())
|
|
|
|
|
return icon;
|
|
|
|
|
|
|
|
|
|
objectValue = objectValue->prototype(m_context->context());
|
|
|
|
|
} while (objectValue);
|
|
|
|
|
}
|
|
|
|
|
return QIcon();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-12 15:51:26 +02:00
|
|
|
QString getId(AST::UiObjectDefinition *objDef) {
|
|
|
|
|
QString id;
|
|
|
|
|
for (AST::UiObjectMemberList *it = objDef->initializer->members; it; it = it->next) {
|
|
|
|
|
if (AST::UiScriptBinding *binding = dynamic_cast<AST::UiScriptBinding*>(it->member)) {
|
|
|
|
|
if (binding->qualifiedId->name->asString() == "id") {
|
|
|
|
|
AST::ExpressionStatement *expr = dynamic_cast<AST::ExpressionStatement*>(binding->statement);
|
|
|
|
|
if (!expr)
|
|
|
|
|
continue;
|
|
|
|
|
AST::IdentifierExpression *idExpr = dynamic_cast<AST::IdentifierExpression*>(expr->expression);
|
|
|
|
|
if (!idExpr)
|
|
|
|
|
continue;
|
|
|
|
|
id = idExpr->name->asString();
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
QmlOutlineModel *m_model;
|
2010-07-13 17:05:47 +02:00
|
|
|
LookupContext::Ptr m_context;
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
QHash<AST::Node*, QModelIndex> m_nodeToIndex;
|
2010-07-13 17:05:47 +02:00
|
|
|
QHash<QString, QIcon> m_typeToIcon;
|
2010-07-12 09:44:42 +02:00
|
|
|
int indent;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
QmlOutlineModel::QmlOutlineModel(QObject *parent) :
|
|
|
|
|
QStandardItemModel(parent)
|
|
|
|
|
{
|
2010-07-13 17:05:47 +02:00
|
|
|
m_icons = Icons::instance();
|
|
|
|
|
const QString resourcePath = Core::ICore::instance()->resourcePath();
|
|
|
|
|
QmlJS::Icons::instance()->setIconFilesPath(resourcePath + "/qmlicons");
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-13 11:18:42 +02:00
|
|
|
QmlJS::Document::Ptr QmlOutlineModel::document() const
|
|
|
|
|
{
|
|
|
|
|
return m_document;
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
void QmlOutlineModel::update(QmlJS::Document::Ptr doc, const QmlJS::Snapshot &snapshot)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
2010-07-13 11:18:42 +02:00
|
|
|
m_document = doc;
|
|
|
|
|
|
2010-07-12 09:44:42 +02:00
|
|
|
m_treePos.clear();
|
|
|
|
|
m_treePos.append(0);
|
|
|
|
|
m_currentItem = invisibleRootItem();
|
|
|
|
|
|
|
|
|
|
QmlOutlineModelSync syncModel(this);
|
2010-07-13 17:05:47 +02:00
|
|
|
syncModel(doc, snapshot);
|
2010-07-12 14:45:22 +02:00
|
|
|
|
|
|
|
|
emit updated();
|
2010-07-12 09:44:42 +02:00
|
|
|
}
|
|
|
|
|
|
2010-07-13 17:05:47 +02:00
|
|
|
QModelIndex QmlOutlineModel::enterElement(const QString &type, const QString &id, const QIcon &icon, const AST::SourceLocation &sourceLocation)
|
2010-07-12 09:44:42 +02:00
|
|
|
{
|
|
|
|
|
QStandardItem *item = enterNode(sourceLocation);
|
2010-07-12 15:51:26 +02:00
|
|
|
if (!id.isEmpty()) {
|
|
|
|
|
item->setText(id);
|
|
|
|
|
} else {
|
|
|
|
|
item->setText(type);
|
|
|
|
|
}
|
2010-07-13 17:05:47 +02:00
|
|
|
item->setIcon(icon);
|
2010-07-12 15:51:26 +02:00
|
|
|
item->setToolTip(type);
|
2010-07-12 09:44:42 +02:00
|
|
|
return item->index();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineModel::leaveElement()
|
|
|
|
|
{
|
|
|
|
|
leaveNode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QModelIndex QmlOutlineModel::enterProperty(const QString &name, const AST::SourceLocation &sourceLocation)
|
|
|
|
|
{
|
|
|
|
|
QStandardItem *item = enterNode(sourceLocation);
|
|
|
|
|
item->setText(name);
|
2010-07-13 17:05:47 +02:00
|
|
|
item->setIcon(m_icons->scriptBindingIcon());
|
2010-07-12 09:44:42 +02:00
|
|
|
return item->index();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineModel::leaveProperty()
|
|
|
|
|
{
|
|
|
|
|
leaveNode();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStandardItem *QmlOutlineModel::enterNode(const QmlJS::AST::SourceLocation &location)
|
|
|
|
|
{
|
|
|
|
|
int siblingIndex = m_treePos.last();
|
|
|
|
|
if (siblingIndex == 0) {
|
|
|
|
|
// first child
|
|
|
|
|
if (!m_currentItem->hasChildren()) {
|
|
|
|
|
QStandardItem *parentItem = m_currentItem;
|
|
|
|
|
m_currentItem = new QStandardItem;
|
|
|
|
|
m_currentItem->setEditable(false);
|
|
|
|
|
parentItem->appendRow(m_currentItem);
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel - Adding" << "element to" << parentItem->text();
|
|
|
|
|
} else {
|
|
|
|
|
m_currentItem = m_currentItem->child(0);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// sibling
|
|
|
|
|
if (m_currentItem->rowCount() <= siblingIndex) {
|
|
|
|
|
// attach
|
|
|
|
|
QStandardItem *oldItem = m_currentItem;
|
|
|
|
|
m_currentItem = new QStandardItem;
|
|
|
|
|
m_currentItem->setEditable(false);
|
|
|
|
|
oldItem->appendRow(m_currentItem);
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel - Adding" << "element to" << oldItem->text();
|
|
|
|
|
} else {
|
|
|
|
|
m_currentItem = m_currentItem->child(siblingIndex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_treePos.append(0);
|
|
|
|
|
m_currentItem->setData(QVariant::fromValue(location), SourceLocationRole);
|
|
|
|
|
|
|
|
|
|
return m_currentItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlOutlineModel::leaveNode()
|
|
|
|
|
{
|
|
|
|
|
int lastIndex = m_treePos.takeLast();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (lastIndex > 0) {
|
|
|
|
|
// element has children
|
|
|
|
|
if (lastIndex < m_currentItem->rowCount()) {
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel - removeRows from " << m_currentItem->text() << lastIndex << m_currentItem->rowCount() - lastIndex;
|
|
|
|
|
m_currentItem->removeRows(lastIndex, m_currentItem->rowCount() - lastIndex);
|
|
|
|
|
}
|
|
|
|
|
m_currentItem = parentItem();
|
|
|
|
|
} else {
|
|
|
|
|
if (m_currentItem->hasChildren()) {
|
|
|
|
|
if (debug)
|
|
|
|
|
qDebug() << "QmlOutlineModel - removeRows from " << m_currentItem->text() << 0 << m_currentItem->rowCount();
|
|
|
|
|
m_currentItem->removeRows(0, m_currentItem->rowCount());
|
|
|
|
|
}
|
|
|
|
|
m_currentItem = parentItem();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
m_treePos.last()++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QStandardItem *QmlOutlineModel::parentItem()
|
|
|
|
|
{
|
|
|
|
|
QStandardItem *parent = m_currentItem->parent();
|
|
|
|
|
if (!parent)
|
|
|
|
|
parent = invisibleRootItem();
|
|
|
|
|
return parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace Internal
|
|
|
|
|
} // namespace QmlJSEditor
|