Merge remote branch 'origin/master' into qmljsinspector

Conflicts:
	src/plugins/qmlinspector/qmlinspector.cpp
This commit is contained in:
Olivier Goffart
2010-07-08 16:13:14 +02:00
383 changed files with 15459 additions and 4077 deletions

View File

@@ -33,12 +33,10 @@
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsbind.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/qmljslookupcontext.h>
#include <qmljs/qmljsscanner.h>
#include <qmljs/qmljsevaluate.h>
#include <qmljs/qmljscompletioncontextfinder.h>
#include <qmljs/qmljslink.h>
#include <qmljs/qmljsscopebuilder.h>
#include <texteditor/basetexteditor.h>
@@ -689,13 +687,9 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
const QIcon symbolIcon = iconForColor(Qt::darkCyan);
const QIcon keywordIcon = iconForColor(Qt::darkYellow);
Interpreter::Engine interp;
Interpreter::Context context(&interp);
Link link(&context, document, snapshot, m_modelManager->importPaths());
// Set up the current scope chain.
ScopeBuilder scopeBuilder(document, &context);
scopeBuilder.push(semanticInfo.astPath(editor->position()));
const QList<AST::Node *> path = semanticInfo.astPath(editor->position());
LookupContext::Ptr lookupContext = LookupContext::create(document, snapshot, path);
Interpreter::Context *context = lookupContext->context();
// Search for the operator that triggered the completion.
QChar completionOperator;
@@ -708,7 +702,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
const Interpreter::ObjectValue *qmlScopeType = 0;
if (contextFinder.isInQmlContext())
qmlScopeType = context.lookupType(document.data(), contextFinder.qmlObjectTypeName());
qmlScopeType = context->lookupType(document.data(), contextFinder.qmlObjectTypeName());
if (completionOperator.isSpace() || completionOperator.isNull() || isDelimiter(completionOperator) ||
(completionOperator == QLatin1Char('(') && m_startPosition != editor->position())) {
@@ -721,7 +715,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
doGlobalCompletion = false;
doJsKeywordCompletion = false;
EnumerateProperties enumerateProperties(&context);
EnumerateProperties enumerateProperties(context);
enumerateProperties.setGlobalCompletion(true);
enumerateProperties.setEnumerateGeneratedSlots(true);
@@ -733,11 +727,11 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
m_completions.append(idPropertyCompletion);
addCompletionsPropertyLhs(enumerateProperties(qmlScopeType), symbolIcon, PropertyOrder);
addCompletions(enumerateProperties(context.scopeChain().qmlTypes), symbolIcon, TypeOrder);
addCompletions(enumerateProperties(context->scopeChain().qmlTypes), symbolIcon, TypeOrder);
if (ScopeBuilder::isPropertyChangesObject(&context, qmlScopeType)
&& context.scopeChain().qmlScopeObjects.size() == 2) {
addCompletions(enumerateProperties(context.scopeChain().qmlScopeObjects.first()), symbolIcon, SymbolOrder);
if (ScopeBuilder::isPropertyChangesObject(context, qmlScopeType)
&& context->scopeChain().qmlScopeObjects.size() == 2) {
addCompletions(enumerateProperties(context->scopeChain().qmlScopeObjects.first()), symbolIcon, SymbolOrder);
}
}
@@ -748,7 +742,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
const Interpreter::Value *value = qmlScopeType;
foreach (const QString &name, contextFinder.bindingPropertyName()) {
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
value = objectValue->property(name, &context);
value = objectValue->property(name, context);
if (!value)
break;
} else {
@@ -772,7 +766,7 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
if (doGlobalCompletion) {
// It's a global completion.
EnumerateProperties enumerateProperties(&context);
EnumerateProperties enumerateProperties(context);
enumerateProperties.setGlobalCompletion(true);
addCompletions(enumerateProperties(), symbolIcon, SymbolOrder);
}
@@ -813,14 +807,13 @@ int CodeCompletion::startCompletion(TextEditor::ITextEditable *editor)
QmlJS::AST::ExpressionNode *expression = expressionUnderCursor(tc);
if (expression != 0 && ! isLiteral(expression)) {
Evaluate evaluate(&context);
// Evaluate the expression under cursor.
const Interpreter::Value *value = interp.convertToObject(evaluate(expression));
Interpreter::Engine *interp = lookupContext->engine();
const Interpreter::Value *value = interp->convertToObject(lookupContext->evaluate(expression));
//qDebug() << "type:" << interp.typeId(value);
if (value && completionOperator == QLatin1Char('.')) { // member completion
EnumerateProperties enumerateProperties(&context);
EnumerateProperties enumerateProperties(context);
if (contextFinder.isInLhsOfBinding() && qmlScopeType && expressionUnderCursor.text().at(0).isLower())
addCompletionsPropertyLhs(enumerateProperties(value), symbolIcon, PropertyOrder);
else

View File

@@ -41,6 +41,7 @@
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljslink.h>
#include <qmljs/qmljsscopebuilder.h>
#include <qmljs/qmljsicontextpane.h>
#include <qmljs/parser/qmljsastvisitor_p.h>
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/parser/qmljsengine_p.h>
@@ -557,7 +558,7 @@ AST::Node *SemanticInfo::nodeUnderCursor(int pos) const
int SemanticInfo::revision() const
{
if (document)
return document->documentRevision();
return document->editorRevision();
return 0;
}
@@ -610,7 +611,8 @@ QString QmlJSEditorEditable::preferredMode() const
QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
TextEditor::BaseTextEditor(parent),
m_methodCombo(0),
m_modelManager(0)
m_modelManager(0),
m_contextPane(0)
{
qRegisterMetaType<QmlJSEditor::Internal::SemanticInfo>("QmlJSEditor::Internal::SemanticInfo");
@@ -643,6 +645,10 @@ QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
baseTextDocument()->setSyntaxHighlighter(new Highlighter(document()));
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<ModelManagerInterface>();
m_contextPane = ExtensionSystem::PluginManager::instance()->getObject<QmlJS::IContextPane>();
if (m_contextPane)
connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(onCursorPositionChanged()));
m_oldCurserPosition = -1;
if (m_modelManager) {
m_semanticHighlighter->setModelManager(m_modelManager);
@@ -654,7 +660,7 @@ QmlJSTextEditor::QmlJSTextEditor(QWidget *parent) :
connect(m_semanticHighlighter, SIGNAL(changed(QmlJSEditor::Internal::SemanticInfo)),
this, SLOT(updateSemanticInfo(QmlJSEditor::Internal::SemanticInfo)));
setRequestMarkEnabled(false);
setRequestMarkEnabled(true);
}
QmlJSTextEditor::~QmlJSTextEditor()
@@ -761,7 +767,7 @@ static void appendExtraSelectionsForMessages(
void QmlJSTextEditor::onDocumentUpdated(QmlJS::Document::Ptr doc)
{
if (file()->fileName() != doc->fileName()
|| doc->documentRevision() != document()->revision()) {
|| doc->editorRevision() != document()->revision()) {
// didn't get the currently open, or an up to date document.
// trigger a semantic rehighlight anyway, after a time
m_semanticRehighlightTimer->start();
@@ -1161,6 +1167,29 @@ void QmlJSTextEditor::contextMenuEvent(QContextMenuEvent *e)
menu->deleteLater();
}
bool QmlJSTextEditor::event(QEvent *e)
{
switch (e->type()) {
case QEvent::ShortcutOverride:
if (static_cast<QKeyEvent*>(e)->key() == Qt::Key_Escape && m_contextPane) {
m_contextPane->apply(editableInterface(), m_semanticInfo.document, 0, false);
}
break;
default:
break;
}
return BaseTextEditor::event(e);
}
void QmlJSTextEditor::wheelEvent(QWheelEvent *event)
{
BaseTextEditor::wheelEvent(event);
if (m_contextPane)
m_contextPane->apply(editableInterface(), m_semanticInfo.document, m_semanticInfo.declaringMember(position()), true);
}
void QmlJSTextEditor::unCommentSelection()
{
Utils::unCommentSelection(this);
@@ -1383,6 +1412,9 @@ void QmlJSTextEditor::updateSemanticInfo(const SemanticInfo &semanticInfo)
FindIdDeclarations updateIds;
m_semanticInfo.idLocations = updateIds(doc);
if (m_contextPane) {
m_contextPane->setEnabled(doc->isParsedCorrectly());
}
if (doc->isParsedCorrectly()) {
FindDeclarations findDeclarations;
m_semanticInfo.declarations = findDeclarations(doc->ast());
@@ -1396,6 +1428,13 @@ void QmlJSTextEditor::updateSemanticInfo(const SemanticInfo &semanticInfo)
m_methodCombo->clear();
m_methodCombo->addItems(items);
updateMethodBoxIndex();
if (m_contextPane) {
Node *newNode = m_semanticInfo.declaringMember(position());
if (newNode) {
m_contextPane->apply(editableInterface(), doc, newNode, true);
m_oldCurserPosition = position();
}
}
}
// update warning/error extra selections
@@ -1403,6 +1442,19 @@ void QmlJSTextEditor::updateSemanticInfo(const SemanticInfo &semanticInfo)
appendExtraSelectionsForMessages(&selections, doc->diagnosticMessages(), document());
appendExtraSelectionsForMessages(&selections, m_semanticInfo.semanticMessages, document());
setExtraSelections(CodeWarningsSelection, selections);
emit semanticInfoUpdated(semanticInfo);
}
void QmlJSTextEditor::onCursorPositionChanged()
{
if (m_contextPane) {
Node *newNode = m_semanticInfo.declaringMember(position());
Node *oldNode = m_semanticInfo.declaringMember(m_oldCurserPosition);
if (oldNode != newNode)
m_contextPane->apply(editableInterface(), m_semanticInfo.document, newNode, false);
m_oldCurserPosition = position();
}
}
SemanticHighlighter::Source QmlJSTextEditor::currentSource(bool force)
@@ -1506,7 +1558,7 @@ SemanticInfo SemanticHighlighter::semanticInfo(const Source &source)
if (! doc) {
snapshot = source.snapshot;
doc = snapshot.documentFromSource(source.code, source.fileName);
doc->setDocumentRevision(source.revision);
doc->setEditorRevision(source.revision);
doc->parse();
}

View File

@@ -51,6 +51,7 @@ class ICore;
namespace QmlJS {
class ModelManagerInterface;
class IContextPane;
}
namespace QmlJSEditor {
@@ -222,6 +223,9 @@ public slots:
void followSymbolUnderCursor();
virtual void setFontSettings(const TextEditor::FontSettings &);
signals:
void semanticInfoUpdated(const QmlJSEditor::Internal::SemanticInfo &semanticInfo);
private slots:
void onDocumentUpdated(QmlJS::Document::Ptr doc);
void modificationChanged(bool);
@@ -242,9 +246,12 @@ private slots:
void semanticRehighlight();
void forceSemanticRehighlight();
void updateSemanticInfo(const QmlJSEditor::Internal::SemanticInfo &semanticInfo);
void onCursorPositionChanged();
protected:
void contextMenuEvent(QContextMenuEvent *e);
bool event(QEvent *e);
void wheelEvent(QWheelEvent *event);
TextEditor::BaseTextEditorEditable *createEditableInterface();
void createToolBar(QmlJSEditorEditable *editable);
TextEditor::BaseTextEditor::Link findLinkAt(const QTextCursor &cursor, bool resolveTarget = true);
@@ -279,6 +286,9 @@ private:
SemanticHighlighter *m_semanticHighlighter;
SemanticInfo m_semanticInfo;
QmlJS::IContextPane *m_contextPane;
int m_oldCurserPosition;
};
} // namespace Internal

View File

@@ -23,7 +23,8 @@ HEADERS += \
qmljspreviewrunner.h \
qmljsquickfix.h \
qmljsrefactoringchanges.h \
qmljscomponentfromobjectdef.h
qmljscomponentfromobjectdef.h \
qmljsoutline.h
SOURCES += \
qmljscodecompletion.cpp \
@@ -39,7 +40,8 @@ SOURCES += \
qmljspreviewrunner.cpp \
qmljsquickfix.cpp \
qmljsrefactoringchanges.cpp \
qmljscomponentfromobjectdef.cpp
qmljscomponentfromobjectdef.cpp \
qmljsoutline.cpp
RESOURCES += qmljseditor.qrc
OTHER_FILES += QmlJSEditor.pluginspec QmlJSEditor.mimetypes.xml

View File

@@ -36,6 +36,7 @@
#include "qmljshoverhandler.h"
#include "qmljsmodelmanager.h"
#include "qmlfilewizard.h"
#include "qmljsoutline.h"
#include "qmljspreviewrunner.h"
#include "qmljsquickfix.h"
@@ -176,6 +177,8 @@ bool QmlJSEditorPlugin::initialize(const QStringList & /*arguments*/, QString *e
m_quickFixCollector = new QmlJSQuickFixCollector;
addAutoReleasedObject(m_quickFixCollector);
addAutoReleasedObject(new QmlJSOutlineWidgetFactory);
return true;
}

View File

@@ -37,11 +37,8 @@
#include <coreplugin/editormanager/editormanager.h>
#include <debugger/debuggerconstants.h>
#include <extensionsystem/pluginmanager.h>
#include <qmljs/qmljsbind.h>
#include <qmljs/qmljsevaluate.h>
#include <qmljs/qmljslookupcontext.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/qmljslink.h>
#include <qmljs/qmljsscopebuilder.h>
#include <qmljs/parser/qmljsast_p.h>
#include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h>
@@ -151,17 +148,11 @@ void HoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int p
if (node && !(AST::cast<AST::StringLiteral *>(node) != 0 || AST::cast<AST::NumericLiteral *>(node) != 0)) {
QList<AST::Node *> astPath = semanticInfo.astPath(pos);
Interpreter::Engine interp;
Interpreter::Context context(&interp);
Link link(&context, qmlDocument, snapshot, m_modelManager->importPaths());
ScopeBuilder scopeBuilder(qmlDocument, &context);
scopeBuilder.push(astPath);
Evaluate check(&context);
const Interpreter::Value *value = check(node);
LookupContext::Ptr lookupContext = LookupContext::create(qmlDocument, snapshot, astPath);
const Interpreter::Value *value = lookupContext->evaluate(node);
QStringList baseClasses;
m_toolTip = prettyPrint(value, &context, &baseClasses);
m_toolTip = prettyPrint(value, lookupContext->context(), &baseClasses);
foreach (const QString &baseClass, baseClasses) {
QString helpId = QLatin1String("QML.");
@@ -193,7 +184,7 @@ void HoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int p
}
QString HoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Context *context,
QStringList *baseClasses) const
QStringList *baseClasses) const
{
if (! value)
return QString();

View File

@@ -384,7 +384,7 @@ void ModelManager::parse(QFutureInterface<void> &future,
}
Document::Ptr doc = Document::create(fileName);
doc->setDocumentRevision(documentRevision);
doc->setEditorRevision(documentRevision);
doc->setSource(contents);
doc->parse();

View File

@@ -0,0 +1,393 @@
#include "qmljsoutline.h"
#include <coreplugin/icore.h>
#include <extensionsystem/pluginmanager.h>
#include <qmljs/parser/qmljsast_p.h>
#include <qmljs/qmljsmodelmanagerinterface.h>
#include <QtCore/QDebug>
#include <QtGui/QVBoxLayout>
#include <typeinfo>
using namespace QmlJS;
enum {
debug = false
};
namespace QmlJSEditor {
namespace Internal {
QmlOutlineModel::QmlOutlineModel(QObject *parent) :
QStandardItemModel(parent)
{
}
QmlJSOutlineTreeView::QmlJSOutlineTreeView(QWidget *parent) :
QTreeView(parent)
{
// see also CppOutlineTreeView
setFocusPolicy(Qt::NoFocus);
setFrameStyle(QFrame::NoFrame);
setAttribute(Qt::WA_MacShowFocusRect, false);
setUniformRowHeights(true);
setHeaderHidden(true);
setTextElideMode(Qt::ElideNone);
setIndentation(20);
setExpandsOnDoubleClick(false);
}
void QmlOutlineModel::startSync()
{
m_treePos.clear();
m_treePos.append(0);
m_currentItem = invisibleRootItem();
}
QModelIndex QmlOutlineModel::enterElement(const QString &type, const AST::SourceLocation &sourceLocation)
{
QStandardItem *item = enterNode(sourceLocation);
item->setText(type);
item->setIcon(m_icons.objectDefinitionIcon());
return item->index();
}
void QmlOutlineModel::leaveElement()
{
leaveNode();
}
QModelIndex QmlOutlineModel::enterProperty(const QString &name, const AST::SourceLocation &sourceLocation)
{
QStandardItem *item = enterNode(sourceLocation);
item->setText(name);
item->setIcon(m_icons.scriptBindingIcon());
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;
}
class QmlOutlineModelSync : protected AST::Visitor
{
public:
QmlOutlineModelSync(QmlOutlineModel *model) :
m_model(model),
indent(0)
{
}
void operator()(Document::Ptr doc)
{
m_nodeToIndex.clear();
if (debug)
qDebug() << "QmlOutlineModel ------";
m_model->startSync();
if (doc && doc->ast())
doc->ast()->accept(this);
}
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;
}
bool visit(AST::UiObjectDefinition *objDef)
{
AST::SourceLocation location;
location.offset = objDef->firstSourceLocation().offset;
location.length = objDef->lastSourceLocation().offset
- objDef->firstSourceLocation().offset
+ objDef->lastSourceLocation().length;
QModelIndex index = m_model->enterElement(asString(objDef->qualifiedTypeNameId), location);
m_nodeToIndex.insert(objDef, index);
return true;
}
void endVisit(AST::UiObjectDefinition * /*objDefinition*/)
{
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();
}
QmlOutlineModel *m_model;
QHash<AST::Node*, QModelIndex> m_nodeToIndex;
int indent;
};
QmlJSOutlineWidget::QmlJSOutlineWidget(QWidget *parent) :
TextEditor::IOutlineWidget(parent),
m_treeView(new QmlJSOutlineTreeView()),
m_model(new QmlOutlineModel),
m_enableCursorSync(true),
m_blockCursorSync(false)
{
QVBoxLayout *layout = new QVBoxLayout;
layout->setMargin(0);
layout->setSpacing(0);
layout->addWidget(m_treeView);
setLayout(layout);
m_treeView->setModel(m_model);
connect(m_treeView->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)),
this, SLOT(updateSelectionInText(QItemSelection)));
}
void QmlJSOutlineWidget::setEditor(QmlJSTextEditor *editor)
{
m_editor = editor;
connect(m_editor.data(), SIGNAL(semanticInfoUpdated(QmlJSEditor::Internal::SemanticInfo)),
this, SLOT(updateOutline(QmlJSEditor::Internal::SemanticInfo)));
connect(m_editor.data(), SIGNAL(cursorPositionChanged()),
this, SLOT(updateSelectionInTree()));
updateOutline(m_editor.data()->semanticInfo());
}
void QmlJSOutlineWidget::setCursorSynchronization(bool syncWithCursor)
{
m_enableCursorSync = syncWithCursor;
if (m_enableCursorSync)
updateSelectionInTree();
}
void QmlJSOutlineWidget::updateOutline(const QmlJSEditor::Internal::SemanticInfo &semanticInfo)
{
Document::Ptr doc = semanticInfo.document;
if (!doc) {
return;
}
if (!m_editor
|| m_editor.data()->file()->fileName() != doc->fileName()
|| m_editor.data()->documentRevision() != doc->editorRevision()) {
return;
}
if (doc->ast()
&& m_model) {
// got a correctly parsed (or recovered) file.
if (QmlOutlineModel *qmlModel = qobject_cast<QmlOutlineModel*>(m_model)) {
QmlOutlineModelSync syncModel(qmlModel);
syncModel(doc);
}
} else {
// TODO: Maybe disable view?
}
m_treeView->expandAll();
}
QModelIndex QmlJSOutlineWidget::indexForPosition(const QModelIndex &rootIndex, int cursorPosition)
{
if (!rootIndex.isValid())
return QModelIndex();
AST::SourceLocation location = rootIndex.data(QmlOutlineModel::SourceLocationRole).value<AST::SourceLocation>();
if (!offsetInsideLocation(cursorPosition, location)) {
return QModelIndex();
}
const int rowCount = rootIndex.model()->rowCount(rootIndex);
for (int i = 0; i < rowCount; ++i) {
QModelIndex childIndex = rootIndex.child(i, 0);
QModelIndex resultIndex = indexForPosition(childIndex, cursorPosition);
if (resultIndex.isValid())
return resultIndex;
}
return rootIndex;
}
void QmlJSOutlineWidget::updateSelectionInTree()
{
if (!syncCursor())
return;
int absoluteCursorPos = m_editor.data()->textCursor().position();
QModelIndex index = indexForPosition(m_model->index(0, 0), absoluteCursorPos);
m_blockCursorSync = true;
m_treeView->selectionModel()->select(index, QItemSelectionModel::ClearAndSelect);
m_blockCursorSync = false;
}
void QmlJSOutlineWidget::updateSelectionInText(const QItemSelection &selection)
{
if (!syncCursor())
return;
if (!selection.indexes().isEmpty()) {
QModelIndex index = selection.indexes().first();
AST::SourceLocation location = index.data(QmlOutlineModel::SourceLocationRole).value<AST::SourceLocation>();
QTextCursor textCursor = m_editor.data()->textCursor();
m_blockCursorSync = true;
textCursor.setPosition(location.offset);
m_editor.data()->setTextCursor(textCursor);
m_blockCursorSync = false;
}
}
bool QmlJSOutlineWidget::offsetInsideLocation(quint32 offset, const QmlJS::AST::SourceLocation &location)
{
return ((offset >= location.offset)
&& (offset <= location.offset + location.length));
}
bool QmlJSOutlineWidget::syncCursor()
{
return m_enableCursorSync && !m_blockCursorSync;
}
bool QmlJSOutlineWidgetFactory::supportsEditor(Core::IEditor *editor) const
{
if (qobject_cast<QmlJSEditorEditable*>(editor))
return true;
return false;
}
TextEditor::IOutlineWidget *QmlJSOutlineWidgetFactory::createWidget(Core::IEditor *editor)
{
QmlJSOutlineWidget *widget = new QmlJSOutlineWidget;
QmlJSEditorEditable *qmlJSEditable = qobject_cast<QmlJSEditorEditable*>(editor);
QmlJSTextEditor *qmlJSEditor = qobject_cast<QmlJSTextEditor*>(qmlJSEditable->widget());
Q_ASSERT(qmlJSEditor);
widget->setEditor(qmlJSEditor);
return widget;
}
} // namespace Internal
} // namespace QmlJSEditor

View File

@@ -0,0 +1,108 @@
#ifndef QMLJSOUTLINE_H
#define QMLJSOUTLINE_H
#include "qmljseditor.h"
#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/inavigationwidgetfactory.h>
#include <texteditor/ioutlinewidget.h>
#include <qmljs/parser/qmljsastvisitor_p.h>
#include <qmljs/qmljsdocument.h>
#include <qmljs/qmljsicons.h>
#include <QtGui/QStandardItemModel>
#include <QtGui/QTreeView>
#include <QtGui/QWidget>
namespace Core {
class IEditor;
}
namespace QmlJS {
class Editor;
};
namespace QmlJSEditor {
namespace Internal {
class QmlOutlineModel : public QStandardItemModel
{
Q_OBJECT
public:
enum CustomRoles {
SourceLocationRole = Qt::UserRole + 1
};
QmlOutlineModel(QObject *parent = 0);
void startSync();
QModelIndex enterElement(const QString &typeName, const QmlJS::AST::SourceLocation &location);
void leaveElement();
QModelIndex enterProperty(const QString &name, const QmlJS::AST::SourceLocation &location);
void leaveProperty();
private:
QStandardItem *enterNode(const QmlJS::AST::SourceLocation &location);
void leaveNode();
QStandardItem *parentItem();
QList<int> m_treePos;
QStandardItem *m_currentItem;
QmlJS::Icons m_icons;
};
class QmlJSOutlineTreeView : public QTreeView
{
Q_OBJECT
public:
QmlJSOutlineTreeView(QWidget *parent = 0);
};
class QmlJSOutlineWidget : public TextEditor::IOutlineWidget
{
Q_OBJECT
public:
QmlJSOutlineWidget(QWidget *parent = 0);
void setEditor(QmlJSTextEditor *editor);
// IOutlineWidget
virtual void setCursorSynchronization(bool syncWithCursor);
private slots:
void updateOutline(const QmlJSEditor::Internal::SemanticInfo &semanticInfo);
void updateSelectionInTree();
void updateSelectionInText(const QItemSelection &selection);
private:
QModelIndex indexForPosition(const QModelIndex &rootIndex, int cursorPosition);
bool offsetInsideLocation(quint32 offset, const QmlJS::AST::SourceLocation &location);
bool syncCursor();
private:
QmlJSOutlineTreeView *m_treeView;
QAbstractItemModel *m_model;
QWeakPointer<QmlJSTextEditor> m_editor;
bool m_enableCursorSync;
bool m_blockCursorSync;
};
class QmlJSOutlineWidgetFactory : public TextEditor::IOutlineWidgetFactory
{
Q_OBJECT
public:
bool supportsEditor(Core::IEditor *editor) const;
TextEditor::IOutlineWidget *createWidget(Core::IEditor *editor);
};
} // namespace Internal
} // namespace QmlJSEditor
Q_DECLARE_METATYPE(QmlJS::AST::SourceLocation);
#endif // QMLJSOUTLINE_H