Re-write of cpp hover handler and tooltip integration with qtdocs.

This commit is contained in:
Leandro Melo
2010-07-12 13:24:45 +02:00
parent 36f5545f5a
commit 123c10a77b
7 changed files with 863 additions and 209 deletions

View File

@@ -0,0 +1,417 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** 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
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "htmldocextractor.h"
#include <QtCore/QLatin1String>
#include <QtCore/QLatin1Char>
#include <QtCore/QStringList>
#include <QtCore/QRegExp>
using namespace Utils;
namespace {
QRegExp createMinimalExp(const QString &pattern) {
QRegExp exp(pattern);
exp.setMinimal(true);
return exp;
}
}
HtmlDocExtractor::HtmlDocExtractor() :
m_lengthReference(-1),
m_truncateAtParagraph(false),
m_formatContents(true)
{}
void HtmlDocExtractor::setLengthReference(const int length, const bool truncateAtParagraph)
{
m_lengthReference = length;
m_truncateAtParagraph = truncateAtParagraph;
}
void HtmlDocExtractor::setFormatContents(const bool format)
{ m_formatContents = format; }
QString HtmlDocExtractor::assemble(const QString &elementAttr,
const QString &elementTemplate) const
{
const QString &cleanAttr = cleanReference(elementAttr);
return QString(elementTemplate).arg(cleanAttr);
}
QString HtmlDocExtractor::getClassOrNamespaceBrief(const QString &html, const QString &name) const
{
QString contents = getContentsByMarks(html, name + QLatin1String("-brief"), false);
if (contents.isEmpty()) {
QLatin1String pattern("<h1 class=\"title\">.*</p>");
contents = findByPattern(html, pattern);
if (!contents.isEmpty())
contents.remove(QRegExp(QLatin1String("<h1.*</h1>")));
}
if (!contents.isEmpty()) {
contents.remove(QLatin1String("<a href=\"#details\">More...</a>"));
if (m_formatContents) {
contents.prepend(QLatin1String("<nobr>"));
contents.append(QLatin1String("</nobr>"));
}
}
formatContents(&contents);
return contents;
}
QString HtmlDocExtractor::getClassOrNamespaceDescription(const QString &html,
const QString &name) const
{
QString contents = getContentsByMarks(html, name + QLatin1String("-description"), false);
if (contents.isEmpty()) {
QLatin1String pattern("<a name=\"details\"></a>.*<hr />.*<hr />");
contents = findByPattern(html, pattern);
}
if (!contents.isEmpty())
contents.replace(QLatin1String("<h2>Detailed Description</h2>"), name);
formatContents(&contents);
return contents;
}
QString HtmlDocExtractor::getEnumDescription(const QString &html, const QString &name) const
{
const QString &enumm = name + QLatin1String("-enum");
QString contents = getClassOrNamespaceMemberDescription(html, name, enumm, false);
formatContents(&contents);
return contents;
}
QString HtmlDocExtractor::getTypedefDescription(const QString &html, const QString &name) const
{
const QString &typedeff = name + QLatin1String("-typedef");
QString contents = getClassOrNamespaceMemberDescription(html, name, typedeff, false);
formatContents(&contents);
return contents;
}
QString HtmlDocExtractor::getVarDescription(const QString &html, const QString &name) const
{
const QString &var = name + QLatin1String("-var");
QString contents = getClassOrNamespaceMemberDescription(html, name, var, false);
formatContents(&contents);
return contents;
}
QString HtmlDocExtractor::getMacroDescription(const QString &html,
const QString &mark,
const QString &anchorName) const
{
QString contents = getClassOrNamespaceMemberDescription(html, mark, anchorName, false, true);
formatContents(&contents);
return contents;
}
QString HtmlDocExtractor::getFunctionDescription(const QString &html,
const QString &mark,
const QString &anchorName,
const bool mainOverload) const
{
QString contents = getClassOrNamespaceMemberDescription(html, mark, anchorName, mainOverload);
if (contents.isEmpty()) {
// Maybe marks are not present and/or this is a property. Besides setX/isX/hasX there are
// other (not so usual) names for property based functions. A few examples of those:
// - toPlainText / Prop. plainText from QPlainTextEdit.
// - resize / Prop. size from QWidget.
// - move / Prop. pos from QWidget (nothing similar in the names in this case).
// So I try to find the link to this property in the list of properties, extract its
// anchor and then follow by the name found.
QString pattern = assemble(anchorName,
QLatin1String("<a href=\"[a-z\\.]+#([A-Za-z]+-prop)\">%1</a>"));
QRegExp exp = createMinimalExp(pattern);
if (exp.indexIn(html) != -1) {
const QString &prop = exp.cap(1);
contents = getClassOrNamespaceMemberDescription(html, prop, prop, false);
}
}
formatContents(&contents);
return contents;
}
QString HtmlDocExtractor::getClassOrNamespaceMemberDescription(const QString &html,
const QString &mark,
const QString &anchorName,
const bool mainOverload,
const bool relaxedMatch) const
{
// Try with extraction marks (present in newer verions of the docs). If nothing is found try
// with the anchor.
QString contents;
if (!mark.isEmpty())
contents = getContentsByMarks(html, mark, mainOverload);
if (contents.isEmpty())
contents = getContentsByAnchor(html, anchorName, relaxedMatch);
return contents;
}
QString HtmlDocExtractor::getContentsByAnchor(const QString &html,
const QString &name,
const bool relaxedMatch) const
{
// This approach is not very accurate.
QString pattern;
if (relaxedMatch) {
pattern = QLatin1String(
"(?:<h3 class=\"[a-z]+\">)?<a name=\"%1.*(?:<h3 class|<p />|<hr />|</div>)");
} else {
// When there are duplicates the HTML generator incrementally appends 'x' to references.
pattern = QLatin1String(
"(?:<h3 class=\"[a-z]+\">)?<a name=\"%1x*\">.*(?:<h3 class|<p />|<hr />|</div>)");
}
return findByPattern(html, assemble(name, pattern));
}
QString HtmlDocExtractor::getContentsByMarks(const QString &html,
const QString &id,
const bool mainOverload) const
{
QString endMark;
QString startMark;
if (id.contains(QLatin1Char('('))) {
const int index = id.indexOf(QLatin1Char('('));
startMark = id.left(index);
endMark = startMark;
if (mainOverload) {
startMark.append(QLatin1String("[overload1]"));
} else {
QString complementaryId = id.right(id.length() - index);
complementaryId.remove(QRegExp(QLatin1String("[\\(\\), ]")));
startMark.append(complementaryId);
}
} else {
startMark = id;
}
startMark.prepend(QLatin1String("$$$"));
if (endMark.isEmpty()) {
if (id.contains(QLatin1Char('-'))) {
const int index = id.indexOf(QLatin1Char('-'));
endMark = id.left(index);
} else {
endMark = id;
}
}
endMark.prepend(QLatin1String("<!-- @@@"));
return findByMarks(html, startMark, endMark);
}
QString HtmlDocExtractor::findByMarks(const QString &html,
const QString &startMark,
const QString &endMark) const
{
QString contents;
int start = html.indexOf(startMark);
if (start != -1) {
start = html.indexOf(QLatin1String("-->"), start);
if (start != -1) {
int end = html.indexOf(endMark, start);
if (end != -1) {
start += 3;
contents = html.mid(start, end - start);
}
}
}
return contents;
}
QString HtmlDocExtractor::findByPattern(const QString &html, const QString &pattern) const
{
QRegExp exp(pattern);
exp.setMinimal(true);
const int match = exp.indexIn(html);
if (match != -1)
return html.mid(match, exp.matchedLength());
return QString();
}
void HtmlDocExtractor::formatContents(QString *html) const
{
if (html->isEmpty())
return;
if (m_formatContents) {
replaceNonStyledHeadingsForBold(html);
replaceTablesForSimpleLines(html);
replaceListsForSimpleLines(html);
stripLinks(html);
stripHorizontalLines(html);
stripDivs(html);
stripTagsStyles(html);
stripHeadings(html);
}
if (m_lengthReference > -1 && html->length() > m_lengthReference) {
if (m_truncateAtParagraph) {
const int nextBegin = html->indexOf(QLatin1String("<p>"), m_lengthReference);
QRegExp exp = createMinimalExp(QLatin1String("</p>|<br />"));
const int previousEnd = html->lastIndexOf(exp, m_lengthReference);
if (nextBegin != -1 && previousEnd != -1)
html->truncate(qMin(nextBegin, previousEnd + exp.matchedLength()));
else if (nextBegin != -1 || previousEnd != -1)
html->truncate((nextBegin != -1? nextBegin : previousEnd + exp.matchedLength()));
} else {
html->truncate(m_lengthReference);
}
html->append(QLatin1String("..."));
if (m_formatContents)
html->append(QLatin1String("<br />"));
}
}
void HtmlDocExtractor::stripAllHtml(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<.*>")));
}
void HtmlDocExtractor::stripHeadings(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<h\\d{1}.*>|</h\\d{1}>")));
}
void HtmlDocExtractor::stripLinks(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<a\\s+.*>|</a>")));
}
void HtmlDocExtractor::stripHorizontalLines(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<hr\\s+/>")));
}
void HtmlDocExtractor::stripDivs(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<div\\s+.*>|</div>|<div\\s+.*/\\s*>")));
}
void HtmlDocExtractor::stripTagsStyles(QString *html)
{
const QRegExp &exp = createMinimalExp(QLatin1String("<(.*\\s+)class=\".*\">"));
html->replace(exp, QLatin1String("<\\1>"));
}
void HtmlDocExtractor::stripTeletypes(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<tt>|</tt>")));
}
void HtmlDocExtractor::replaceNonStyledHeadingsForBold(QString *html)
{
const QRegExp &hStart = createMinimalExp(QLatin1String("<h\\d{1}>"));
const QRegExp &hEnd = createMinimalExp(QLatin1String("</h\\d{1}>"));
html->replace(hStart, QLatin1String("<p><b>"));
html->replace(hEnd, QLatin1String("</b></p>"));
}
void HtmlDocExtractor::replaceTablesForSimpleLines(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<table.*>")));
html->remove(QLatin1String("</table>"));
html->remove(createMinimalExp(QLatin1String("<tr.*><th.*>.*</th></tr>")));
html->replace(QLatin1String("</td><td"), QLatin1String("</td>&nbsp;<td"));
html->remove(createMinimalExp(QLatin1String("<td.*>")));
html->remove(QLatin1String("</td>"));
html->replace(QLatin1String("<tr>"), QLatin1String("&nbsp;&nbsp;-&nbsp;"));
html->replace(QLatin1String("</tr>"), QLatin1String("<br />"));
}
void HtmlDocExtractor::replaceListsForSimpleLines(QString *html)
{
html->remove(createMinimalExp(QLatin1String("<(?:ul|ol).*>")));
html->remove(createMinimalExp(QLatin1String("</(?:ul|ol)>")));
html->replace(QLatin1String("<li>"), QLatin1String("&nbsp;&nbsp;-&nbsp;"));
html->replace(QLatin1String("</li>"), QLatin1String("<br />"));
}
/*
@todo: We need to clean the anchor in the same way qtdoc does. Currently, this method is a
duplicate of HtmlGenerator::cleanRef. It would be good to reuse the same code either by exposing
parts of qtdocs or by refactoring the behavior to use some Qt component for example.
*/
QString HtmlDocExtractor::cleanReference(const QString &reference)
{
QString clean;
if (reference.isEmpty())
return clean;
clean.reserve(reference.size() + 20);
const QChar c = reference[0];
const uint u = c.unicode();
if ((u >= QLatin1Char('a') && u <= QLatin1Char('z')) ||
(u >= QLatin1Char('A') && u <= QLatin1Char('Z')) ||
(u >= QLatin1Char('0') && u <= QLatin1Char('9'))) {
clean += c;
} else if (u == QLatin1Char('~')) {
clean += QLatin1String("dtor.");
} else if (u == QLatin1Char('_')) {
clean += QLatin1String("underscore.");
} else {
clean += QLatin1String("A");
}
for (int i = 1; i < (int) reference.length(); i++) {
const QChar c = reference[i];
const uint u = c.unicode();
if ((u >= QLatin1Char('a') && u <= QLatin1Char('z')) ||
(u >= QLatin1Char('A') && u <= QLatin1Char('Z')) ||
(u >= QLatin1Char('0') && u <= QLatin1Char('9')) || u == QLatin1Char('-') ||
u == QLatin1Char('_') || u == QLatin1Char(':') || u == QLatin1Char('.')) {
clean += c;
} else if (c.isSpace()) {
clean += QLatin1String("-");
} else if (u == QLatin1Char('!')) {
clean += QLatin1String("-not");
} else if (u == QLatin1Char('&')) {
clean += QLatin1String("-and");
} else if (u == QLatin1Char('<')) {
clean += QLatin1String("-lt");
} else if (u == QLatin1Char('=')) {
clean += QLatin1String("-eq");
} else if (u == QLatin1Char('>')) {
clean += QLatin1String("-gt");
} else if (u == QLatin1Char('#')) {
clean += QLatin1String("#");
} else {
clean += QLatin1String("-");
clean += QString::number((int)u, 16);
}
}
return clean;
}

