Skip to content
Snippets Groups Projects
Commit 3564c529 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Clean up the C++ hover handler.

parent 678c09c8
No related branches found
No related tags found
No related merge requests found
...@@ -53,6 +53,7 @@ ...@@ -53,6 +53,7 @@
#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 <QtGui/QToolTip> #include <QtGui/QToolTip>
#include <QtGui/QTextCursor> #include <QtGui/QTextCursor>
...@@ -188,29 +189,36 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in ...@@ -188,29 +189,36 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
if (!edit) if (!edit)
return; return;
const Snapshot documents = m_modelManager->snapshot();
const QString fileName = editor->file()->fileName();
Document::Ptr doc = documents.value(fileName);
if (! doc)
return; // nothing to do
QTextCursor tc(edit->document()); QTextCursor tc(edit->document());
tc.setPosition(pos); tc.setPosition(pos);
const unsigned lineNumber = tc.block().blockNumber() + 1;
const Snapshot documents = m_modelManager->snapshot(); // Find the last symbol up to the cursor position
int line = 0, column = 0;
editor->convertPosition(tc.position(), &line, &column);
Symbol *lastSymbol = doc->findSymbolAt(line, column);
const int lineNumber = tc.block().blockNumber() + 1; TypeOfExpression typeOfExpression;
const QString fileName = editor->file()->fileName(); typeOfExpression.setSnapshot(documents);
Document::Ptr doc = documents.value(fileName);
if (doc) { foreach (Document::DiagnosticMessage m, doc->diagnosticMessages()) {
foreach (Document::DiagnosticMessage m, doc->diagnosticMessages()) { if (m.line() == lineNumber) {
if (m.line() == lineNumber) { m_toolTip = m.text();
m_toolTip = m.text(); break;
break;
}
} }
}
if (m_toolTip.isEmpty()) { if (m_toolTip.isEmpty()) {
unsigned lineno = tc.blockNumber() + 1; foreach (const Document::Include &incl, doc->includes()) {
foreach (const Document::Include &incl, doc->includes()) { if (incl.line() == lineNumber) {
if (lineno == incl.line()) { m_toolTip = incl.fileName();
m_toolTip = incl.fileName(); break;
break;
}
} }
} }
} }
...@@ -233,43 +241,34 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in ...@@ -233,43 +241,34 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
ExpressionUnderCursor expressionUnderCursor; ExpressionUnderCursor expressionUnderCursor;
const QString expression = expressionUnderCursor(tc); const QString expression = expressionUnderCursor(tc);
if (doc) { const QList<TypeOfExpression::Result> types =
// Find the last symbol up to the cursor position typeOfExpression(expression, doc, lastSymbol);
int line = 0, column = 0;
editor->convertPosition(tc.position(), &line, &column); if (!types.isEmpty()) {
Symbol *lastSymbol = doc->findSymbolAt(line, column); FullySpecifiedType firstType = types.first().first;
FullySpecifiedType docType = firstType;
TypeOfExpression typeOfExpression;
typeOfExpression.setSnapshot(documents); if (const PointerType *pt = firstType->asPointerType()) {
QList<TypeOfExpression::Result> types = typeOfExpression(expression, doc, lastSymbol); docType = pt->elementType();
} else if (const ReferenceType *rt = firstType->asReferenceType()) {
if (!types.isEmpty()) { docType = rt->elementType();
FullySpecifiedType firstType = types.first().first; }
FullySpecifiedType docType = firstType;
m_helpId = buildHelpId(docType, types.first().second);
if (const PointerType *pt = firstType->asPointerType()) { QString displayName = buildHelpId(firstType, types.first().second);
docType = pt->elementType();
} else if (const ReferenceType *rt = firstType->asReferenceType()) { if (!firstType->isClass() && !firstType->isNamedType()) {
docType = rt->elementType(); Overview overview;
} overview.setShowArgumentNames(true);
overview.setShowReturnTypes(true);
m_toolTip = overview.prettyType(firstType, displayName);
m_helpId = buildHelpId(docType, types.first().second); } else {
QString displayName = buildHelpId(firstType, types.first().second); m_toolTip = m_helpId;
if (!firstType->isClass() && !firstType->isNamedType()) {
Overview overview;
overview.setShowArgumentNames(true);
overview.setShowReturnTypes(true);
m_toolTip = overview.prettyType(firstType, displayName);
} else {
m_toolTip = m_helpId;
}
} }
} }
} }
if (doc && m_toolTip.isEmpty()) { if (m_toolTip.isEmpty()) {
foreach (const Document::MacroUse &use, doc->macroUses()) { foreach (const Document::MacroUse &use, doc->macroUses()) {
if (use.contains(pos)) { if (use.contains(pos)) {
m_toolTip = use.macro().toString(); m_toolTip = use.macro().toString();
...@@ -285,12 +284,15 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in ...@@ -285,12 +284,15 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
m_helpEngineNeedsSetup = false; m_helpEngineNeedsSetup = false;
} }
if (! m_toolTip.isEmpty())
m_toolTip = Qt::escape(m_toolTip);
if (!m_helpId.isEmpty() && !m_helpEngine->linksForIdentifier(m_helpId).isEmpty()) { if (!m_helpId.isEmpty() && !m_helpEngine->linksForIdentifier(m_helpId).isEmpty()) {
m_toolTip = QString(QLatin1String("<table><tr><td valign=middle><nobr>%1</td>" m_toolTip = QString(QLatin1String("<table><tr><td valign=middle><nobr>%1</td>"
"<td><img src=\":/cppeditor/images/f1.svg\"></td></tr></table>")) "<td><img src=\":/cppeditor/images/f1.svg\"></td></tr></table>"))
.arg(Qt::escape(m_toolTip)); .arg(m_toolTip);
editor->setContextHelpId(m_helpId); editor->setContextHelpId(m_helpId);
} else if (!m_toolTip.isEmpty()) { } else if (!m_toolTip.isEmpty()) {
m_toolTip = QString(QLatin1String("<nobr>%1")).arg(Qt::escape(m_toolTip)); m_toolTip = QString(QLatin1String("<nobr>%1")).arg(m_toolTip);
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment