forked from qt-creator/qt-creator
QmlOutline: Show sensible tooltips
Show the same tooltips as in the text editor. To accomplish this I refactored the QmlOutlineModel quite a bit ...
This commit is contained in:
@@ -826,8 +826,6 @@ void QmlJSTextEditor::onDocumentUpdated(QmlJS::Document::Ptr doc)
|
|||||||
|
|
||||||
const SemanticHighlighter::Source source = currentSource(/*force = */ true);
|
const SemanticHighlighter::Source source = currentSource(/*force = */ true);
|
||||||
m_semanticHighlighter->rehighlight(source);
|
m_semanticHighlighter->rehighlight(source);
|
||||||
|
|
||||||
m_updateOutlineTimer->start();
|
|
||||||
} else {
|
} else {
|
||||||
// show parsing errors
|
// show parsing errors
|
||||||
QList<QTextEdit::ExtraSelection> selections;
|
QList<QTextEdit::ExtraSelection> selections;
|
||||||
@@ -860,18 +858,15 @@ void QmlJSTextEditor::jumpToOutlineElement(int /*index*/)
|
|||||||
|
|
||||||
void QmlJSTextEditor::updateOutlineNow()
|
void QmlJSTextEditor::updateOutlineNow()
|
||||||
{
|
{
|
||||||
const Snapshot snapshot = m_modelManager->snapshot();
|
if (!m_semanticInfo.document)
|
||||||
Document::Ptr document = snapshot.document(file()->fileName());
|
|
||||||
|
|
||||||
if (!document)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (document->editorRevision() != editorRevision()) {
|
if (m_semanticInfo.document->editorRevision() != editorRevision()) {
|
||||||
m_updateOutlineTimer->start();
|
m_updateOutlineTimer->start();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_outlineModel->update(document, snapshot);
|
m_outlineModel->update(m_semanticInfo);
|
||||||
|
|
||||||
QTreeView *treeView = static_cast<QTreeView*>(m_outlineCombo->view());
|
QTreeView *treeView = static_cast<QTreeView*>(m_outlineCombo->view());
|
||||||
treeView->expandAll();
|
treeView->expandAll();
|
||||||
@@ -1590,6 +1585,9 @@ void QmlJSTextEditor::updateSemanticInfo(const SemanticInfo &semanticInfo)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// update outline
|
||||||
|
m_updateOutlineTimer->start();
|
||||||
|
|
||||||
// update warning/error extra selections
|
// update warning/error extra selections
|
||||||
QList<QTextEdit::ExtraSelection> selections;
|
QList<QTextEdit::ExtraSelection> selections;
|
||||||
appendExtraSelectionsForMessages(&selections, doc->diagnosticMessages(), document());
|
appendExtraSelectionsForMessages(&selections, doc->diagnosticMessages(), document());
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#include "qmloutlinemodel.h"
|
#include "qmloutlinemodel.h"
|
||||||
|
#include "qmljseditor.h"
|
||||||
#include <qmljs/parser/qmljsastvisitor_p.h>
|
#include <qmljs/parser/qmljsastvisitor_p.h>
|
||||||
#include <qmljs/qmljsinterpreter.h>
|
#include <qmljs/qmljsinterpreter.h>
|
||||||
#include <qmljs/qmljslookupcontext.h>
|
#include <qmljs/qmljslookupcontext.h>
|
||||||
@@ -18,6 +19,32 @@ enum {
|
|||||||
namespace QmlJSEditor {
|
namespace QmlJSEditor {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
|
QmlOutlineItem::QmlOutlineItem(QmlOutlineModel *model) :
|
||||||
|
m_outlineModel(model)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
QVariant QmlOutlineItem::data(int role) const
|
||||||
|
{
|
||||||
|
if (role == Qt::ToolTipRole) {
|
||||||
|
AST::SourceLocation location = data(QmlOutlineModel::SourceLocationRole).value<AST::SourceLocation>();
|
||||||
|
AST::UiQualifiedId *uiQualifiedId = data(QmlOutlineModel::IdPointerRole).value<AST::UiQualifiedId*>();
|
||||||
|
if (!uiQualifiedId)
|
||||||
|
return QVariant();
|
||||||
|
|
||||||
|
QList<AST::Node *> astPath = m_outlineModel->m_semanticInfo.astPath(location.begin());
|
||||||
|
|
||||||
|
QmlJS::Document::Ptr document = m_outlineModel->m_semanticInfo.document;
|
||||||
|
QmlJS::Snapshot snapshot = m_outlineModel->m_semanticInfo.snapshot;
|
||||||
|
LookupContext::Ptr lookupContext = LookupContext::create(document, snapshot, astPath);
|
||||||
|
const Interpreter::Value *value = lookupContext->evaluate(uiQualifiedId);
|
||||||
|
|
||||||
|
return prettyPrint(value, lookupContext->context());
|
||||||
|
}
|
||||||
|
|
||||||
|
return QStandardItem::data(role);
|
||||||
|
}
|
||||||
|
|
||||||
QmlOutlineItem &QmlOutlineItem::copyValues(const QmlOutlineItem &other)
|
QmlOutlineItem &QmlOutlineItem::copyValues(const QmlOutlineItem &other)
|
||||||
{
|
{
|
||||||
*this = other;
|
*this = other;
|
||||||
@@ -25,6 +52,26 @@ QmlOutlineItem &QmlOutlineItem::copyValues(const QmlOutlineItem &other)
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString QmlOutlineItem::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Context *context) const
|
||||||
|
{
|
||||||
|
if (! value)
|
||||||
|
return QString();
|
||||||
|
|
||||||
|
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
|
||||||
|
const QString className = objectValue->className();
|
||||||
|
if (!className.isEmpty()) {
|
||||||
|
return className;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const QString typeId = context->engine()->typeId(value);
|
||||||
|
if (typeId == QLatin1String("undefined")) {
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return typeId;
|
||||||
|
}
|
||||||
|
|
||||||
class QmlOutlineModelSync : protected AST::Visitor
|
class QmlOutlineModelSync : protected AST::Visitor
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -34,23 +81,14 @@ public:
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void operator()(Document::Ptr doc, const Snapshot &snapshot)
|
void operator()(Document::Ptr doc)
|
||||||
{
|
{
|
||||||
m_nodeToIndex.clear();
|
m_nodeToIndex.clear();
|
||||||
|
|
||||||
// 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*>());
|
|
||||||
|
|
||||||
if (debug)
|
if (debug)
|
||||||
qDebug() << "QmlOutlineModel ------";
|
qDebug() << "QmlOutlineModel ------";
|
||||||
if (doc && doc->ast())
|
if (doc && doc->ast())
|
||||||
doc->ast()->accept(this);
|
doc->ast()->accept(this);
|
||||||
|
|
||||||
m_context.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -68,51 +106,22 @@ private:
|
|||||||
indent--;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
typedef QPair<QString,QString> ElementType;
|
typedef QPair<QString,QString> ElementType;
|
||||||
bool visit(AST::UiObjectDefinition *objDef)
|
bool visit(AST::UiObjectDefinition *objDef)
|
||||||
{
|
{
|
||||||
AST::SourceLocation location = getLocation(objDef);
|
QModelIndex index = m_model->enterObjectDefinition(objDef);
|
||||||
|
|
||||||
const QString typeName = asString(objDef->qualifiedTypeNameId);
|
|
||||||
|
|
||||||
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);
|
|
||||||
m_nodeToIndex.insert(objDef, index);
|
m_nodeToIndex.insert(objDef, index);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void endVisit(AST::UiObjectDefinition * /*objDef*/)
|
void endVisit(AST::UiObjectDefinition * /*objDef*/)
|
||||||
{
|
{
|
||||||
m_model->leaveElement();
|
m_model->leaveObjectDefiniton();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool visit(AST::UiScriptBinding *scriptBinding)
|
bool visit(AST::UiScriptBinding *scriptBinding)
|
||||||
{
|
{
|
||||||
AST::SourceLocation location = getLocation(scriptBinding);
|
QModelIndex index = m_model->enterScriptBinding(scriptBinding);
|
||||||
|
|
||||||
QModelIndex index = m_model->enterProperty(asString(scriptBinding->qualifiedId), false, location);
|
|
||||||
m_nodeToIndex.insert(scriptBinding, index);
|
m_nodeToIndex.insert(scriptBinding, index);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -120,13 +129,12 @@ private:
|
|||||||
|
|
||||||
void endVisit(AST::UiScriptBinding * /*scriptBinding*/)
|
void endVisit(AST::UiScriptBinding * /*scriptBinding*/)
|
||||||
{
|
{
|
||||||
m_model->leaveProperty();
|
m_model->leaveScriptBinding();
|
||||||
}
|
}
|
||||||
|
|
||||||
bool visit(AST::UiPublicMember *publicMember)
|
bool visit(AST::UiPublicMember *publicMember)
|
||||||
{
|
{
|
||||||
AST::SourceLocation location = getLocation(publicMember);
|
QModelIndex index = m_model->enterPublicMember(publicMember);
|
||||||
QModelIndex index = m_model->enterProperty(publicMember->name->asString(), true, location);
|
|
||||||
m_nodeToIndex.insert(publicMember, index);
|
m_nodeToIndex.insert(publicMember, index);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -134,65 +142,12 @@ private:
|
|||||||
|
|
||||||
void endVisit(AST::UiPublicMember * /*publicMember*/)
|
void endVisit(AST::UiPublicMember * /*publicMember*/)
|
||||||
{
|
{
|
||||||
m_model->leaveProperty();
|
m_model->leavePublicMember();
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
AST::SourceLocation getLocation(AST::UiObjectMember *objMember) {
|
|
||||||
AST::SourceLocation location;
|
|
||||||
location.offset = objMember->firstSourceLocation().offset;
|
|
||||||
location.length = objMember->lastSourceLocation().offset
|
|
||||||
- objMember->firstSourceLocation().offset
|
|
||||||
+ objMember->lastSourceLocation().length;
|
|
||||||
return location;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QmlOutlineModel *m_model;
|
QmlOutlineModel *m_model;
|
||||||
LookupContext::Ptr m_context;
|
|
||||||
|
|
||||||
QHash<AST::Node*, QModelIndex> m_nodeToIndex;
|
QHash<AST::Node*, QModelIndex> m_nodeToIndex;
|
||||||
QHash<QString, QIcon> m_typeToIcon;
|
|
||||||
int indent;
|
int indent;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -206,66 +161,106 @@ QmlOutlineModel::QmlOutlineModel(QObject *parent) :
|
|||||||
|
|
||||||
QmlJS::Document::Ptr QmlOutlineModel::document() const
|
QmlJS::Document::Ptr QmlOutlineModel::document() const
|
||||||
{
|
{
|
||||||
return m_document;
|
return m_semanticInfo.document;
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlOutlineModel::update(QmlJS::Document::Ptr doc, const QmlJS::Snapshot &snapshot)
|
void QmlOutlineModel::update(const SemanticInfo &semanticInfo)
|
||||||
{
|
{
|
||||||
m_document = doc;
|
m_semanticInfo = semanticInfo;
|
||||||
|
|
||||||
m_treePos.clear();
|
m_treePos.clear();
|
||||||
m_treePos.append(0);
|
m_treePos.append(0);
|
||||||
m_currentItem = invisibleRootItem();
|
m_currentItem = invisibleRootItem();
|
||||||
|
|
||||||
|
// 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(semanticInfo.document, semanticInfo.snapshot, QList<AST::Node*>());
|
||||||
|
m_typeToIcon.clear();
|
||||||
|
|
||||||
QmlOutlineModelSync syncModel(this);
|
QmlOutlineModelSync syncModel(this);
|
||||||
syncModel(doc, snapshot);
|
syncModel(m_semanticInfo.document);
|
||||||
|
|
||||||
|
m_context.clear();
|
||||||
|
|
||||||
emit updated();
|
emit updated();
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex QmlOutlineModel::enterElement(const QString &type, const QString &id, const QIcon &icon, const AST::SourceLocation &sourceLocation)
|
QModelIndex QmlOutlineModel::enterObjectDefinition(AST::UiObjectDefinition *objDef)
|
||||||
{
|
{
|
||||||
QmlOutlineItem prototype;
|
QmlOutlineItem prototype(this);
|
||||||
|
|
||||||
|
const QString typeName = asString(objDef->qualifiedTypeNameId);
|
||||||
|
const QString id = getId(objDef);
|
||||||
if (!id.isEmpty()) {
|
if (!id.isEmpty()) {
|
||||||
prototype.setText(id);
|
prototype.setText(id);
|
||||||
} else {
|
} else {
|
||||||
prototype.setText(type);
|
prototype.setText(typeName);
|
||||||
}
|
}
|
||||||
prototype.setIcon(icon);
|
if (!m_typeToIcon.contains(typeName)) {
|
||||||
prototype.setToolTip(type);
|
m_typeToIcon.insert(typeName, getIcon(objDef));
|
||||||
|
}
|
||||||
|
prototype.setIcon(m_typeToIcon.value(typeName));
|
||||||
prototype.setData(ElementType, ItemTypeRole);
|
prototype.setData(ElementType, ItemTypeRole);
|
||||||
prototype.setData(QVariant::fromValue(sourceLocation), SourceLocationRole);
|
prototype.setData(QVariant::fromValue(getLocation(objDef)), SourceLocationRole);
|
||||||
|
prototype.setData(QVariant::fromValue(static_cast<AST::Node*>(objDef)), NodePointerRole);
|
||||||
|
prototype.setData(QVariant::fromValue(static_cast<AST::UiQualifiedId*>(objDef->qualifiedTypeNameId)), IdPointerRole);
|
||||||
|
|
||||||
return enterNode(prototype);
|
return enterNode(prototype);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlOutlineModel::leaveElement()
|
void QmlOutlineModel::leaveObjectDefiniton()
|
||||||
{
|
{
|
||||||
leaveNode();
|
leaveNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
QModelIndex QmlOutlineModel::enterProperty(const QString &name, bool isCustomProperty, const AST::SourceLocation &sourceLocation)
|
QModelIndex QmlOutlineModel::enterScriptBinding(AST::UiScriptBinding *scriptBinding)
|
||||||
{
|
{
|
||||||
QmlOutlineItem prototype;
|
QmlOutlineItem prototype(this);
|
||||||
|
|
||||||
prototype.setText(name);
|
prototype.setText(asString(scriptBinding->qualifiedId));
|
||||||
if (isCustomProperty) {
|
prototype.setIcon(m_icons->scriptBindingIcon());
|
||||||
prototype.setIcon(m_icons->publicMemberIcon());
|
|
||||||
} else {
|
|
||||||
prototype.setIcon(m_icons->scriptBindingIcon());
|
|
||||||
}
|
|
||||||
prototype.setData(PropertyType, ItemTypeRole);
|
prototype.setData(PropertyType, ItemTypeRole);
|
||||||
prototype.setData(QVariant::fromValue(sourceLocation), SourceLocationRole);
|
prototype.setData(QVariant::fromValue(getLocation(scriptBinding)), SourceLocationRole);
|
||||||
|
prototype.setData(QVariant::fromValue(static_cast<AST::Node*>(scriptBinding)), NodePointerRole);
|
||||||
|
prototype.setData(QVariant::fromValue(static_cast<AST::UiQualifiedId*>(scriptBinding->qualifiedId)), IdPointerRole);
|
||||||
|
|
||||||
return enterNode(prototype);
|
return enterNode(prototype);
|
||||||
}
|
}
|
||||||
|
|
||||||
void QmlOutlineModel::leaveProperty()
|
void QmlOutlineModel::leaveScriptBinding()
|
||||||
{
|
{
|
||||||
leaveNode();
|
leaveNode();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QModelIndex QmlOutlineModel::enterPublicMember(AST::UiPublicMember *publicMember)
|
||||||
|
{
|
||||||
|
QmlOutlineItem prototype(this);
|
||||||
|
|
||||||
|
prototype.setText(publicMember->name->asString());
|
||||||
|
prototype.setIcon(m_icons->publicMemberIcon());
|
||||||
|
prototype.setData(PropertyType, ItemTypeRole);
|
||||||
|
prototype.setData(QVariant::fromValue(getLocation(publicMember)), SourceLocationRole);
|
||||||
|
prototype.setData(QVariant::fromValue(static_cast<AST::Node*>(publicMember)), NodePointerRole);
|
||||||
|
|
||||||
|
return enterNode(prototype);
|
||||||
|
}
|
||||||
|
|
||||||
|
void QmlOutlineModel::leavePublicMember()
|
||||||
|
{
|
||||||
|
leaveNode();
|
||||||
|
}
|
||||||
|
|
||||||
|
QmlJS::AST::Node *QmlOutlineModel::nodeForIndex(const QModelIndex &index)
|
||||||
|
{
|
||||||
|
if (index.isValid()) {
|
||||||
|
return index.data(NodePointerRole).value<QmlJS::AST::Node*>();
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
QModelIndex QmlOutlineModel::enterNode(const QmlOutlineItem &prototype)
|
QModelIndex QmlOutlineModel::enterNode(const QmlOutlineItem &prototype)
|
||||||
{
|
{
|
||||||
int siblingIndex = m_treePos.last();
|
int siblingIndex = m_treePos.last();
|
||||||
@@ -275,7 +270,7 @@ QModelIndex QmlOutlineModel::enterNode(const QmlOutlineItem &prototype)
|
|||||||
if (debug)
|
if (debug)
|
||||||
qDebug() << "QmlOutlineModel - Adding" << "element to" << m_currentItem->text();
|
qDebug() << "QmlOutlineModel - Adding" << "element to" << m_currentItem->text();
|
||||||
|
|
||||||
QmlOutlineItem *newItem = new QmlOutlineItem;
|
QmlOutlineItem *newItem = new QmlOutlineItem(this);
|
||||||
newItem->copyValues(prototype);
|
newItem->copyValues(prototype);
|
||||||
newItem->setEditable(false);
|
newItem->setEditable(false);
|
||||||
m_currentItem->appendRow(newItem);
|
m_currentItem->appendRow(newItem);
|
||||||
@@ -293,7 +288,7 @@ QModelIndex QmlOutlineModel::enterNode(const QmlOutlineItem &prototype)
|
|||||||
if (debug)
|
if (debug)
|
||||||
qDebug() << "QmlOutlineModel - Adding" << "element to" << m_currentItem->text();
|
qDebug() << "QmlOutlineModel - Adding" << "element to" << m_currentItem->text();
|
||||||
|
|
||||||
QmlOutlineItem *newItem = new QmlOutlineItem;
|
QmlOutlineItem *newItem = new QmlOutlineItem(this);
|
||||||
newItem->copyValues(prototype);
|
newItem->copyValues(prototype);
|
||||||
newItem->setEditable(false);
|
newItem->setEditable(false);
|
||||||
m_currentItem->appendRow(newItem);
|
m_currentItem->appendRow(newItem);
|
||||||
@@ -345,5 +340,73 @@ QStandardItem *QmlOutlineModel::parentItem()
|
|||||||
return parent;
|
return parent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
QString QmlOutlineModel::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;
|
||||||
|
}
|
||||||
|
|
||||||
|
AST::SourceLocation QmlOutlineModel::getLocation(AST::UiObjectMember *objMember) {
|
||||||
|
AST::SourceLocation location;
|
||||||
|
location.offset = objMember->firstSourceLocation().offset;
|
||||||
|
location.length = objMember->lastSourceLocation().offset
|
||||||
|
- objMember->firstSourceLocation().offset
|
||||||
|
+ objMember->lastSourceLocation().length;
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
QIcon QmlOutlineModel::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_icons->icon(module, typeName);
|
||||||
|
if (! icon.isNull())
|
||||||
|
return icon;
|
||||||
|
|
||||||
|
objectValue = objectValue->prototype(m_context->context());
|
||||||
|
} while (objectValue);
|
||||||
|
}
|
||||||
|
return QIcon();
|
||||||
|
}
|
||||||
|
|
||||||
|
QString QmlOutlineModel::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;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
} // namespace QmlJSEditor
|
} // namespace QmlJSEditor
|
||||||
|
@@ -1,18 +1,39 @@
|
|||||||
#ifndef QMLOUTLINEMODEL_H
|
#ifndef QMLOUTLINEMODEL_H
|
||||||
#define QMLOUTLINEMODEL_H
|
#define QMLOUTLINEMODEL_H
|
||||||
|
|
||||||
|
#include "qmljseditor.h"
|
||||||
#include <qmljs/qmljsdocument.h>
|
#include <qmljs/qmljsdocument.h>
|
||||||
#include <qmljs/qmljsicons.h>
|
#include <qmljs/qmljsicons.h>
|
||||||
|
#include <qmljs/qmljslookupcontext.h>
|
||||||
|
|
||||||
#include <QStandardItemModel>
|
#include <QStandardItemModel>
|
||||||
|
|
||||||
|
namespace QmlJS {
|
||||||
|
namespace Interpreter {
|
||||||
|
class Value;
|
||||||
|
class Context;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace QmlJSEditor {
|
namespace QmlJSEditor {
|
||||||
namespace Internal {
|
namespace Internal {
|
||||||
|
|
||||||
|
class QmlOutlineModel;
|
||||||
|
|
||||||
class QmlOutlineItem : public QStandardItem
|
class QmlOutlineItem : public QStandardItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QmlOutlineItem ©Values(const QmlOutlineItem &other); // so that we can assign all values at once
|
QmlOutlineItem(QmlOutlineModel *model);
|
||||||
|
|
||||||
|
//QStandardItem
|
||||||
|
QVariant data(int role = Qt::UserRole + 1) const;
|
||||||
|
|
||||||
|
QmlOutlineItem ©Values(const QmlOutlineItem &other); // so that we can assign all values at onc
|
||||||
|
|
||||||
|
private:
|
||||||
|
QString prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Context *context) const;
|
||||||
|
|
||||||
|
QmlOutlineModel *m_outlineModel;
|
||||||
};
|
};
|
||||||
|
|
||||||
class QmlOutlineModel : public QStandardItemModel
|
class QmlOutlineModel : public QStandardItemModel
|
||||||
@@ -22,7 +43,9 @@ public:
|
|||||||
|
|
||||||
enum CustomRoles {
|
enum CustomRoles {
|
||||||
SourceLocationRole = Qt::UserRole + 1,
|
SourceLocationRole = Qt::UserRole + 1,
|
||||||
ItemTypeRole = SourceLocationRole + 1
|
ItemTypeRole,
|
||||||
|
NodePointerRole,
|
||||||
|
IdPointerRole
|
||||||
};
|
};
|
||||||
|
|
||||||
enum ItemTypes {
|
enum ItemTypes {
|
||||||
@@ -33,15 +56,18 @@ public:
|
|||||||
QmlOutlineModel(QObject *parent = 0);
|
QmlOutlineModel(QObject *parent = 0);
|
||||||
|
|
||||||
QmlJS::Document::Ptr document() const;
|
QmlJS::Document::Ptr document() const;
|
||||||
void update(QmlJS::Document::Ptr doc, const QmlJS::Snapshot &snapshot);
|
void update(const SemanticInfo &semanticInfo);
|
||||||
|
|
||||||
QModelIndex enterElement(const QString &typeName, const QString &id, const QIcon &icon,
|
QModelIndex enterObjectDefinition(QmlJS::AST::UiObjectDefinition *objectDefinition);
|
||||||
const QmlJS::AST::SourceLocation &location);
|
void leaveObjectDefiniton();
|
||||||
void leaveElement();
|
|
||||||
|
|
||||||
QModelIndex enterProperty(const QString &name, bool isCustomProperty,
|
QModelIndex enterScriptBinding(QmlJS::AST::UiScriptBinding *scriptBinding);
|
||||||
const QmlJS::AST::SourceLocation &location);
|
void leaveScriptBinding();
|
||||||
void leaveProperty();
|
|
||||||
|
QModelIndex enterPublicMember(QmlJS::AST::UiPublicMember *publicMember);
|
||||||
|
void leavePublicMember();
|
||||||
|
|
||||||
|
QmlJS::AST::Node *nodeForIndex(const QModelIndex &index);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void updated();
|
void updated();
|
||||||
@@ -52,17 +78,29 @@ private:
|
|||||||
|
|
||||||
QStandardItem *parentItem();
|
QStandardItem *parentItem();
|
||||||
|
|
||||||
QmlJS::Document::Ptr m_document;
|
static QString asString(QmlJS::AST::UiQualifiedId *id);
|
||||||
|
static QmlJS::AST::SourceLocation getLocation(QmlJS::AST::UiObjectMember *objMember);
|
||||||
|
QIcon getIcon(QmlJS::AST::UiObjectDefinition *objDef);
|
||||||
|
static QString getId(QmlJS::AST::UiObjectDefinition *objDef);
|
||||||
|
|
||||||
|
|
||||||
|
SemanticInfo m_semanticInfo;
|
||||||
QList<int> m_treePos;
|
QList<int> m_treePos;
|
||||||
QStandardItem *m_currentItem;
|
QStandardItem *m_currentItem;
|
||||||
QmlJS::Icons *m_icons;
|
QmlJS::Icons *m_icons;
|
||||||
|
|
||||||
|
QmlJS::LookupContext::Ptr m_context;
|
||||||
|
QHash<QString, QIcon> m_typeToIcon;
|
||||||
|
|
||||||
friend class QmlOutlineModelSync;
|
friend class QmlOutlineModelSync;
|
||||||
|
friend class QmlOutlineItem;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Internal
|
} // namespace Internal
|
||||||
} // namespace QmlJSEditor
|
} // namespace QmlJSEditor
|
||||||
|
|
||||||
Q_DECLARE_METATYPE(QmlJS::AST::SourceLocation);
|
Q_DECLARE_METATYPE(QmlJS::AST::SourceLocation);
|
||||||
|
Q_DECLARE_METATYPE(QmlJS::AST::Node*);
|
||||||
|
Q_DECLARE_METATYPE(QmlJS::AST::UiQualifiedId*);
|
||||||
|
|
||||||
#endif // QMLOUTLINEMODEL_H
|
#endif // QMLOUTLINEMODEL_H
|
||||||
|
Reference in New Issue
Block a user