From a658ee55f2a5f0fae0d1350583fb29fedea93b52 Mon Sep 17 00:00:00 2001
From: Roberto Raggi <roberto.raggi@nokia.com>
Date: Wed, 27 Jan 2010 14:56:22 +0100
Subject: [PATCH] Ask the HelpEngine for documentation about builtin QML
 components.

Done with: Erik
---
 src/libs/qmljs/qmljsbind.cpp                |  7 ++-
 src/plugins/qmljseditor/qmlhoverhandler.cpp | 70 +++++++++++----------
 src/plugins/qmljseditor/qmlhoverhandler.h   |  4 +-
 3 files changed, 46 insertions(+), 35 deletions(-)

diff --git a/src/libs/qmljs/qmljsbind.cpp b/src/libs/qmljs/qmljsbind.cpp
index 80a4d43bc97..9d5b523bdf0 100644
--- a/src/libs/qmljs/qmljsbind.cpp
+++ b/src/libs/qmljs/qmljsbind.cpp
@@ -193,7 +193,12 @@ bool Bind::visit(UiImport *ast)
                             }
                         }
 
-                        namespaceObject->setProperty(userComponent->componentName(), objectValue);
+                        const QString componentName = userComponent->componentName();
+
+                        if (! componentName.isEmpty()) {
+                            objectValue->setClassName(componentName);
+                            namespaceObject->setProperty(componentName, objectValue);
+                        }
                     }
                 }
             }
diff --git a/src/plugins/qmljseditor/qmlhoverhandler.cpp b/src/plugins/qmljseditor/qmlhoverhandler.cpp
index 9aace5d309d..eb874a04399 100644
--- a/src/plugins/qmljseditor/qmlhoverhandler.cpp
+++ b/src/plugins/qmljseditor/qmlhoverhandler.cpp
@@ -146,6 +146,11 @@ void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
     if (qmlDocument.isNull())
         return;
 
+    if (m_helpEngineNeedsSetup && m_helpEngine->registeredDocumentations().count() > 0) {
+        m_helpEngine->setupData();
+        m_helpEngineNeedsSetup = false;
+    }
+
     QTextCursor tc(edit->document());
     tc.setPosition(pos);
 
@@ -165,7 +170,7 @@ void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
         bool stop = false;
         while (!stop) {
             const QChar ch = editor->characterAt(tc.position());
-            if (ch.isLetterOrNumber() || ch == QLatin1Char('_') || ch == QLatin1Char('.')) {
+            if (ch.isLetterOrNumber() || ch == QLatin1Char('_')) {
                 tc.setPosition(tc.position() + 1);
             } else {
                 stop = true;
@@ -189,61 +194,60 @@ void QmlHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
         Interpreter::ObjectValue *scope = bind(declaringMember);
         Check check(&interp);
         const Interpreter::Value *value = check(expression, scope);
-        m_toolTip = prettyPrint(value, &interp);
-
-#if 0
-        QmlLookupContext context(expressionUnderCursor.expressionScopes(), doc, m_modelManager->snapshot(), typeSystem);
-        QmlResolveExpression resolver(context);
-        Symbol *resolvedSymbol = resolver.typeOf(expressionUnderCursor.expressionNode());
-
-        if (resolvedSymbol) {
-            symbolName = resolvedSymbol->name();
-
-            if (resolvedSymbol->isBuildInSymbol())
-                m_helpId = buildHelpId(resolvedSymbol);
-            else if (SymbolFromFile *symbolFromFile = resolvedSymbol->asSymbolFromFile())
-                m_toolTip = symbolFromFile->fileName();
+        QStringList baseClasses;
+        m_toolTip = prettyPrint(value, &interp, &baseClasses);
+        foreach (const QString &baseClass, baseClasses) {
+            QString helpId = QLatin1String("QML.");
+            helpId += baseClass;
+
+            if (! m_helpEngine->linksForIdentifier(helpId).isEmpty()) {
+                m_helpId = helpId;
+                break;
+            }
         }
-#endif
-    }
-
-    if (m_helpEngineNeedsSetup && m_helpEngine->registeredDocumentations().count() > 0) {
-        m_helpEngine->setupData();
-        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()) {
         if (showF1) {
-            m_toolTip = QString(QLatin1String("<table><tr><td valign=middle><nobr>%1</td>"
-                                              "<td><img src=\":/cppeditor/images/f1.svg\"></td></tr></table>"))
+            m_toolTip = QString::fromUtf8("<table><tr><td valign=middle><nobr>%1</td>"
+                                            "<td><img src=\":/cppeditor/images/f1.svg\"></td></tr></table>")
                     .arg(m_toolTip);
         }
         editor->setContextHelpId(m_helpId);
     } else if (!m_toolTip.isEmpty()) {
-        m_toolTip = QString(QLatin1String("<nobr>%1")).arg(m_toolTip);
+        m_toolTip = QString::fromUtf8("<nobr>%1").arg(m_toolTip);
     } else if (!m_helpId.isEmpty()) {
-        m_toolTip = QString(QLatin1String("<nobr>No help available for \"%1\"")).arg(symbolName);
+        m_toolTip = QString::fromUtf8("<nobr>No help available for \"%1\"").arg(symbolName);
     }
 }
 
-QString QmlHoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Engine *interp) const
+QString QmlHoverHandler::prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Engine *interp,
+                                     QStringList *baseClasses) const
 {
     if (!value)
         return QString();
 
     if (const Interpreter::ObjectValue *objectValue = value->asObjectValue()) {
-        QString className = objectValue->className();
+        do {
+            const QString className = objectValue->className();
+
+            if (! className.isEmpty())
+                baseClasses->append(className);
 
-        while (objectValue && objectValue->prototype() && className.isEmpty()) {
             objectValue = objectValue->prototype();
-            className = objectValue->className();
-        }
+        } while (objectValue);
 
-        return className;
+        if (! baseClasses->isEmpty())
+            return baseClasses->first();
     }
 
-    return interp->typeId(value);
+    QString typeId = interp->typeId(value);
+
+    if (typeId.isEmpty() || typeId == QLatin1String("undefined"))
+        typeId = QLatin1String("Unknown");
+
+    return typeId;
 }
diff --git a/src/plugins/qmljseditor/qmlhoverhandler.h b/src/plugins/qmljseditor/qmlhoverhandler.h
index abfabe6071f..0f481e444bc 100644
--- a/src/plugins/qmljseditor/qmlhoverhandler.h
+++ b/src/plugins/qmljseditor/qmlhoverhandler.h
@@ -37,6 +37,7 @@
 QT_BEGIN_NAMESPACE
 class QHelpEngineCore;
 class QPoint;
+class QStringList;
 QT_END_NAMESPACE
 
 namespace Core {
@@ -73,7 +74,8 @@ private slots:
 
 private:
     void updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, int pos);
-    QString prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Engine *interp) const;
+    QString prettyPrint(const QmlJS::Interpreter::Value *value, QmlJS::Interpreter::Engine *interp,
+                        QStringList *baseClasses) const;
 
 private:
     QmlModelManagerInterface *m_modelManager;
-- 
GitLab