View File

@@ -0,0 +1,101 @@
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** 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
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#ifndef HTMLDOCEXTRACTOR_H
#define HTMLDOCEXTRACTOR_H
#include "utils_global.h"
#include <QtCore/QString>
namespace Utils {
class QTCREATOR_UTILS_EXPORT HtmlDocExtractor
{
public:
HtmlDocExtractor();
void setLengthReference(const int reference, const bool truncateAtParagraph);
void setFormatContents(const bool format);
QString getClassOrNamespaceBrief(const QString &html, const QString &name) const;
QString getClassOrNamespaceDescription(const QString &html, const QString &name) const;
QString getEnumDescription(const QString &html, const QString &name) const;
QString getTypedefDescription(const QString &html, const QString &name) const;
QString getVarDescription(const QString &html, const QString &name) const;
QString getMacroDescription(const QString &html,
const QString &mark,
const QString &anchorName) const;
QString getFunctionDescription(const QString &html,
const QString &mark,
const QString &anchorName,
const bool mainOverload = true) const;
private:
QString assemble(const QString& elementAttr, const QString &elementTemplate) const;
QString getContentsByAnchor(const QString &html,
const QString &name,
const bool relaxedMatch) const;
QString getContentsByMarks(const QString &html,
const QString &id,
const bool mainOverload) const;
QString getClassOrNamespaceMemberDescription(const QString &html,
const QString &mark,
const QString &anchorName,
const bool mainOverload,
const bool relaxedMatch = false) const;
QString findByMarks(const QString &html,
const QString &startMark,
const QString &endMark) const;
QString findByPattern(const QString &html, const QString &pattern) const;
void formatContents(QString *html) const;
static void stripAllHtml(QString *html);
static void stripHeadings(QString *html);
static void stripLinks(QString *html);
static void stripHorizontalLines(QString *html);
static void stripDivs(QString *html);
static void stripTagsStyles(QString *html);
static void stripTeletypes(QString *html);
static void replaceNonStyledHeadingsForBold(QString *html);
static void replaceTablesForSimpleLines(QString *html);
static void replaceListsForSimpleLines(QString *html);
static QString cleanReference(const QString &reference);
int m_lengthReference;
bool m_truncateAtParagraph;
bool m_formatContents;
};
} // namespace Utils
#endif // HTMLDOCEXTRACTOR_H

