2009-05-06 15:45:19 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator
|
|
|
|
|
**
|
|
|
|
|
** Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
|
|
|
**
|
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"
|
|
|
|
|
#include "qmlhoverhandler.h"
|
2009-05-06 15:45:19 +02:00
|
|
|
|
|
|
|
|
#include <coreplugin/icore.h>
|
|
|
|
|
#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/qmljsbind.h>
|
2010-02-15 11:52:39 +01:00
|
|
|
#include <qmljs/qmljsevaluate.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>
|
2009-05-06 15:45:19 +02:00
|
|
|
#include <texteditor/itexteditor.h>
|
|
|
|
|
#include <texteditor/basetexteditor.h>
|
|
|
|
|
|
2009-10-08 13:51:00 +02:00
|
|
|
#include <QtCore/QDebug>
|
2009-05-06 15:45:19 +02:00
|
|
|
#include <QtCore/QDir>
|
|
|
|
|
#include <QtCore/QFileInfo>
|
|
|
|
|
#include <QtCore/QSettings>
|
|
|
|
|
#include <QtGui/QToolTip>
|
|
|
|
|
#include <QtGui/QTextCursor>
|
|
|
|
|
#include <QtGui/QTextBlock>
|
|
|
|
|
#include <QtHelp/QHelpEngineCore>
|
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
QmlHoverHandler::QmlHoverHandler(QObject *parent)
|
2009-05-06 15:45:19 +02:00
|
|
|
: QObject(parent)
|
2009-10-08 13:48:39 +02:00
|
|
|
, m_helpEngineNeedsSetup(false)
|
2009-05-06 15:45:19 +02:00
|
|
|
{
|
2009-10-08 13:48:39 +02:00
|
|
|
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<QmlModelManagerInterface>();
|
|
|
|
|
|
2009-05-06 15:45:19 +02:00
|
|
|
ICore *core = ICore::instance();
|
2009-10-08 13:48:39 +02:00
|
|
|
QFileInfo fi(core->settings()->fileName());
|
|
|
|
|
// FIXME shouldn't the help engine create the directory if it doesn't exist?
|
|
|
|
|
QDir directory(fi.absolutePath()+"/qtcreator");
|
|
|
|
|
if (!directory.exists())
|
|
|
|
|
directory.mkpath(directory.absolutePath());
|
|
|
|
|
|
|
|
|
|
m_helpEngine = new QHelpEngineCore(directory.absolutePath()
|
|
|
|
|
+ QLatin1String("/helpcollection.qhc"), this);
|
|
|
|
|
//m_helpEngine->setAutoSaveFilter(false);
|
|
|
|
|
if (!m_helpEngine->setupData())
|
|
|
|
|
qWarning() << "Could not initialize help engine:" << m_helpEngine->error();
|
|
|
|
|
m_helpEngine->setCurrentFilter(tr("Unfiltered"));
|
|
|
|
|
m_helpEngineNeedsSetup = m_helpEngine->registeredDocumentations().count() == 0;
|
2009-05-06 15:45:19 +02:00
|
|
|
|
|
|
|
|
// Listen for editor opened events in order to connect to tooltip/helpid requests
|
|
|
|
|
connect(core->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
|
|
|
|
|
this, SLOT(editorOpened(Core::IEditor *)));
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlHoverHandler::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)));
|
|
|
|
|
}
|
|
|
|
|
|
2009-09-30 17:43:21 +02:00
|
|
|
void QmlHoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint &point, int pos)
|
2009-05-06 15:45:19 +02:00
|
|
|
{
|
|
|
|
|
if (! editor)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
ICore *core = ICore::instance();
|
|
|
|
|
const int dbgcontext = core->uniqueIDManager()->uniqueIdentifier(Debugger::Constants::C_GDBDEBUGGER);
|
|
|
|
|
|
|
|
|
|
if (core->hasContext(dbgcontext))
|
|
|
|
|
return;
|
|
|
|
|
|
2009-10-08 13:48:39 +02:00
|
|
|
updateHelpIdAndTooltip(editor, pos);
|
2009-05-06 15:45:19 +02:00
|
|
|
|
|
|
|
|
if (m_toolTip.isEmpty())
|
|
|
|
|
QToolTip::hideText();
|
|
|
|
|
else {
|
|
|
|
|
const QPoint pnt = point - QPoint(0,
|
|
|
|
|
#ifdef Q_WS_WIN
|
|
|
|
|
24
|
|
|
|
|
#else
|
|
|
|
|
16
|
|
|
|
|
#endif
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
QToolTip::showText(pnt, m_toolTip);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-08 13:48:39 +02:00
|
|
|
void QmlHoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int pos)
|
|
|
|
|
{
|
|
|
|
|
updateHelpIdAndTooltip(editor, pos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos)
|
|
|
|
|
{
|
|
|
|
|
m_helpId.clear();
|
|
|
|
|
m_toolTip.clear();
|
|
|
|
|
|
|
|
|
|
if (!m_modelManager)
|
|
|
|
|
return;
|
|
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
QmlJSTextEditor *edit = qobject_cast<QmlJSTextEditor *>(editor->widget());
|
|
|
|
|
if (!edit)
|
2009-10-08 13:48:39 +02:00
|
|
|
return;
|
|
|
|
|
|
2010-02-02 10:41:48 +01:00
|
|
|
const SemanticInfo semanticInfo = edit->semanticInfo();
|
2010-02-02 15:01:42 +01:00
|
|
|
|
|
|
|
|
if (semanticInfo.revision() != edit->documentRevision())
|
2010-01-27 12:40:02 +01:00
|
|
|
return;
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-02-02 15:01:42 +01:00
|
|
|
const Snapshot snapshot = semanticInfo.snapshot;
|
|
|
|
|
const Document::Ptr qmlDocument = semanticInfo.document;
|
|
|
|
|
|
2010-01-27 14:56:22 +01:00
|
|
|
if (m_helpEngineNeedsSetup && m_helpEngine->registeredDocumentations().count() > 0) {
|
|
|
|
|
m_helpEngine->setupData();
|
|
|
|
|
m_helpEngineNeedsSetup = false;
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-08 13:48:39 +02:00
|
|
|
// We only want to show F1 if the tooltip matches the help id
|
|
|
|
|
bool showF1 = true;
|
|
|
|
|
|
2010-01-27 12:40:02 +01:00
|
|
|
foreach (const QTextEdit::ExtraSelection &sel, edit->extraSelections(TextEditor::BaseTextEditor::CodeWarningsSelection)) {
|
2010-01-26 14:03:37 +01:00
|
|
|
if (pos >= sel.cursor.selectionStart() && pos <= sel.cursor.selectionEnd()) {
|
2009-10-08 13:48:39 +02:00
|
|
|
showF1 = false;
|
2010-01-26 14:03:37 +01:00
|
|
|
m_toolTip = sel.format.toolTip();
|
2009-10-08 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-07 12:14:35 +01:00
|
|
|
QString symbolName = QLatin1String("<unknown>");
|
2009-10-08 13:48:39 +02:00
|
|
|
if (m_helpId.isEmpty()) {
|
2010-02-02 13:18:56 +01:00
|
|
|
AST::Node *node = semanticInfo.nodeUnderCursor(pos);
|
|
|
|
|
if (node && !(AST::cast<AST::StringLiteral *>(node) != 0 || AST::cast<AST::NumericLiteral *>(node) != 0)) {
|
2010-02-02 16:36:14 +01:00
|
|
|
AST::Node *declaringMember = semanticInfo.declaringMember(pos);
|
2010-02-02 13:18:56 +01:00
|
|
|
|
|
|
|
|
Interpreter::Engine interp;
|
2010-02-04 10:19:37 +01:00
|
|
|
Interpreter::Context context(&interp);
|
|
|
|
|
context.build(declaringMember, qmlDocument, snapshot);
|
2010-02-02 13:18:56 +01:00
|
|
|
|
2010-02-15 11:52:39 +01:00
|
|
|
Evaluate check(&context);
|
2010-02-03 10:59:52 +01:00
|
|
|
const Interpreter::Value *value = check(node);
|
2010-02-02 13:18:56 +01:00
|
|
|
|
|
|
|
|
QStringList baseClasses;
|
2010-02-04 10:19:37 +01:00
|
|
|
m_toolTip = prettyPrint(value, &context, &baseClasses);
|
2010-02-02 13:18:56 +01:00
|
|
|
|
|
|
|
|
foreach (const QString &baseClass, baseClasses) {
|
|
|
|
|
QString helpId = QLatin1String("QML.");
|
|
|
|
|
helpId += baseClass;
|
2009-10-08 13:48:39 +02:00
|
|
|
|
2010-02-02 13:18:56 +01:00
|
|
|
if (! m_helpEngine->linksForIdentifier(helpId).isEmpty()) {
|
|
|
|
|
m_helpId = helpId;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2010-01-27 14:56:22 +01:00
|
|
|
}
|
2009-10-08 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!m_toolTip.isEmpty())
|
|
|
|
|
m_toolTip = Qt::escape(m_toolTip);
|
|
|
|
|
|
2010-01-27 14:56:22 +01:00
|
|
|
if (!m_helpId.isEmpty()) {
|
2009-10-08 13:48:39 +02:00
|
|
|
if (showF1) {
|
2010-01-27 14:56:22 +01:00
|
|
|
m_toolTip = QString::fromUtf8("<table><tr><td valign=middle><nobr>%1</td>"
|
|
|
|
|
"<td><img src=\":/cppeditor/images/f1.svg\"></td></tr></table>")
|
2009-10-08 13:48:39 +02:00
|
|
|
.arg(m_toolTip);
|
|
|
|
|
}
|
|
|
|
|
editor->setContextHelpId(m_helpId);
|
|
|
|
|
} else if (!m_toolTip.isEmpty()) {
|
2010-01-27 14:56:22 +01:00
|
|
|
m_toolTip = QString::fromUtf8("<nobr>%1").arg(m_toolTip);
|
2009-10-08 13:48:39 +02:00
|
|
|
} else if (!m_helpId.isEmpty()) {
|
2010-01-27 14:56:22 +01:00
|
|
|
m_toolTip = QString::fromUtf8("<nobr>No help available for \"%1\"").arg(symbolName);
|
2009-10-08 13:48:39 +02:00
|
|
|
}
|
|
|
|
|
}
|
2010-01-27 14:00:18 +01:00
|
|
|
|
2010-02-03 15:39:57 +01:00
|
|
|
QString QmlHoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Context *context,
|
2010-01-27 14:56:22 +01:00
|
|
|
QStringList *baseClasses) const
|
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())
|
|
|
|
|
baseClasses->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-01-27 14:56:22 +01:00
|
|
|
if (! baseClasses->isEmpty())
|
|
|
|
|
return baseClasses->first();
|
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
|
|
|
}
|