summaryrefslogtreecommitdiff
path: root/src/plugins/cpptools/cppmodelmanager.cpp
diff options
context:
space:
mode:
authorEike Ziller <eike.ziller@qt.io>2019-08-13 15:42:03 +0200
committerEike Ziller <eike.ziller@qt.io>2019-08-15 06:27:29 +0000
commit857b299356c60a48d448a00ec0c9a28d8588f18b (patch)
tree8dfc1bbb0a525718d287d4d2a5cc5e69540536f6 /src/plugins/cpptools/cppmodelmanager.cpp
parent6b5bf41d9fa06ad4a2a1cc20a19808cf94199676 (diff)
downloadqt-creator-857b299356c60a48d448a00ec0c9a28d8588f18b.tar.gz
VcsBase: Make dependency on CppTools optional
The VcsBaseSubmitEditor uses CppModelManager to collect the symbol names from the affected files for completion in the commit message. Move the C++ code model code into CppModelManager, register it in the plugin manager, and call it via QObject means from the submit editor. This avoids a hard dependency from VcsBase to CppTools. Change-Id: I2fb34dbef153c1414820d711e7fc5596bcac1691 Reviewed-by: hjk <hjk@qt.io>
Diffstat (limited to 'src/plugins/cpptools/cppmodelmanager.cpp')
-rw-r--r--src/plugins/cpptools/cppmodelmanager.cpp67
1 files changed, 64 insertions, 3 deletions
diff --git a/src/plugins/cpptools/cppmodelmanager.cpp b/src/plugins/cpptools/cppmodelmanager.cpp
index 7833472559..71e32c33be 100644
--- a/src/plugins/cpptools/cppmodelmanager.cpp
+++ b/src/plugins/cpptools/cppmodelmanager.cpp
@@ -52,15 +52,16 @@
#include "followsymbolinterface.h"
#include <coreplugin/documentmanager.h>
+#include <coreplugin/editormanager/editormanager.h>
#include <coreplugin/icore.h>
-#include <coreplugin/vcsmanager.h>
#include <coreplugin/progressmanager/progressmanager.h>
-#include <coreplugin/editormanager/editormanager.h>
-#include <texteditor/textdocument.h>
+#include <coreplugin/vcsmanager.h>
+#include <extensionsystem/pluginmanager.h>
#include <projectexplorer/project.h>
#include <projectexplorer/projectexplorer.h>
#include <projectexplorer/projectmacro.h>
#include <projectexplorer/session.h>
+#include <texteditor/textdocument.h>
#include <utils/fileutils.h>
#include <utils/hostosinfo.h>
#include <utils/qtcassert.h>
@@ -511,6 +512,10 @@ CppModelManager::CppModelManager()
: CppModelManagerBase(nullptr)
, d(new CppModelManagerPrivate)
{
+ // Used for weak dependency in VcsBaseSubmitEditor
+ setObjectName("CppModelManager");
+ ExtensionSystem::PluginManager::addObject(this);
+
d->m_indexingSupporter = nullptr;
d->m_enableGC = true;
@@ -561,6 +566,8 @@ CppModelManager::CppModelManager()
CppModelManager::~CppModelManager()
{
+ ExtensionSystem::PluginManager::removeObject(this);
+
delete d->m_internalIndexingSupport;
delete d;
}
@@ -1305,6 +1312,60 @@ void CppModelManager::renameIncludes(const QString &oldFileName, const QString &
}
}
+// Return the class name which function belongs to
+static const char *belongingClassName(const Function *function)
+{
+ if (!function)
+ return nullptr;
+
+ if (auto funcName = function->name()) {
+ if (auto qualifiedNameId = funcName->asQualifiedNameId()) {
+ if (const Name *funcBaseName = qualifiedNameId->base()) {
+ if (auto identifier = funcBaseName->identifier())
+ return identifier->chars();
+ }
+ }
+ }
+
+ return nullptr;
+}
+
+QSet<QString> CppModelManager::symbolsInFiles(const QSet<Utils::FilePath> &files) const
+{
+ QSet<QString> uniqueSymbols;
+ const Snapshot cppSnapShot = snapshot();
+
+ // Iterate over the files and get interesting symbols
+ for (const Utils::FilePath &file : files) {
+ // Add symbols from the C++ code model
+ const CPlusPlus::Document::Ptr doc = cppSnapShot.document(file);
+ if (!doc.isNull() && doc->control()) {
+ const CPlusPlus::Control *ctrl = doc->control();
+ CPlusPlus::Symbol **symPtr = ctrl->firstSymbol(); // Read-only
+ while (symPtr != ctrl->lastSymbol()) {
+ const CPlusPlus::Symbol *sym = *symPtr;
+
+ const CPlusPlus::Identifier *symId = sym->identifier();
+ // Add any class, function or namespace identifiers
+ if ((sym->isClass() || sym->isFunction() || sym->isNamespace()) && symId
+ && symId->chars()) {
+ uniqueSymbols.insert(QString::fromUtf8(symId->chars()));
+ }
+
+ // Handle specific case : get "Foo" in "void Foo::function() {}"
+ if (sym->isFunction() && !sym->asFunction()->isDeclaration()) {
+ const char *className = belongingClassName(sym->asFunction());
+ if (className)
+ uniqueSymbols.insert(QString::fromUtf8(className));
+ }
+
+ ++symPtr;
+ }
+ }
+ }
+ return uniqueSymbols;
+}
+
void CppModelManager::onCoreAboutToClose()
{
Core::ProgressManager::cancelTasks(CppTools::Constants::TASK_INDEX);