2009-05-06 15:45:19 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2011-01-11 16:28:15 +01:00
|
|
|
** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
2009-05-06 15:45:19 +02:00
|
|
|
**
|
2009-06-17 00:01:27 +10:00
|
|
|
** Contact: Nokia Corporation (qt-info@nokia.com)
|
2009-05-06 15:45:19 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** No Commercial Usage
|
2009-05-06 15:45:19 +02:00
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** This file contains pre-release code and may not be distributed.
|
|
|
|
|
** You may use this file in accordance with the terms and conditions
|
|
|
|
|
** contained in the Technology Preview License Agreement accompanying
|
|
|
|
|
** this package.
|
2009-05-06 15:45:19 +02:00
|
|
|
**
|
|
|
|
|
** GNU Lesser General Public License Usage
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
2010-12-17 16:01:08 +01:00
|
|
|
** In addition, as a special exception, Nokia gives you certain additional
|
|
|
|
|
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
|
**
|
|
|
|
|
** If you have questions regarding the use of this file, please contact
|
|
|
|
|
** Nokia at qt-info@nokia.com.
|
2009-05-06 15:45:19 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
#include "qmljseditor.h"
|
2010-11-02 11:10:27 +01:00
|
|
|
#include "qmljseditoreditable.h"
|
2009-10-08 13:48:39 +02:00
|
|
|
#include "qmlexpressionundercursor.h"
|
2010-02-15 13:41:51 +01:00
|
|
|
#include "qmljshoverhandler.h"
|
2009-05-06 15:45:19 +02:00
|
|
|
|
2010-08-03 15:42:14 +02:00
|
|
|
#include <coreplugin/editormanager/ieditor.h>
|
2009-05-06 15:45:19 +02:00
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2010-08-27 12:11:55 +02:00
|
|
|
#include <coreplugin/helpmanager.h>
|
2009-05-06 15:45:19 +02:00
|
|
|
#include <extensionsystem/pluginmanager.h>
|
2010-01-27 12:40:02 +01:00
|
|
|
#include <qmljs/qmljsinterpreter.h>
|
2010-02-02 13:18:56 +01:00
|
|
|
#include <qmljs/parser/qmljsast_p.h>
|
2010-08-02 14:50:38 +02:00
|
|
|
#include <qmljs/parser/qmljsastfwd_p.h>
|
|
|
|
|
#include <qmljs/qmljscheck.h>
|
2009-05-06 15:45:19 +02:00
|
|
|
#include <texteditor/itexteditor.h>
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
2010-08-27 12:11:55 +02:00
|
|
|
#include <texteditor/helpitem.h>
|
2010-08-02 14:50:38 +02:00
|
|
|
#include <texteditor/tooltip/tooltip.h>
|
2010-08-23 15:21:02 +02:00
|
|
|
#include <texteditor/tooltip/tipcontents.h>
|
2009-05-06 15:45:19 +02:00
|
|
|
|
2010-08-27 12:11:55 +02:00
|
|
|
#include <QtCore/QList>
|
|
|
|
|
|
2009-05-06 15:45:19 +02:00
|
|
|
using namespace Core;
|
2010-01-18 16:15:23 +01:00
|
|
|
using namespace QmlJS;
|
2010-01-15 17:20:03 +01:00
|
|
|
using namespace QmlJSEditor;
|
|
|
|
|
using namespace QmlJSEditor::Internal;
|
2009-05-06 15:45:19 +02:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
QString textAt(const Document::Ptr doc,
|
|
|
|
|
const AST::SourceLocation &from,
|
|
|
|
|
const AST::SourceLocation &to)
|
|
|
|
|
{
|
|
|
|
|
return doc->source().mid(from.offset, to.end() - from.begin());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AST::UiObjectInitializer *nodeInitializer(AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
AST::UiObjectInitializer *initializer = 0;
|
|
|
|
|
if (const AST::UiObjectBinding *binding = AST::cast<const AST::UiObjectBinding *>(node))
|
|
|
|
|
initializer = binding->initializer;
|
|
|
|
|
else if (const AST::UiObjectDefinition *definition =
|
|
|
|
|
AST::cast<const AST::UiObjectDefinition *>(node))
|
|
|
|
|
initializer = definition->initializer;
|
|
|
|
|
return initializer;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
|
bool posIsInSource(const unsigned pos, T *node)
|
|
|
|
|
{
|
|
|
|
|
if (node &&
|
|
|
|
|
pos >= node->firstSourceLocation().begin() && pos < node->lastSourceLocation().end()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-03 15:42:14 +02:00
|
|
|
HoverHandler::HoverHandler(QObject *parent) : BaseHoverHandler(parent), m_modelManager(0)
|
2009-05-06 15:45:19 +02:00
|
|
|
{
|
2010-08-02 14:50:38 +02:00
|
|
|
m_modelManager =
|
|
|
|
|
ExtensionSystem::PluginManager::instance()->getObject<QmlJS::ModelManagerInterface>();
|
2009-05-06 15:45:19 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-03 15:42:14 +02:00
|
|
|
bool HoverHandler::acceptEditor(IEditor *editor)
|
2009-05-06 15:45:19 +02:00
|
|
|
{
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSEditorEditable *qmlEditor = qobject_cast<QmlJSEditorEditable *>(editor);
|
2010-08-03 15:42:14 +02:00
|
|
|
if (qmlEditor)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2010-08-02 14:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
|
|
|
|
|
{
|
2010-08-27 12:11:55 +02:00
|
|
|
reset();
|
|
|
|
|
|
2009-10-08 13:48:39 +02:00
|
|
|
if (!m_modelManager)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-11-02 11:10:27 +01:00
|
|
|
QmlJSEditor::QmlJSTextEditor *qmlEditor = qobject_cast<QmlJSEditor::QmlJSTextEditor *>(editor->widget());
|
2010-08-02 14:50:38 +02:00
|
|
|
if (!qmlEditor)
|
2009-10-08 13:48:39 +02:00
|
|
|
return;
|
|
|
|
|
|
2010-11-17 14:48:25 +01:00
|
|
|
if (matchDiagnosticMessage(qmlEditor, pos))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const QmlJSEditor::SemanticInfo &semanticInfo = qmlEditor->semanticInfo();
|
|
|
|
|
if (! semanticInfo.isValid() || semanticInfo.revision() != qmlEditor->editorRevision())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
QList<AST::Node *> astPath = semanticInfo.astPath(pos);
|
|
|
|
|
if (astPath.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
const Document::Ptr qmlDocument = semanticInfo.document;
|
|
|
|
|
LookupContext::Ptr lookupContext = semanticInfo.lookupContext(astPath);
|
|
|
|
|
|
|
|
|
|
if (matchColorItem(lookupContext, qmlDocument, astPath, pos))
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
AST::Node *node = semanticInfo.nodeUnderCursor(pos);
|
|
|
|
|
handleOrdinaryMatch(lookupContext, node);
|
|
|
|
|
|
|
|
|
|
TextEditor::HelpItem helpItem = qmlHelpItem(lookupContext, node);
|
|
|
|
|
if (!helpItem.helpId().isEmpty())
|
|
|
|
|
setLastHelpItemIdentified(helpItem);
|
2010-08-02 14:50:38 +02:00
|
|
|
}
|
|
|
|
|
|
2010-11-02 11:10:27 +01:00
|
|
|
bool HoverHandler::matchDiagnosticMessage(QmlJSEditor::QmlJSTextEditor *qmlEditor, int pos)
|
2010-08-02 14:50:38 +02:00
|
|
|
{
|
|
|
|
|
foreach (const QTextEdit::ExtraSelection &sel,
|
|
|
|
|
qmlEditor->extraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection)) {
|
2010-01-26 14:03:37 +01:00
|
|
|
if (pos >= sel.cursor.selectionStart() && pos <= sel.cursor.selectionEnd()) {
|
2010-08-03 15:42:14 +02:00
|
|
|
setToolTip(sel.format.toolTip());
|
2010-08-02 14:50:38 +02:00
|
|
|
return true;
|
2009-10-08 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-08-02 14:50:38 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
bool HoverHandler::matchColorItem(const LookupContext::Ptr &lookupContext,
|
|
|
|
|
const Document::Ptr &qmlDocument,
|
|
|
|
|
const QList<AST::Node *> &astPath,
|
|
|
|
|
unsigned pos)
|
|
|
|
|
{
|
|
|
|
|
AST::UiObjectInitializer *initializer = nodeInitializer(astPath.last());
|
|
|
|
|
if (!initializer)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
AST::UiObjectMember *member = 0;
|
|
|
|
|
for (AST::UiObjectMemberList *list = initializer->members; list; list = list->next) {
|
|
|
|
|
if (posIsInSource(pos, list->member)) {
|
|
|
|
|
member = list->member;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!member)
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
QString color;
|
|
|
|
|
const Interpreter::Value *value = 0;
|
|
|
|
|
if (const AST::UiScriptBinding *binding = AST::cast<const AST::UiScriptBinding *>(member)) {
|
|
|
|
|
if (binding->qualifiedId && posIsInSource(pos, binding->statement)) {
|
|
|
|
|
value = lookupContext->evaluate(binding->qualifiedId);
|
|
|
|
|
if (value && value->asColorValue()) {
|
|
|
|
|
color = textAt(qmlDocument,
|
|
|
|
|
binding->statement->firstSourceLocation(),
|
|
|
|
|
binding->statement->lastSourceLocation());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (const AST::UiPublicMember *publicMember =
|
|
|
|
|
AST::cast<const AST::UiPublicMember *>(member)) {
|
|
|
|
|
if (publicMember->name && posIsInSource(pos, publicMember->expression)) {
|
|
|
|
|
value = lookupContext->context()->lookup(publicMember->name->asString());
|
|
|
|
|
if (const Interpreter::Reference *ref = value->asReference())
|
|
|
|
|
value = lookupContext->context()->lookupReference(ref);
|
|
|
|
|
color = textAt(qmlDocument,
|
|
|
|
|
publicMember->expression->firstSourceLocation(),
|
|
|
|
|
publicMember->expression->lastSourceLocation());
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-02-02 13:18:56 +01:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
if (!color.isEmpty()) {
|
|
|
|
|
color.remove(QLatin1Char('\''));
|
|
|
|
|
color.remove(QLatin1Char('\"'));
|
|
|
|
|
color.remove(QLatin1Char(';'));
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
m_colorTip = QmlJS::toQColor(color);
|
|
|
|
|
if (m_colorTip.isValid()) {
|
2010-08-03 15:42:14 +02:00
|
|
|
setToolTip(color);
|
2010-08-02 14:50:38 +02:00
|
|
|
return true;
|
2009-10-08 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-08-02 14:50:38 +02:00
|
|
|
return false;
|
|
|
|
|
}
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
void HoverHandler::handleOrdinaryMatch(const LookupContext::Ptr &lookupContext, AST::Node *node)
|
|
|
|
|
{
|
|
|
|
|
if (node && !(AST::cast<AST::StringLiteral *>(node) != 0 ||
|
|
|
|
|
AST::cast<AST::NumericLiteral *>(node) != 0)) {
|
|
|
|
|
const Interpreter::Value *value = lookupContext->evaluate(node);
|
2010-08-27 12:11:55 +02:00
|
|
|
prettyPrintTooltip(value, lookupContext->context());
|
2010-08-02 14:50:38 +02:00
|
|
|
}
|
|
|
|
|
}
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-08-27 12:11:55 +02:00
|
|
|
void HoverHandler::reset()
|
2010-08-03 15:42:14 +02:00
|
|
|
{
|
|
|
|
|
m_colorTip = QColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HoverHandler::operateTooltip(TextEditor::ITextEditor *editor, const QPoint &point)
|
|
|
|
|
{
|
|
|
|
|
if (toolTip().isEmpty())
|
|
|
|
|
TextEditor::ToolTip::instance()->hide();
|
|
|
|
|
else {
|
|
|
|
|
if (m_colorTip.isValid()) {
|
2010-08-23 15:21:02 +02:00
|
|
|
TextEditor::ToolTip::instance()->show(point,
|
|
|
|
|
TextEditor::ColorContent(m_colorTip),
|
|
|
|
|
editor->widget());
|
2010-08-03 15:42:14 +02:00
|
|
|
} else {
|
2010-08-23 15:21:02 +02:00
|
|
|
TextEditor::ToolTip::instance()->show(point,
|
|
|
|
|
TextEditor::TextContent(toolTip()),
|
|
|
|
|
editor->widget());
|
2010-08-03 15:42:14 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-08-27 12:11:55 +02:00
|
|
|
void HoverHandler::prettyPrintTooltip(const QmlJS::Interpreter::Value *value,
|
|
|
|
|
const QmlJS::Interpreter::Context *context)
|
2010-01-27 14:00:18 +01:00
|
|
|
{
|
2010-02-03 15:39:57 +01:00
|
|
|
if (! value)
|
2010-08-27 12:11:55 +02:00
|
|
|
return;
|
2010-01-27 14:00:18 +01:00
|
|
|
|
|
|
|
|
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
|
2010-11-12 14:53:00 +01:00
|
|
|
Interpreter::PrototypeIterator iter(objectValue, context);
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
const Interpreter::ObjectValue *prototype = iter.next();
|
|
|
|
|
const QString className = prototype->className();
|
2010-01-27 14:56:22 +01:00
|
|
|
|
2010-08-27 12:11:55 +02:00
|
|
|
if (! className.isEmpty()) {
|
2010-11-17 14:48:25 +01:00
|
|
|
setToolTip(className);
|
2010-11-12 14:53:00 +01:00
|
|
|
break;
|
2010-11-17 14:48:25 +01:00
|
|
|
}
|
2010-11-12 14:53:00 +01:00
|
|
|
}
|
2010-08-02 14:50:38 +02:00
|
|
|
} else if (const Interpreter::QmlEnumValue *enumValue =
|
|
|
|
|
dynamic_cast<const Interpreter::QmlEnumValue *>(value)) {
|
2010-08-27 12:11:55 +02:00
|
|
|
setToolTip(enumValue->name());
|
2010-01-27 14:00:18 +01:00
|
|
|
}
|
|
|
|
|
|
2010-08-27 12:11:55 +02:00
|
|
|
if (toolTip().isEmpty()) {
|
|
|
|
|
QString typeId = context->engine()->typeId(value);
|
|
|
|
|
if (typeId != QLatin1String("undefined"))
|
|
|
|
|
setToolTip(typeId);
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-27 14:56:22 +01:00
|
|
|
|
2010-11-17 14:48:25 +01:00
|
|
|
// if node refers to a property, its name and defining object are returned - otherwise zero
|
|
|
|
|
static const Interpreter::ObjectValue *isMember(const LookupContext::Ptr &lookupContext,
|
|
|
|
|
AST::Node *node, QString *name)
|
2010-08-27 12:11:55 +02:00
|
|
|
{
|
2010-11-17 14:48:25 +01:00
|
|
|
const Interpreter::ObjectValue *owningObject = 0;
|
|
|
|
|
if (AST::IdentifierExpression *identExp = AST::cast<AST::IdentifierExpression *>(node)) {
|
|
|
|
|
if (!identExp->name)
|
|
|
|
|
return 0;
|
|
|
|
|
*name = identExp->name->asString();
|
|
|
|
|
lookupContext->context()->lookup(*name, &owningObject);
|
|
|
|
|
} else if (AST::FieldMemberExpression *fme = AST::cast<AST::FieldMemberExpression *>(node)) {
|
|
|
|
|
if (!fme->base || !fme->name)
|
|
|
|
|
return 0;
|
|
|
|
|
*name = fme->name->asString();
|
|
|
|
|
const Interpreter::Value *base = lookupContext->evaluate(fme->base);
|
|
|
|
|
if (!base)
|
|
|
|
|
return 0;
|
|
|
|
|
owningObject = base->asObjectValue();
|
|
|
|
|
if (owningObject)
|
|
|
|
|
owningObject->lookupMember(*name, lookupContext->context(), &owningObject);
|
|
|
|
|
} else if (AST::UiQualifiedId *qid = AST::cast<AST::UiQualifiedId *>(node)) {
|
|
|
|
|
if (!qid->name)
|
|
|
|
|
return 0;
|
|
|
|
|
*name = qid->name->asString();
|
|
|
|
|
const Interpreter::Value *value = lookupContext->context()->lookup(*name, &owningObject);
|
|
|
|
|
for (AST::UiQualifiedId *it = qid->next; it; it = it->next) {
|
|
|
|
|
if (!value)
|
|
|
|
|
return 0;
|
|
|
|
|
const Interpreter::ObjectValue *next = value->asObjectValue();
|
|
|
|
|
if (!next || !it->name)
|
|
|
|
|
return 0;
|
|
|
|
|
*name = it->name->asString();
|
|
|
|
|
value = next->lookupMember(*name, lookupContext->context(), &owningObject);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return owningObject;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEditor::HelpItem HoverHandler::qmlHelpItem(const LookupContext::Ptr &lookupContext,
|
|
|
|
|
AST::Node *node) const
|
|
|
|
|
{
|
|
|
|
|
QString name;
|
|
|
|
|
if (const Interpreter::ObjectValue *scope = isMember(lookupContext, node, &name)) {
|
|
|
|
|
// maybe it's a type?
|
|
|
|
|
if (!name.isEmpty() && name.at(0).isUpper()) {
|
|
|
|
|
const QString maybeHelpId(QLatin1String("QML.") + name);
|
|
|
|
|
if (!Core::HelpManager::instance()->linksForIdentifier(maybeHelpId).isEmpty())
|
|
|
|
|
return TextEditor::HelpItem(maybeHelpId, name, TextEditor::HelpItem::QmlComponent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// otherwise, it's probably a property
|
|
|
|
|
const Interpreter::ObjectValue *lastScope;
|
|
|
|
|
scope->lookupMember(name, lookupContext->context(), &lastScope);
|
|
|
|
|
Interpreter::PrototypeIterator iter(scope, lookupContext->context());
|
|
|
|
|
while (iter.hasNext()) {
|
|
|
|
|
const Interpreter::ObjectValue *cur = iter.next();
|
|
|
|
|
|
|
|
|
|
const QString className = cur->className();
|
|
|
|
|
if (!className.isEmpty()) {
|
|
|
|
|
const QString maybeHelpId(className + QLatin1String("::") + name);
|
|
|
|
|
if (!Core::HelpManager::instance()->linksForIdentifier(maybeHelpId).isEmpty())
|
|
|
|
|
return TextEditor::HelpItem(maybeHelpId, name, TextEditor::HelpItem::QmlProperty);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (cur == lastScope)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return TextEditor::HelpItem();
|
2010-01-27 14:00:18 +01:00
|
|
|
}
|