View File

@@ -40,7 +40,8 @@ SOURCES += reloadpromptutils.cpp \
detailswidget.cpp \ detailswidget.cpp \
changeset.cpp \ changeset.cpp \
filterlineedit.cpp \ filterlineedit.cpp \
faketooltip.cpp faketooltip.cpp \
htmldocextractor.cpp
win32 { win32 {
SOURCES += abstractprocess_win.cpp \ SOURCES += abstractprocess_win.cpp \
consoleprocess_win.cpp \ consoleprocess_win.cpp \
@@ -93,7 +94,8 @@ HEADERS += utils_global.h \
detailswidget.h \ detailswidget.h \
changeset.h \ changeset.h \
filterlineedit.h \ filterlineedit.h \
faketooltip.h faketooltip.h \
htmldocextractor.h
FORMS += filewizardpage.ui \ FORMS += filewizardpage.ui \
projectintropage.ui \ projectintropage.ui \
newclasswidget.ui \ newclasswidget.ui \

View File

@@ -240,6 +240,13 @@ QUrl HelpManager::findFile(const QUrl &url) const
return m_helpEngine->findFile(url); return m_helpEngine->findFile(url);
} }
QByteArray HelpManager::fileData(const QUrl &url) const
{
if (m_needsSetup)
return QByteArray();
return m_helpEngine->fileData(url);
}
void HelpManager::handleHelpRequest(const QString &url) void HelpManager::handleHelpRequest(const QString &url)
{ {
emit helpRequested(QUrl(url)); emit helpRequested(QUrl(url));

View File

@@ -38,6 +38,7 @@
#include <QtCore/QStringList> #include <QtCore/QStringList>
#include <QtCore/QUrl> #include <QtCore/QUrl>
#include <QtCore/QVariant> #include <QtCore/QVariant>
#include <QtCore/QByteArray>
QT_FORWARD_DECLARE_CLASS(QHelpEngineCore) QT_FORWARD_DECLARE_CLASS(QHelpEngineCore)
QT_FORWARD_DECLARE_CLASS(QSqlQuery) QT_FORWARD_DECLARE_CLASS(QSqlQuery)
@@ -64,6 +65,7 @@ public:
QStringList findKeywords(const QString &key, int maxHits = INT_MAX) const; QStringList findKeywords(const QString &key, int maxHits = INT_MAX) const;
QUrl findFile(const QUrl &url) const; QUrl findFile(const QUrl &url) const;
QByteArray fileData(const QUrl &url) const;
void handleHelpRequest(const QString &url); void handleHelpRequest(const QString &url);
QStringList registeredNamespaces() const; QStringList registeredNamespaces() const;

View File

@@ -29,7 +29,6 @@
#include "cpphoverhandler.h" #include "cpphoverhandler.h"
#include "cppeditor.h" #include "cppeditor.h"
#include "cppplugin.h"
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/helpmanager.h> #include <coreplugin/helpmanager.h>
@@ -40,47 +39,67 @@
#include <texteditor/itexteditor.h> #include <texteditor/itexteditor.h>
#include <texteditor/basetexteditor.h> #include <texteditor/basetexteditor.h>
#include <debugger/debuggerconstants.h> #include <debugger/debuggerconstants.h>
#include <utils/htmldocextractor.h>
#include <CoreTypes.h>
#include <FullySpecifiedType.h> #include <FullySpecifiedType.h>
#include <Literals.h>
#include <Control.h>
#include <Names.h>
#include <Scope.h> #include <Scope.h>
#include <Symbol.h> #include <Symbol.h>
#include <Symbols.h> #include <Symbols.h>
#include <cplusplus/ExpressionUnderCursor.h> #include <cplusplus/ExpressionUnderCursor.h>
#include <cplusplus/Overview.h> #include <cplusplus/Overview.h>
#include <cplusplus/TypeOfExpression.h> #include <cplusplus/TypeOfExpression.h>
#include <cplusplus/SimpleLexer.h> #include <cplusplus/LookupContext.h>
#include <cplusplus/LookupItem.h>
#include <QtCore/QDebug>
#include <QtCore/QDir> #include <QtCore/QDir>
#include <QtCore/QFileInfo> #include <QtCore/QFileInfo>
#include <QtCore/QSettings>
#include <QtGui/QToolTip> #include <QtGui/QToolTip>
#include <QtGui/QTextCursor> #include <QtGui/QTextCursor>
#include <QtGui/QTextBlock>
using namespace CppEditor::Internal; using namespace CppEditor::Internal;
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace Core; using namespace Core;
namespace {
QString removeQualificationIfAny(const QString &name) {
int index = name.lastIndexOf(QLatin1Char(':'));
if (index == -1)
return name;
else
return name.right(name.length() - index - 1);
}
void moveCursorToEndOfQualifiedName(QTextCursor *tc) {
QTextDocument *doc = tc->document();
if (!doc)
return;
while (true) {
const QChar &ch = doc->characterAt(tc->position());
if (ch.isLetterOrNumber() || ch == QLatin1Char('_'))
tc->movePosition(QTextCursor::NextCharacter);
else if (ch == QLatin1Char(':') &&
doc->characterAt(tc->position() + 1) == QLatin1Char(':'))
tc->movePosition(QTextCursor::NextCharacter, QTextCursor::MoveAnchor, 2);
else
break;
}
}
}
CppHoverHandler::CppHoverHandler(QObject *parent) CppHoverHandler::CppHoverHandler(QObject *parent)
: QObject(parent) : QObject(parent), m_modelManager(0), m_matchingHelpCandidate(-1)
{ {
m_modelManager = ExtensionSystem::PluginManager::instance()->getObject<CppTools::CppModelManagerInterface>(); m_modelManager =
ExtensionSystem::PluginManager::instance()->getObject<CppTools::CppModelManagerInterface>();
m_htmlDocExtractor.setLengthReference(1000, true);
// Listen for editor opened events in order to connect to tooltip/helpid requests // Listen for editor opened events in order to connect to tooltip/helpid requests
connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)), connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
this, SLOT(editorOpened(Core::IEditor *))); this, SLOT(editorOpened(Core::IEditor *)));
} }
void CppHoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int pos)
{
updateHelpIdAndTooltip(editor, pos);
}
void CppHoverHandler::editorOpened(IEditor *editor) void CppHoverHandler::editorOpened(IEditor *editor)
{ {
CPPEditorEditable *cppEditor = qobject_cast<CPPEditorEditable *>(editor); CPPEditorEditable *cppEditor = qobject_cast<CPPEditorEditable *>(editor);
@@ -94,22 +113,56 @@ void CppHoverHandler::editorOpened(IEditor *editor)
this, SLOT(updateContextHelpId(TextEditor::ITextEditor*, int))); this, SLOT(updateContextHelpId(TextEditor::ITextEditor*, int)));
} }
void CppHoverHandler::updateContextHelpId(TextEditor::ITextEditor *editor, int pos)
{
if (!editor)
return;
// If the tooltip is visible and there is a help match, this match is used to update the help
// id. Otherwise, the identification process happens.
if (!QToolTip::isVisible() || m_matchingHelpCandidate == -1)
identifyMatch(editor, pos);
if (m_matchingHelpCandidate != -1)
editor->setContextHelpId(m_helpCandidates.at(m_matchingHelpCandidate).m_helpId);
else
editor->setContextHelpId(QString());
}
void CppHoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint &point, int pos) void CppHoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint &point, int pos)
{ {
if (!editor) if (!editor)
return; return;
ICore *core = ICore::instance(); editor->setContextHelpId(QString());
const int dbgcontext = core->uniqueIDManager()->uniqueIdentifier(Debugger::Constants::C_DEBUGMODE);
if (core->hasContext(dbgcontext)) ICore *core = ICore::instance();
const int dbgContext =
core->uniqueIDManager()->uniqueIdentifier(Debugger::Constants::C_DEBUGMODE);
if (core->hasContext(dbgContext))
return; return;
updateHelpIdAndTooltip(editor, pos); identifyMatch(editor, pos);
if (m_toolTip.isEmpty()) if (m_toolTip.isEmpty()) {
QToolTip::hideText(); QToolTip::hideText();
else { } else {
if (m_matchingHelpCandidate != -1) {
const QString &contents = getDocContents();
if (!contents.isEmpty()) {
m_toolTip = contents;
} else {
m_toolTip = Qt::escape(m_toolTip);
m_toolTip.prepend(QLatin1String("<nobr>"));
m_toolTip.append(QLatin1String("</nobr>"));
}
m_toolTip = QString(QLatin1String("<table><tr>"
"<td valign=middle>%1</td>"
"<td><img src=\":/cppeditor/images/f1.png\"></td>"
"</tr></table>")).arg(m_toolTip);
}
const QPoint pnt = point - QPoint(0, const QPoint pnt = point - QPoint(0,
#ifdef Q_WS_WIN #ifdef Q_WS_WIN
24 24
@@ -122,210 +175,239 @@ void CppHoverHandler::showToolTip(TextEditor::ITextEditor *editor, const QPoint
} }
} }
static QString buildHelpId(Symbol *symbol, const Name *declarationName) void CppHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
{ {
Scope *scope = 0; resetMatchings();
if (symbol) {
scope = symbol->scope();
declarationName = symbol->name();
}
if (! declarationName)
return QString();
Overview overview;
overview.setShowArgumentNames(false);
overview.setShowReturnTypes(false);
QStringList qualifiedNames;
qualifiedNames.prepend(overview.prettyName(declarationName));
for (; scope; scope = scope->enclosingScope()) {
Symbol *owner = scope->owner();
if (owner && owner->name() && ! scope->isEnumScope()) {
const Name *name = owner->name();
const Identifier *id = 0;
if (const NameId *nameId = name->asNameId())
id = nameId->identifier();
else if (const TemplateNameId *nameId = name->asTemplateNameId())
id = nameId->identifier();
if (id)
qualifiedNames.prepend(QString::fromLatin1(id->chars(), id->size()));
}
}
return qualifiedNames.join(QLatin1String("::"));
}
void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos)
{
m_helpId.clear();
m_toolTip.clear();
if (!m_modelManager) if (!m_modelManager)
return; return;
TextEditor::BaseTextEditor *edit = qobject_cast<TextEditor::BaseTextEditor *>(editor->widget()); const Snapshot &snapshot = m_modelManager->snapshot();
if (!edit) Document::Ptr doc = snapshot.document(editor->file()->fileName());
if (!doc)
return; return;
const Snapshot snapshot = m_modelManager->snapshot(); int line = 0;
const QString fileName = editor->file()->fileName(); int column = 0;
Document::Ptr doc = snapshot.document(fileName); editor->convertPosition(pos, &line, &column);
if (!doc)
return; // nothing to do
QString formatTooltip = edit->extraSelectionTooltip(pos); if (!matchDiagnosticMessage(doc, line) &&
QTextCursor tc(edit->document()); !matchIncludeFile(doc, line) &&
!matchMacroInUse(doc, pos)) {
TextEditor::BaseTextEditor *baseEditor = baseTextEditor(editor);
if (!baseEditor)
return;
bool extraSelectionTooltip = false;
if (!baseEditor->extraSelectionTooltip(pos).isEmpty()) {
m_toolTip = baseEditor->extraSelectionTooltip(pos);
extraSelectionTooltip = true;
}
QTextCursor tc(baseEditor->document());
tc.setPosition(pos); tc.setPosition(pos);
moveCursorToEndOfQualifiedName(&tc);
const unsigned lineNumber = tc.block().blockNumber() + 1; // Fetch the expression's code
ExpressionUnderCursor expressionUnderCursor;
// Find the last symbol up to the cursor position const QString &expression = expressionUnderCursor(tc);
int line = 0, column = 0;
editor->convertPosition(tc.position(), &line, &column);
Scope *scope = doc->scopeAt(line, column); Scope *scope = doc->scopeAt(line, column);
TypeOfExpression typeOfExpression; TypeOfExpression typeOfExpression;
typeOfExpression.init(doc, snapshot); typeOfExpression.init(doc, snapshot);
const QList<LookupItem> &lookupItems = typeOfExpression(expression, scope);
if (lookupItems.isEmpty())
return;
// We only want to show F1 if the tooltip matches the help id const LookupItem &lookupItem = lookupItems.first(); // ### TODO: select the best candidate.
bool showF1 = true; handleLookupItemMatch(lookupItem, !extraSelectionTooltip);
}
foreach (const Document::DiagnosticMessage &m, doc->diagnosticMessages()) { evaluateHelpCandidates();
if (m.line() == lineNumber) { }
bool CppHoverHandler::matchDiagnosticMessage(const CPlusPlus::Document::Ptr &document,
const int line)
{
foreach (const Document::DiagnosticMessage &m, document->diagnosticMessages()) {
if (m.line() == line) {
m_toolTip = m.text(); m_toolTip = m.text();
showF1 = false; return true;
break;
} }
} }
return false;
}
QMap<QString, QUrl> helpLinks; bool CppHoverHandler::matchIncludeFile(const CPlusPlus::Document::Ptr &document, const int line)
if (m_toolTip.isEmpty()) { {
foreach (const Document::Include &incl, doc->includes()) { foreach (const Document::Include &includeFile, document->includes()) {
if (incl.line() == lineNumber) { if (includeFile.line() == line) {
m_toolTip = QDir::toNativeSeparators(incl.fileName()); m_toolTip = QDir::toNativeSeparators(includeFile.fileName());
m_helpId = QFileInfo(incl.fileName()).fileName(); const QString &fileName = QFileInfo(includeFile.fileName()).fileName();
helpLinks = Core::HelpManager::instance()->linksForIdentifier(m_helpId); m_helpCandidates.append(HelpCandidate(fileName, fileName, HelpCandidate::Include));
break; return true;
}
} }
} }
return false;
}
if (m_helpId.isEmpty()) { bool CppHoverHandler::matchMacroInUse(const CPlusPlus::Document::Ptr &document, const int pos)
// Move to the end of a qualified name {
bool stop = false; foreach (const Document::MacroUse &use, document->macroUses()) {
while (!stop) { if (use.contains(pos)) {
const QChar ch = editor->characterAt(tc.position()); const Macro& macro = use.macro();
if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) m_toolTip = macro.toString();
tc.setPosition(tc.position() + 1); m_helpCandidates.append(HelpCandidate(macro.name(),
else if (ch == QLatin1Char(':') && editor->characterAt(tc.position() + 1) == QLatin1Char(':')) { macro.name(),
tc.setPosition(tc.position() + 2); HelpCandidate::Macro));
} else { return true;
stop = true;
} }
} }
return false;
}
// Fetch the expression's code void CppHoverHandler::handleLookupItemMatch(const LookupItem &lookupItem, const bool assignTooltip)
ExpressionUnderCursor expressionUnderCursor; {
const QString expression = expressionUnderCursor(tc); Symbol *matchingDeclaration = lookupItem.declaration();
FullySpecifiedType matchingType = lookupItem.type();
const QList<LookupItem> types = typeOfExpression(expression, scope);
if (!types.isEmpty()) {
Overview overview; Overview overview;
overview.setShowArgumentNames(true); overview.setShowArgumentNames(true);
overview.setShowReturnTypes(true); overview.setShowReturnTypes(true);
overview.setShowFullyQualifiedNamed(true); overview.setShowFullyQualifiedNamed(true);
const LookupItem result = types.first(); // ### TODO: select the best candidate. if (!matchingDeclaration && assignTooltip) {
FullySpecifiedType symbolTy = result.type(); // result of `type of expression'. m_toolTip = overview.prettyType(matchingType, QLatin1String(""));
Symbol *declaration = result.declaration(); // lookup symbol
const Name *declarationName = declaration ? declaration->name() : 0;
if (declaration && declaration->scope()
&& declaration->scope()->isClassScope()) {
Class *enclosingClass = declaration->scope()->owner()->asClass();
if (const Identifier *id = enclosingClass->identifier()) {
if (id->isEqualTo(declaration->identifier()))
declaration = enclosingClass;
}
}
m_helpId = buildHelpId(declaration, declarationName);
if (m_toolTip.isEmpty()) {
Symbol *symbol = declaration;
if (declaration)
symbol = declaration;
if (symbol && symbol == declaration && symbol->isClass()) {
m_toolTip = m_helpId;
} else if (declaration && (declaration->isDeclaration() || declaration->isArgument())) {
m_toolTip = overview.prettyType(symbolTy, buildHelpId(declaration, declaration->name()));
} else if (symbolTy->isClassType() || symbolTy->isEnumType() ||
symbolTy->isForwardClassDeclarationType()) {
m_toolTip = m_helpId;
} else { } else {
m_toolTip = overview.prettyType(symbolTy, m_helpId); QString qualifiedName;
HelpCandidate::Category helpCategory;
if (matchingDeclaration->enclosingSymbol()->isClass() ||
matchingDeclaration->enclosingSymbol()->isNamespace() ||
matchingDeclaration->enclosingSymbol()->isEnum()) {
// Fully qualify the name if enclosed by a class, namespace or enum.
QList<const Name *> names = LookupContext::fullyQualifiedName(matchingDeclaration);
if (matchingDeclaration->isNamespace() ||
matchingDeclaration->isClass() ||
matchingDeclaration->isForwardClassDeclaration()) {
// In this case the declaration name appears in the fully qualified name. Remove
// it since it is already considered below.
names.removeLast();
helpCategory = HelpCandidate::ClassOrNamespace;
} else if (matchingDeclaration->isEnum()) {
helpCategory = HelpCandidate::Enum;
} else if (matchingDeclaration->isTypedef()) {
helpCategory = HelpCandidate::Typedef;
} else if (matchingDeclaration->isStatic() && !matchingDeclaration->isFunction()) {
helpCategory = HelpCandidate::Var;
} else {
helpCategory = HelpCandidate::Function;
}
foreach (const Name *name, names) {
qualifiedName.append(overview.prettyName(name));
qualifiedName.append(QLatin1String("::"));
}
}
qualifiedName.append(overview.prettyName(matchingDeclaration->name()));
if (assignTooltip) {
if (matchingDeclaration->isClass() ||
matchingDeclaration->isNamespace() ||
matchingDeclaration->isForwardClassDeclaration() ||
matchingDeclaration->isEnum()) {
m_toolTip = qualifiedName;
} else {
m_toolTip = overview.prettyType(matchingType, qualifiedName);
} }
} }
// Some docs don't contain the namespace in the documentation pages, for instance // Help identifiers are simply the name with no signature, arguments or return type.
// there is QtMobility::QContactManager but the help page is for QContactManager. // They might or might not include a qualification. This is why two candidates are
// To show their help anyway, try stripping scopes until we find something. // created.
const QString startHelpId = m_helpId; overview.setShowArgumentNames(false);
while (!m_helpId.isEmpty()) { overview.setShowReturnTypes(false);
helpLinks = Core::HelpManager::instance()->linksForIdentifier(m_helpId); overview.setShowFunctionSignatures(false);
if (!helpLinks.isEmpty()) overview.setShowFullyQualifiedNamed(false);
break; const QString &simpleName = overview.prettyName(matchingDeclaration->name());
overview.setShowFunctionSignatures(true);
const QString &specifierId = overview.prettyType(matchingType, simpleName);
int coloncolonIndex = m_helpId.indexOf(QLatin1String("::")); m_helpCandidates.append(HelpCandidate(simpleName, specifierId, helpCategory));
if (coloncolonIndex == -1) { m_helpCandidates.append(HelpCandidate(qualifiedName, specifierId, helpCategory));
m_helpId = startHelpId;
break;
}
m_helpId.remove(0, coloncolonIndex + 2);
}
}
}
if (m_toolTip.isEmpty()) {
foreach (const Document::MacroUse &use, doc->macroUses()) {
if (use.contains(pos)) {
const Macro m = use.macro();
m_toolTip = m.toString();
m_helpId = m.name();
break;
}
}
}
if (!formatTooltip.isEmpty())
m_toolTip = formatTooltip;
if (!m_helpId.isEmpty() && !helpLinks.isEmpty()) {
if (showF1) {
// we need the original width without escape sequences
const int width = QFontMetrics(QToolTip::font()).width(m_toolTip);
m_toolTip = QString(QLatin1String("<table><tr><td valign=middle width=%2>%1</td>"
"<td><img src=\":/cppeditor/images/f1.png\"></td></tr></table>"))
.arg(Qt::escape(m_toolTip)).arg(width);
}
editor->setContextHelpId(m_helpId);
} else if (!m_toolTip.isEmpty() && Qt::mightBeRichText(m_toolTip)) {
m_toolTip = QString(QLatin1String("<nobr>%1</nobr>")).arg(Qt::escape(m_toolTip));
} }
} }
void CppHoverHandler::evaluateHelpCandidates()
{
for (int i = 0; i < m_helpCandidates.size(); ++i) {
if (helpIdExists(m_helpCandidates.at(i).m_helpId)) {
m_matchingHelpCandidate = i;
return;
}
}
}
bool CppHoverHandler::helpIdExists(const QString &helpId) const
{
QMap<QString, QUrl> helpLinks = Core::HelpManager::instance()->linksForIdentifier(helpId);
if (!helpLinks.isEmpty())
return true;
return false;
}
QString CppHoverHandler::getDocContents()
{
Q_ASSERT(m_matchingHelpCandidate >= 0);
QString contents;
const HelpCandidate &help = m_helpCandidates.at(m_matchingHelpCandidate);
QMap<QString, QUrl> helpLinks =
Core::HelpManager::instance()->linksForIdentifier(help.m_helpId);
foreach (const QUrl &url, helpLinks) {
// The help id might or might not be qualified. But anchors and marks are not qualified.
const QString &name = removeQualificationIfAny(help.m_helpId);
const QByteArray &html = Core::HelpManager::instance()->fileData(url);
switch (help.m_category) {
case HelpCandidate::Include:
contents = m_htmlDocExtractor.getClassOrNamespaceBrief(html, name);
break;
case HelpCandidate::ClassOrNamespace:
contents = m_htmlDocExtractor.getClassOrNamespaceDescription(html, name);
break;
case HelpCandidate::Function:
contents =
m_htmlDocExtractor.getFunctionDescription(html, help.m_markId, name);
break;
case HelpCandidate::Enum:
contents = m_htmlDocExtractor.getEnumDescription(html, name);
break;
case HelpCandidate::Typedef:
contents = m_htmlDocExtractor.getTypedefDescription(html, name);
break;
case HelpCandidate::Var:
contents = m_htmlDocExtractor.getVarDescription(html, name);
break;
case HelpCandidate::Macro:
contents = m_htmlDocExtractor.getMacroDescription(html, help.m_markId, name);
break;
default:
break;
}
if (!contents.isEmpty())
break;
}
return contents;
}
void CppHoverHandler::resetMatchings()
{
m_matchingHelpCandidate = -1;
m_helpCandidates.clear();
m_toolTip.clear();
}
TextEditor::BaseTextEditor *CppHoverHandler::baseTextEditor(TextEditor::ITextEditor *editor)
{
return qobject_cast<TextEditor::BaseTextEditor *>(editor->widget());
}

