2009-05-06 15:45:19 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
2010-03-05 11:25:49 +01:00
|
|
|
** Copyright (c) 2010 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
|
|
|
**
|
|
|
|
|
** Commercial Usage
|
|
|
|
|
**
|
|
|
|
|
** Licensees holding valid Qt Commercial licenses may use this file in
|
|
|
|
|
** accordance with the Qt Commercial License Agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and Nokia.
|
|
|
|
|
**
|
|
|
|
|
** 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.
|
|
|
|
|
**
|
|
|
|
|
** If you are unsure which license is appropriate for your use, please
|
2009-08-14 09:30:56 +02:00
|
|
|
** contact the sales department at http://qt.nokia.com/contact.
|
2009-05-06 15:45:19 +02:00
|
|
|
**
|
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
2010-01-15 17:20:03 +01:00
|
|
|
#include "qmljseditor.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
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
2010-06-11 13:11:37 +02:00
|
|
|
#include <coreplugin/helpmanager.h>
|
2009-05-06 15:45:19 +02:00
|
|
|
#include <coreplugin/uniqueidmanager.h>
|
|
|
|
|
#include <coreplugin/editormanager/editormanager.h>
|
2009-12-02 17:59:27 +01:00
|
|
|
#include <debugger/debuggerconstants.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-02 14:50:38 +02:00
|
|
|
#include <texteditor/tooltip/tooltip.h>
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HoverHandler::HoverHandler(QObject *parent) :
|
|
|
|
|
QObject(parent), m_modelManager(0), m_matchingHelpCandidate(-1)
|
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-10-08 13:48:39 +02:00
|
|
|
|
2009-05-06 15:45:19 +02:00
|
|
|
// Listen for editor opened events in order to connect to tooltip/helpid requests
|
2010-06-11 13:11:37 +02:00
|
|
|
connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
|
2009-05-06 15:45:19 +02:00
|
|
|
this, SLOT(editorOpened(Core::IEditor *)));
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 13:41:51 +01:00
|
|
|
void HoverHandler::editorOpened(IEditor *editor)
|
2009-05-06 15:45:19 +02:00
|
|
|
{
|
2010-01-15 17:20:03 +01:00
|
|
|
QmlJSEditorEditable *qmlEditor = qobject_cast<QmlJSEditorEditable *>(editor);
|
2009-09-30 17:43:21 +02:00
|
|
|
if (!qmlEditor)
|
2009-05-06 15:45:19 +02:00
|
|
|
return;
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
connect(qmlEditor, SIGNAL(tooltipRequested(TextEditor::ITextEditor*, QPoint, int)),
|
2009-05-06 15:45:19 +02:00
|
|
|
this, SLOT(showToolTip(TextEditor::ITextEditor*, QPoint, int)));
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
connect(qmlEditor, SIGNAL(contextHelpIdRequested(TextEditor::ITextEditor*, int)),
|
2009-05-06 15:45:19 +02:00
|
|
|
this, SLOT(updateContextHelpId(TextEditor::ITextEditor*, int)));
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 13:41:51 +01:00
|
|
|
void HoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint &point, int pos)
|
2009-05-06 15:45:19 +02:00
|
|
|
{
|
2010-08-02 14:50:38 +02:00
|
|
|
if (!editor)
|
2009-05-06 15:45:19 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ICore *core = ICore::instance();
|
2010-08-02 14:50:38 +02:00
|
|
|
const int dbgcontext =
|
|
|
|
|
core->uniqueIDManager()->uniqueIdentifier(Debugger::Constants::C_DEBUGMODE);
|
2009-05-06 15:45:19 +02:00
|
|
|
if (core->hasContext(dbgcontext))
|
|
|
|
|
return;
|
|
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
editor->setContextHelpId(QString());
|
|
|
|
|
|
|
|
|
|
identifyMatch(editor, pos);
|
2009-05-06 15:45:19 +02:00
|
|
|
|
|
|
|
|
if (m_toolTip.isEmpty())
|
2010-08-02 14:50:38 +02:00
|
|
|
TextEditor::ToolTip::instance()->hide();
|
2009-05-06 15:45:19 +02:00
|
|
|
else {
|
2010-08-02 14:50:38 +02:00
|
|
|
const QPoint &pnt = point - QPoint(0,
|
2009-05-06 15:45:19 +02:00
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
|
24
|
|
|
|
|
#else
|
|
|
|
|
16
|
|
|
|
|
#endif
|
|
|
|
|
);
|
|
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
if (m_colorTip.isValid()) {
|
|
|
|
|
TextEditor::ToolTip::instance()->showColor(pnt, m_colorTip, editor->widget());
|
|
|
|
|
} else {
|
|
|
|
|
m_toolTip = Qt::escape(m_toolTip);
|
|
|
|
|
if (m_matchingHelpCandidate != -1) {
|
|
|
|
|
m_toolTip = QString::fromUtf8(
|
|
|
|
|
"<table><tr><td valign=middle><nobr>%1</td><td>"
|
|
|
|
|
"<img src=\":/cppeditor/images/f1.png\"></td></tr></table>").arg(m_toolTip);
|
|
|
|
|
} else {
|
|
|
|
|
m_toolTip = QString::fromUtf8("<nobr>%1</nobr>").arg(m_toolTip);
|
|
|
|
|
}
|
|
|
|
|
TextEditor::ToolTip::instance()->showText(pnt, m_toolTip, editor->widget());
|
|
|
|
|
}
|
2009-05-06 15:45:19 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-15 13:41:51 +01:00
|
|
|
void HoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int pos)
|
2009-10-08 13:48:39 +02:00
|
|
|
{
|
2010-08-02 14:50:38 +02:00
|
|
|
// If the tooltip is visible and there is a help match, use it to update the help id.
|
|
|
|
|
// Otherwise, identify the match.
|
|
|
|
|
if (!TextEditor::ToolTip::instance()->isVisible() || m_matchingHelpCandidate == -1)
|
|
|
|
|
identifyMatch(editor, pos);
|
|
|
|
|
|
|
|
|
|
if (m_matchingHelpCandidate != -1)
|
|
|
|
|
editor->setContextHelpId(m_helpCandidates.at(m_matchingHelpCandidate));
|
|
|
|
|
else
|
|
|
|
|
editor->setContextHelpId(QString());
|
2009-10-08 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
void HoverHandler::resetMatchings()
|
2009-10-08 13:48:39 +02:00
|
|
|
{
|
2010-08-02 14:50:38 +02:00
|
|
|
m_matchingHelpCandidate = -1;
|
|
|
|
|
m_helpCandidates.clear();
|
2009-10-08 13:48:39 +02:00
|
|
|
m_toolTip.clear();
|
2010-08-02 14:50:38 +02:00
|
|
|
m_colorTip = QColor();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
|
|
|
|
|
{
|
|
|
|
|
resetMatchings();
|
2009-10-08 13:48:39 +02:00
|
|
|
|
|
|
|
|
if (!m_modelManager)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
QmlJSTextEditor *qmlEditor = qobject_cast<QmlJSTextEditor *>(editor->widget());
|
|
|
|
|
if (!qmlEditor)
|
2009-10-08 13:48:39 +02:00
|
|
|
return;
|
|
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
if (!matchDiagnosticMessage(qmlEditor, pos)) {
|
|
|
|
|
const SemanticInfo &semanticInfo = qmlEditor->semanticInfo();
|
|
|
|
|
if (semanticInfo.revision() != qmlEditor->editorRevision())
|
|
|
|
|
return;
|
2010-02-02 15:01:42 +01:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
QList<AST::Node *> astPath = semanticInfo.astPath(pos);
|
|
|
|
|
if (astPath.isEmpty())
|
|
|
|
|
return;
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
const Snapshot &snapshot = semanticInfo.snapshot;
|
|
|
|
|
const Document::Ptr qmlDocument = semanticInfo.document;
|
|
|
|
|
LookupContext::Ptr lookupContext = LookupContext::create(qmlDocument, snapshot, astPath);
|
2010-02-02 15:01:42 +01:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
if (!matchColorItem(lookupContext, qmlDocument, astPath, pos))
|
|
|
|
|
handleOrdinaryMatch(lookupContext, semanticInfo.nodeUnderCursor(pos));
|
|
|
|
|
}
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
evaluateHelpCandidates();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HoverHandler::matchDiagnosticMessage(QmlJSTextEditor *qmlEditor, int pos)
|
|
|
|
|
{
|
|
|
|
|
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()) {
|
|
|
|
|
m_toolTip = 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()) {
|
|
|
|
|
m_toolTip = color;
|
|
|
|
|
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);
|
|
|
|
|
m_toolTip = prettyPrint(value, lookupContext->context());
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
void HoverHandler::evaluateHelpCandidates()
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < m_helpCandidates.size(); ++i) {
|
|
|
|
|
QString helpId = m_helpCandidates.at(i);
|
|
|
|
|
helpId.prepend(QLatin1String("QML."));
|
|
|
|
|
if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty()) {
|
|
|
|
|
m_matchingHelpCandidate = i;
|
|
|
|
|
m_helpCandidates[i] = helpId;
|
|
|
|
|
break;
|
2009-10-08 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-01-27 14:00:18 +01:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
QString HoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value,
|
|
|
|
|
QmlJS::Interpreter::Context *context)
|
2010-01-27 14:00:18 +01:00
|
|
|
{
|
2010-02-03 15:39:57 +01:00
|
|
|
if (! value)
|
2010-01-27 14:00:18 +01:00
|
|
|
return QString();
|
|
|
|
|
|
|
|
|
|
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
|
2010-01-27 14:56:22 +01:00
|
|
|
do {
|
|
|
|
|
const QString className = objectValue->className();
|
|
|
|
|
|
|
|
|
|
if (! className.isEmpty())
|
2010-08-02 14:50:38 +02:00
|
|
|
m_helpCandidates.append(className);
|
2010-01-27 14:00:18 +01:00
|
|
|
|
2010-02-03 15:39:57 +01:00
|
|
|
objectValue = objectValue->prototype(context);
|
2010-01-27 14:56:22 +01:00
|
|
|
} while (objectValue);
|
2010-01-27 14:00:18 +01:00
|
|
|
|
2010-08-02 14:50:38 +02:00
|
|
|
if (! m_helpCandidates.isEmpty())
|
|
|
|
|
return m_helpCandidates.first();
|
|
|
|
|
} else if (const Interpreter::QmlEnumValue *enumValue =
|
|
|
|
|
dynamic_cast<const Interpreter::QmlEnumValue *>(value)) {
|
2010-04-29 15:52:17 +02:00
|
|
|
return enumValue->name();
|
2010-01-27 14:00:18 +01:00
|
|
|
}
|
|
|
|
|
|
2010-02-03 15:39:57 +01:00
|
|
|
QString typeId = context->engine()->typeId(value);
|
2010-01-27 14:56:22 +01:00
|
|
|
|
2010-01-28 15:35:00 +01:00
|
|
|
if (typeId == QLatin1String("undefined"))
|
|
|
|
|
typeId.clear();
|
2010-01-27 14:56:22 +01:00
|
|
|
|
|
|
|
|
return typeId;
|
2010-01-27 14:00:18 +01:00
|
|
|
}
|