Files
qt-creator/src/plugins/qmljseditor/qmljshoverhandler.cpp

278 lines
9.4 KiB
C++
Raw Normal View History

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
**
** 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
**
**************************************************************************/
#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
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>
#include <extensionsystem/pluginmanager.h>
#include <qmljs/qmljsinterpreter.h>
#include <qmljs/parser/qmljsast_p.h>
#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>
#include <texteditor/tooltip/tooltip.h>
2009-05-06 15:45:19 +02:00
using namespace Core;
using namespace QmlJS;
using namespace QmlJSEditor;
using namespace QmlJSEditor::Internal;
2009-05-06 15:45:19 +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
{
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
{
QmlJSEditorEditable *qmlEditor = qobject_cast<QmlJSEditorEditable *>(editor);
2010-08-03 15:42:14 +02:00
if (qmlEditor)
return true;
return false;
}
void HoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
{
2009-10-08 13:48:39 +02:00
if (!m_modelManager)
return;
QmlJSTextEditor *qmlEditor = qobject_cast<QmlJSTextEditor *>(editor->widget());
if (!qmlEditor)
2009-10-08 13:48:39 +02:00
return;
if (!matchDiagnosticMessage(qmlEditor, pos)) {
const SemanticInfo &semanticInfo = qmlEditor->semanticInfo();
if (semanticInfo.revision() != qmlEditor->editorRevision())
return;
QList<AST::Node *> astPath = semanticInfo.astPath(pos);
if (astPath.isEmpty())
return;
2009-10-08 13:48:39 +02:00
const Snapshot &snapshot = semanticInfo.snapshot;
const Document::Ptr qmlDocument = semanticInfo.document;
LookupContext::Ptr lookupContext = LookupContext::create(qmlDocument, snapshot, astPath);
if (!matchColorItem(lookupContext, qmlDocument, astPath, pos))
handleOrdinaryMatch(lookupContext, semanticInfo.nodeUnderCursor(pos));
}
}
bool HoverHandler::matchDiagnosticMessage(QmlJSTextEditor *qmlEditor, int pos)
{
foreach (const QTextEdit::ExtraSelection &sel,
qmlEditor->extraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection)) {
if (pos >= sel.cursor.selectionStart() && pos <= sel.cursor.selectionEnd()) {
2010-08-03 15:42:14 +02:00
setToolTip(sel.format.toolTip());
return true;
2009-10-08 13:48:39 +02:00
}
}
return false;
}
2009-10-08 13:48:39 +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());
}
}
if (!color.isEmpty()) {
color.remove(QLatin1Char('\''));
color.remove(QLatin1Char('\"'));
color.remove(QLatin1Char(';'));
2009-10-08 13:48:39 +02:00
m_colorTip = QmlJS::toQColor(color);
if (m_colorTip.isValid()) {
2010-08-03 15:42:14 +02:00
setToolTip(color);
return true;
2009-10-08 13:48:39 +02:00
}
}
return false;
}
2009-10-08 13:48:39 +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-03 15:42:14 +02:00
setToolTip(prettyPrint(value, lookupContext->context()));
}
}
2009-10-08 13:48:39 +02:00
2010-08-03 15:42:14 +02:00
void HoverHandler::resetExtras()
{
m_colorTip = QColor();
}
void HoverHandler::evaluateHelpCandidates()
{
2010-08-03 15:42:14 +02:00
for (int i = 0; i < helpCandidates().size(); ++i) {
HelpCandidate helpCandidate = helpCandidates().at(i);
helpCandidate.m_helpId.prepend(QLatin1String("QML."));
if (helpIdExists(helpCandidate.m_helpId)) {
setMatchingHelpCandidate(i);
setHelpCandidate(helpCandidate, i);
break;
2009-10-08 13:48:39 +02:00
}
}
}
2010-08-03 15:42:14 +02:00
void HoverHandler::decorateToolTip(TextEditor::ITextEditor *editor)
{
if (matchingHelpCandidate() != -1) {
const QString &contents = getDocContents(extendToolTips(editor));
if (!contents.isEmpty()) {
appendToolTip(contents);
} else {
QString tip = Qt::escape(toolTip());
tip.prepend(QLatin1String("<nobr>"));
tip.append(QLatin1String("</nobr>"));
setToolTip(tip);
}
}
}
void HoverHandler::operateTooltip(TextEditor::ITextEditor *editor, const QPoint &point)
{
if (toolTip().isEmpty())
TextEditor::ToolTip::instance()->hide();
else {
if (m_colorTip.isValid()) {
TextEditor::ToolTip::instance()->showColor(point, m_colorTip, editor->widget());
} else {
if (matchingHelpCandidate() != -1)
addF1ToToolTip();
TextEditor::ToolTip::instance()->showText(point, toolTip(), editor->widget());
}
}
}
QString HoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value,
QmlJS::Interpreter::Context *context)
{
if (! value)
return QString();
if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
do {
const QString className = objectValue->className();
if (! className.isEmpty())
2010-08-03 15:42:14 +02:00
addHelpCandidate(HelpCandidate(className, HelpCandidate::QML));
objectValue = objectValue->prototype(context);
} while (objectValue);
2010-08-03 15:42:14 +02:00
if (! helpCandidates().isEmpty())
return helpCandidates().first().m_helpId;
} else if (const Interpreter::QmlEnumValue *enumValue =
dynamic_cast<const Interpreter::QmlEnumValue *>(value)) {
return enumValue->name();
}
QString typeId = context->engine()->typeId(value);
if (typeId == QLatin1String("undefined"))
typeId.clear();
return typeId;
}