Commit bcaf2031 authored by Leandro Melo's avatar Leandro Melo
Browse files

C++ editor: Try to find a help item if there is a syntax error

Task-number: QTCREATORBUG-2674
parent 51c47402
......@@ -97,16 +97,11 @@ void CppElementEvaluator::setTextCursor(const QTextCursor &tc)
void CppElementEvaluator::setLookupBaseClasses(const bool lookup)
{ m_lookupBaseClasses = lookup; }
QSharedPointer<CppElement> CppElementEvaluator::identifyCppElement()
{
m_element.clear();
evaluate();
return m_element;
}
// @todo: Consider refactoring code from CPPEditor::findLinkAt into here.
void CppElementEvaluator::evaluate()
void CppElementEvaluator::execute()
{
clear();
if (!m_modelManager)
return;
......@@ -120,37 +115,36 @@ void CppElementEvaluator::evaluate()
const int pos = m_tc.position();
m_editor->convertPosition(pos, &line, &column);
if (!matchDiagnosticMessage(doc, line)) {
if (!matchIncludeFile(doc, line) && !matchMacroInUse(doc, pos)) {
moveCursorToEndOfName(&m_tc);
checkDiagnosticMessage(doc, line);
// Fetch the expression's code
ExpressionUnderCursor expressionUnderCursor;
const QString &expression = expressionUnderCursor(m_tc);
Scope *scope = doc->scopeAt(line, column);
if (!matchIncludeFile(doc, line) && !matchMacroInUse(doc, pos)) {
moveCursorToEndOfName(&m_tc);
TypeOfExpression typeOfExpression;
typeOfExpression.init(doc, snapshot);
const QList<LookupItem> &lookupItems = typeOfExpression(expression, scope);
if (lookupItems.isEmpty())
return;
// Fetch the expression's code
ExpressionUnderCursor expressionUnderCursor;
const QString &expression = expressionUnderCursor(m_tc);
Scope *scope = doc->scopeAt(line, column);
const LookupItem &lookupItem = lookupItems.first(); // ### TODO: select best candidate.
handleLookupItemMatch(snapshot, lookupItem, typeOfExpression.context());
}
TypeOfExpression typeOfExpression;
typeOfExpression.init(doc, snapshot);
const QList<LookupItem> &lookupItems = typeOfExpression(expression, scope);
if (lookupItems.isEmpty())
return;
const LookupItem &lookupItem = lookupItems.first(); // ### TODO: select best candidate.
handleLookupItemMatch(snapshot, lookupItem, typeOfExpression.context());
}
}
bool CppElementEvaluator::matchDiagnosticMessage(const CPlusPlus::Document::Ptr &document,
void CppElementEvaluator::checkDiagnosticMessage(const CPlusPlus::Document::Ptr &document,
unsigned line)
{
foreach (const Document::DiagnosticMessage &m, document->diagnosticMessages()) {
if (m.line() == line) {
m_element = QSharedPointer<CppElement>(new CppDiagnosis(m));
return true;
m_diagnosis = m.text();
break;
}
}
return false;
}
bool CppElementEvaluator::matchIncludeFile(const CPlusPlus::Document::Ptr &document, unsigned line)
......@@ -216,6 +210,32 @@ void CppElementEvaluator::handleLookupItemMatch(const Snapshot &snapshot,
}
}
bool CppElementEvaluator::identifiedCppElement() const
{
return !m_element.isNull();
}
const QSharedPointer<CppElement> &CppElementEvaluator::cppElement() const
{
return m_element;
}
bool CppElementEvaluator::hasDiagnosis() const
{
return !m_diagnosis.isEmpty();
}
const QString &CppElementEvaluator::diagnosis() const
{
return m_diagnosis;
}
void CppEditor::Internal::CppElementEvaluator::clear()
{
m_element.clear();
m_diagnosis.clear();
}
// CppElement
CppElement::CppElement() : m_helpCategory(TextEditor::HelpItem::Unknown)
{}
......@@ -269,19 +289,6 @@ Unknown::~Unknown()
const QString &Unknown::type() const
{ return m_type; }
// CppDiagnosis
CppDiagnosis::CppDiagnosis(const Document::DiagnosticMessage &message) :
CppElement(), m_text(message.text())
{
setTooltip(m_text);
}
CppDiagnosis::~CppDiagnosis()
{}
const QString &CppDiagnosis::text() const
{ return m_text; }
// CppInclude
CppInclude::~CppInclude()
{}
......
......@@ -70,11 +70,15 @@ public:
void setTextCursor(const QTextCursor &tc);
void setLookupBaseClasses(const bool lookup);
QSharedPointer<CppElement> identifyCppElement();
void execute();
bool identifiedCppElement() const;
const QSharedPointer<CppElement> &cppElement() const;
bool hasDiagnosis() const;
const QString &diagnosis() const;
private:
void evaluate();
bool matchDiagnosticMessage(const CPlusPlus::Document::Ptr &document, unsigned line);
void clear();
void checkDiagnosticMessage(const CPlusPlus::Document::Ptr &document, unsigned line);
bool matchIncludeFile(const CPlusPlus::Document::Ptr &document, unsigned line);
bool matchMacroInUse(const CPlusPlus::Document::Ptr &document, unsigned pos);
void handleLookupItemMatch(const CPlusPlus::Snapshot &snapshot,
......@@ -86,6 +90,7 @@ private:
QTextCursor m_tc;
bool m_lookupBaseClasses;
QSharedPointer<CppElement> m_element;
QString m_diagnosis;
};
class CppElement
......@@ -129,18 +134,6 @@ private:
QString m_type;
};
class CppDiagnosis : public CppElement
{
public:
CppDiagnosis(const CPlusPlus::Document::DiagnosticMessage &message);
virtual ~CppDiagnosis();
const QString &text() const;
private:
QString m_text;
};
class CppInclude : public CppElement
{
public:
......
......@@ -78,9 +78,15 @@ void CppHoverHandler::identifyMatch(TextEditor::ITextEditor *editor, int pos)
CppElementEvaluator evaluator(cppEditor);
evaluator.setTextCursor(tc);
QSharedPointer<CppElement> cppElement = evaluator.identifyCppElement();
if (!cppElement.isNull()) {
setToolTip(cppElement->tooltip());
evaluator.execute();
if (evaluator.hasDiagnosis()) {
setToolTip(evaluator.diagnosis());
setIsDiagnosticTooltip(true);
}
if (evaluator.identifiedCppElement()) {
const QSharedPointer<CppElement> &cppElement = evaluator.cppElement();
if (!isDiagnosticTooltip())
setToolTip(cppElement->tooltip());
foreach (const QString &helpId, cppElement->helpIdCandidates()) {
if (!Core::HelpManager::instance()->linksForIdentifier(helpId).isEmpty()) {
setLastHelpItemIdentified(TextEditor::HelpItem(helpId,
......@@ -98,6 +104,9 @@ void CppHoverHandler::decorateToolTip()
if (Qt::mightBeRichText(toolTip()))
setToolTip(Qt::escape(toolTip()));
if (isDiagnosticTooltip())
return;
const TextEditor::HelpItem &help = lastHelpItemIdentified();
if (help.isValid()) {
// If Qt is built with a namespace, we still show the tip without it, as
......
......@@ -118,8 +118,9 @@ void CppTypeHierarchyWidget::perform()
CppElementEvaluator evaluator(m_cppEditor);
evaluator.setLookupBaseClasses(true);
QSharedPointer<CppElement> cppElement = evaluator.identifyCppElement();
if (!cppElement.isNull()) {
evaluator.execute();
if (evaluator.identifiedCppElement()) {
const QSharedPointer<CppElement> &cppElement = evaluator.cppElement();
CppElement *element = cppElement.data();
if (CppClass *cppClass = dynamic_cast<CppClass *>(element))
buildModel(*cppClass, m_model->invisibleRootItem());
......
......@@ -49,7 +49,7 @@
using namespace TextEditor;
using namespace Core;
BaseHoverHandler::BaseHoverHandler(QObject *parent) : QObject(parent)
BaseHoverHandler::BaseHoverHandler(QObject *parent) : QObject(parent), m_diagnosticTooltip(false)
{
// Listen for editor opened events in order to connect to tooltip/helpid requests
connect(ICore::instance()->editorManager(), SIGNAL(editorOpened(Core::IEditor *)),
......@@ -127,6 +127,16 @@ void BaseHoverHandler::addF1ToToolTip()
"</tr></table>")).arg(m_toolTip);
}
void BaseHoverHandler::setIsDiagnosticTooltip(bool isDiagnosticTooltip)
{
m_diagnosticTooltip = isDiagnosticTooltip;
}
bool BaseHoverHandler::isDiagnosticTooltip() const
{
return m_diagnosticTooltip;
}
void BaseHoverHandler::setLastHelpItemIdentified(const HelpItem &help)
{ m_lastHelpItemIdentified = help; }
......@@ -135,6 +145,7 @@ const HelpItem &BaseHoverHandler::lastHelpItemIdentified() const
void BaseHoverHandler::clear()
{
m_diagnosticTooltip = false;
m_toolTip.clear();
m_lastHelpItemIdentified = HelpItem();
}
......@@ -151,7 +162,7 @@ void BaseHoverHandler::decorateToolTip()
if (Qt::mightBeRichText(toolTip()))
setToolTip(Qt::escape(toolTip()));
if (lastHelpItemIdentified().isValid()) {
if (!isDiagnosticTooltip() && lastHelpItemIdentified().isValid()) {
const QString &contents = lastHelpItemIdentified().extractContent(false);
if (!contents.isEmpty()) {
setToolTip(Qt::escape(toolTip()));
......
......@@ -74,6 +74,9 @@ protected:
void addF1ToToolTip();
void setIsDiagnosticTooltip(bool isDiagnosticTooltip);
bool isDiagnosticTooltip() const;
void setLastHelpItemIdentified(const HelpItem &help);
const HelpItem &lastHelpItemIdentified() const;
......@@ -88,6 +91,7 @@ private:
virtual void decorateToolTip();
virtual void operateTooltip(ITextEditor *editor, const QPoint &point);
bool m_diagnosticTooltip;
QString m_toolTip;
HelpItem m_lastHelpItemIdentified;
};
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment