From 0df1ee1abd30703722c83a5c43c4fff4a14ab779 Mon Sep 17 00:00:00 2001
From: hjk <qthjk@ovi.com>
Date: Wed, 10 Oct 2012 21:00:48 +0200
Subject: [PATCH] CppTools: make correspondingHeaderOrSource freely accessible

Change-Id: Ic8aba607157381359984ef1c99b2f32161daaecd
Reviewed-by: hjk <qthjk@ovi.com>
---
 src/plugins/cpptools/cpptoolsplugin.cpp       | 36 +++++++++++--------
 src/plugins/cpptools/cpptoolsplugin.h         |  6 ++--
 src/plugins/cpptools/cpptoolsreuse.h          |  2 ++
 .../cpptools/insertionpointlocator.cpp        |  4 +--
 4 files changed, 27 insertions(+), 21 deletions(-)

diff --git a/src/plugins/cpptools/cpptoolsplugin.cpp b/src/plugins/cpptools/cpptoolsplugin.cpp
index e368f6c2bb1..d1742ea51ee 100644
--- a/src/plugins/cpptools/cpptoolsplugin.cpp
+++ b/src/plugins/cpptools/cpptoolsplugin.cpp
@@ -39,6 +39,7 @@
 #include "cpplocatorfilter.h"
 #include "symbolsfindfilter.h"
 #include "cpptoolssettings.h"
+#include "cpptoolsreuse.h"
 
 #include <extensionsystem/pluginmanager.h>
 
@@ -74,12 +75,15 @@
 
 #include <sstream>
 
-using namespace CppTools::Internal;
 using namespace CPlusPlus;
 
+namespace CppTools {
+namespace Internal {
+
 enum { debug = 0 };
 
 static CppToolsPlugin *m_instance = 0;
+static QHash<QString, QString> m_headerSourceMapping;
 
 CppToolsPlugin::CppToolsPlugin() :
     m_modelManager(0),
@@ -250,16 +254,23 @@ static int commonStringLength(const QString &s1, const QString &s2)
     return length;
 }
 
-QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) const
+} // namespace Internal
+
+QString correspondingHeaderOrSource(const QString &fileName, bool *wasHeader)
 {
-    const QFileInfo fi(fileName);
-    if (m_headerSourceMapping.contains(fi.absoluteFilePath()))
-        return m_headerSourceMapping.value(fi.absoluteFilePath());
+    using namespace Internal;
 
     const Core::MimeDatabase *mimeDatase = Core::ICore::mimeDatabase();
-    ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::currentProject();
+    const QFileInfo fi(fileName);
+    if (m_headerSourceMapping.contains(fi.absoluteFilePath())) {
+        if (wasHeader)
+            *wasHeader = fileType(mimeDatase, fi) == HeaderFile;
+        return m_headerSourceMapping.value(fi.absoluteFilePath());
+    }
 
-    const FileType type = fileType(mimeDatase, fi);
+    FileType type = fileType(mimeDatase, fi);
+    if (wasHeader)
+        *wasHeader = type == HeaderFile;
 
     if (debug)
         qDebug() << Q_FUNC_INFO << fileName <<  type;
@@ -298,6 +309,7 @@ QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) co
     }
 
     // Find files in the project
+    ProjectExplorer::Project *project = ProjectExplorer::ProjectExplorerPlugin::currentProject();
     if (project) {
         QString bestFileName;
         int compareValue = 0;
@@ -324,12 +336,6 @@ QString CppToolsPlugin::correspondingHeaderOrSourceI(const QString &fileName) co
     return QString();
 }
 
-QString CppToolsPlugin::correspondingHeaderOrSource(const QString &fileName)
-{
-    const QString rc = m_instance->correspondingHeaderOrSourceI(fileName);
-    if (debug)
-        qDebug() << Q_FUNC_INFO << fileName << rc;
-    return rc;
-}
+} // namespace CppTools
 
-Q_EXPORT_PLUGIN(CppToolsPlugin)
+Q_EXPORT_PLUGIN(CppTools::Internal::CppToolsPlugin)
diff --git a/src/plugins/cpptools/cpptoolsplugin.h b/src/plugins/cpptools/cpptoolsplugin.h
index 6bc1ddef44a..38d51bccf3e 100644
--- a/src/plugins/cpptools/cpptoolsplugin.h
+++ b/src/plugins/cpptools/cpptoolsplugin.h
@@ -30,6 +30,8 @@
 #ifndef CPPTOOLS_H
 #define CPPTOOLS_H
 
+#include "cpptools_global.h"
+
 #include <extensionsystem/iplugin.h>
 #include <projectexplorer/projectexplorer.h>
 #include <find/ifindfilter.h>
@@ -70,7 +72,6 @@ public:
     void extensionsInitialized();
     ShutdownFlag aboutToShutdown();
     CppModelManager *cppModelManager() { return m_modelManager; }
-    static QString correspondingHeaderOrSource(const QString &fileName);
 
 private slots:
     void switchHeaderSource();
@@ -109,12 +110,9 @@ private:
 #endif
 
 private:
-    QString correspondingHeaderOrSourceI(const QString &fileName) const;
-
     CppModelManager *m_modelManager;
     QSharedPointer<CppFileSettings> m_fileSettings;
     CppToolsSettings *m_settings;
-    mutable QHash<QString, QString> m_headerSourceMapping;
 };
 
 } // namespace Internal
diff --git a/src/plugins/cpptools/cpptoolsreuse.h b/src/plugins/cpptools/cpptoolsreuse.h
index d9bc4fc7520..6a4b6647d82 100644
--- a/src/plugins/cpptools/cpptoolsreuse.h
+++ b/src/plugins/cpptools/cpptoolsreuse.h
@@ -52,6 +52,8 @@ bool CPPTOOLS_EXPORT isValidIdentifier(const QString &s);
 
 bool CPPTOOLS_EXPORT isQtKeyword(const QStringRef &text);
 
+QString CPPTOOLS_EXPORT correspondingHeaderOrSource(const QString &fileName, bool *wasHeader = 0);
+
 } // CppTools
 
 #endif // CPPTOOLSREUSE_H
diff --git a/src/plugins/cpptools/insertionpointlocator.cpp b/src/plugins/cpptools/insertionpointlocator.cpp
index e70fefd4eea..4bb5b2a4e0b 100644
--- a/src/plugins/cpptools/insertionpointlocator.cpp
+++ b/src/plugins/cpptools/insertionpointlocator.cpp
@@ -27,7 +27,7 @@
 **
 ****************************************************************************/
 
-#include "cpptoolsplugin.h"
+#include "cpptoolsreuse.h"
 #include "cpprefactoringchanges.h"
 #include "insertionpointlocator.h"
 #include "symbolfinder.h"
@@ -578,7 +578,7 @@ QList<InsertionLocation> InsertionPointLocator::methodDefinition(
                                                    declaration->fileNameLength());
     QString target = declFileName;
     if (!isSourceFile(declFileName)) {
-        QString candidate = Internal::CppToolsPlugin::correspondingHeaderOrSource(declFileName);
+        QString candidate = CppTools::correspondingHeaderOrSource(declFileName);
         if (!candidate.isEmpty())
             target = candidate;
     }
-- 
GitLab