Clean up the C++ hover handler.

This commit is contained in:
Roberto Raggi
2008-12-18 10:52:29 +01:00
parent 678c09c813
commit 3564c529d0

View File

@@ -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,15 +189,24 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
if (!edit) if (!edit)
return; return;
QTextCursor tc(edit->document());
tc.setPosition(pos);
const Snapshot documents = m_modelManager->snapshot(); const Snapshot documents = m_modelManager->snapshot();
const int lineNumber = tc.block().blockNumber() + 1;
const QString fileName = editor->file()->fileName(); const QString fileName = editor->file()->fileName();
Document::Ptr doc = documents.value(fileName); Document::Ptr doc = documents.value(fileName);
if (doc) { if (! doc)
return; // nothing to do
QTextCursor tc(edit->document());
tc.setPosition(pos);
const unsigned lineNumber = tc.block().blockNumber() + 1;
// 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);
TypeOfExpression typeOfExpression;
typeOfExpression.setSnapshot(documents);
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();
@@ -205,15 +215,13 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
} }
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 (lineno == incl.line()) { if (incl.line() == lineNumber) {
m_toolTip = incl.fileName(); m_toolTip = incl.fileName();
break; break;
} }
} }
} }
}
if (m_toolTip.isEmpty()) { if (m_toolTip.isEmpty()) {
// Move to the end of a qualified name // Move to the end of a qualified name
@@ -233,15 +241,8 @@ 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);
Symbol *lastSymbol = doc->findSymbolAt(line, column);
TypeOfExpression typeOfExpression;
typeOfExpression.setSnapshot(documents);
QList<TypeOfExpression::Result> types = typeOfExpression(expression, doc, lastSymbol);
if (!types.isEmpty()) { if (!types.isEmpty()) {
FullySpecifiedType firstType = types.first().first; FullySpecifiedType firstType = types.first().first;
@@ -253,7 +254,6 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
docType = rt->elementType(); docType = rt->elementType();
} }
m_helpId = buildHelpId(docType, types.first().second); m_helpId = buildHelpId(docType, types.first().second);
QString displayName = buildHelpId(firstType, types.first().second); QString displayName = buildHelpId(firstType, types.first().second);
@@ -267,9 +267,8 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
} }
} }
} }
}
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
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);
} }
} }