View File

@@ -30,12 +30,21 @@
#ifndef CPPHOVERHANDLER_H #ifndef CPPHOVERHANDLER_H
#define CPPHOVERHANDLER_H #define CPPHOVERHANDLER_H
#include <utils/htmldocextractor.h>
#include <QtCore/QObject> #include <QtCore/QObject>
#include <QtCore/QList>
#include <cplusplus/CppDocument.h>
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
class QPoint; class QPoint;
QT_END_NAMESPACE QT_END_NAMESPACE
namespace CPlusPlus {
class LookupItem;
}
namespace Core { namespace Core {
class IEditor; class IEditor;
} }
@@ -46,6 +55,7 @@ class CppModelManagerInterface;
namespace TextEditor { namespace TextEditor {
class ITextEditor; class ITextEditor;
class BaseTextEditor;
} }
namespace CppEditor { namespace CppEditor {
@@ -54,7 +64,6 @@ namespace Internal {
class CppHoverHandler : public QObject class CppHoverHandler : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
CppHoverHandler(QObject *parent = 0); CppHoverHandler(QObject *parent = 0);
@@ -66,11 +75,45 @@ private slots:
void editorOpened(Core::IEditor *editor); void editorOpened(Core::IEditor *editor);
private: private:
void updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos); struct HelpCandidate
{
enum Category {
ClassOrNamespace,
Enum,
Typedef,
Var,
Macro,
Include,
Function
};
HelpCandidate(const QString &helpId, const QString &markId, Category category) :
m_helpId(helpId), m_markId(markId), m_category(category)
{}
QString m_helpId;
QString m_markId;
Category m_category;
};
void resetMatchings();
void identifyMatch(TextEditor::ITextEditor *editor, int pos);
bool matchDiagnosticMessage(const CPlusPlus::Document::Ptr &document, const int line);
bool matchIncludeFile(const CPlusPlus::Document::Ptr &document, const int line);
bool matchMacroInUse(const CPlusPlus::Document::Ptr &document, const int pos);
void handleLookupItemMatch(const CPlusPlus::LookupItem &lookupItem,
const bool assignTooltip);
void evaluateHelpCandidates();
bool helpIdExists(const QString &helpId) const;
QString getDocContents();
static TextEditor::BaseTextEditor *baseTextEditor(TextEditor::ITextEditor *editor);
CppTools::CppModelManagerInterface *m_modelManager; CppTools::CppModelManagerInterface *m_modelManager;
QString m_helpId; int m_matchingHelpCandidate;
QList<HelpCandidate> m_helpCandidates;
QString m_toolTip; QString m_toolTip;
Utils::HtmlDocExtractor m_htmlDocExtractor;
}; };
} // namespace Internal } // namespace Internal