forked from qt-creator/qt-creator
ProjectExplorer: Use a local object pool for IProjectManagers
Change-Id: I94ada96da2bf7ce4c95c0ae8f393e6303e79ff60 Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -29,6 +29,8 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
|
namespace Utils { class MimeType; }
|
||||||
|
|
||||||
namespace ProjectExplorer {
|
namespace ProjectExplorer {
|
||||||
|
|
||||||
class Project;
|
class Project;
|
||||||
@@ -38,6 +40,10 @@ class PROJECTEXPLORER_EXPORT IProjectManager : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
IProjectManager();
|
||||||
|
// Finds a IProjectManager matching the passed MimeType.
|
||||||
|
static IProjectManager *managerForMimeType(const Utils::MimeType &mt);
|
||||||
|
|
||||||
virtual QString mimeType() const = 0;
|
virtual QString mimeType() const = 0;
|
||||||
// FileName is a canonical path of a checked-to-exist file.
|
// FileName is a canonical path of a checked-to-exist file.
|
||||||
virtual Project *openProject(const QString &fileName) = 0;
|
virtual Project *openProject(const QString &fileName) = 0;
|
||||||
|
|||||||
@@ -33,8 +33,6 @@
|
|||||||
|
|
||||||
#include <coreplugin/featureprovider.h>
|
#include <coreplugin/featureprovider.h>
|
||||||
|
|
||||||
#include <extensionsystem/pluginmanager.h>
|
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/macroexpander.h>
|
#include <utils/macroexpander.h>
|
||||||
#include <utils/mimetypes/mimedatabase.h>
|
#include <utils/mimetypes/mimedatabase.h>
|
||||||
@@ -105,18 +103,12 @@ void JsonKitsPage::setPreferredFeatures(const QVariant &data)
|
|||||||
void JsonKitsPage::setupProjectFiles(const JsonWizard::GeneratorFiles &files)
|
void JsonKitsPage::setupProjectFiles(const JsonWizard::GeneratorFiles &files)
|
||||||
{
|
{
|
||||||
Project *project = nullptr;
|
Project *project = nullptr;
|
||||||
QList<IProjectManager *> managerList = ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
|
||||||
|
|
||||||
foreach (const JsonWizard::GeneratorFile &f, files) {
|
for (const JsonWizard::GeneratorFile &f : files) {
|
||||||
if (f.file.attributes() & GeneratedFile::OpenProjectAttribute) {
|
if (f.file.attributes() & GeneratedFile::OpenProjectAttribute) {
|
||||||
const QFileInfo fi(f.file.path());
|
const QFileInfo fi(f.file.path());
|
||||||
const QString path = fi.absoluteFilePath();
|
const QString path = fi.absoluteFilePath();
|
||||||
|
IProjectManager *manager = IProjectManager::managerForMimeType(Utils::mimeTypeForFile(fi));
|
||||||
Utils::MimeType mt = Utils::mimeTypeForFile(fi);
|
|
||||||
if (!mt.isValid())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
auto manager = Utils::findOrDefault(managerList, Utils::equal(&IProjectManager::mimeType, mt.name()));
|
|
||||||
project = manager ? manager->openProject(path) : nullptr;
|
project = manager ? manager->openProject(path) : nullptr;
|
||||||
if (project) {
|
if (project) {
|
||||||
if (setupProject(project))
|
if (setupProject(project))
|
||||||
|
|||||||
@@ -32,8 +32,6 @@
|
|||||||
|
|
||||||
#include <coreplugin/editormanager/editormanager.h>
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
|
|
||||||
#include <extensionsystem/pluginmanager.h>
|
|
||||||
|
|
||||||
#include <utils/algorithm.h>
|
#include <utils/algorithm.h>
|
||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
@@ -103,23 +101,14 @@ Core::GeneratedFiles JsonWizardScannerGenerator::fileList(Utils::MacroExpander *
|
|||||||
|
|
||||||
result = scan(project.absolutePath(), project);
|
result = scan(project.absolutePath(), project);
|
||||||
|
|
||||||
QList<IProjectManager *> projectManagers
|
|
||||||
= ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
|
||||||
|
|
||||||
int projectCount = 0;
|
int projectCount = 0;
|
||||||
for (auto it = result.begin(); it != result.end(); ++it) {
|
for (auto it = result.begin(); it != result.end(); ++it) {
|
||||||
const QString relPath = project.relativeFilePath(it->path());
|
const QString relPath = project.relativeFilePath(it->path());
|
||||||
it->setBinary(binaryPattern.match(relPath).hasMatch());
|
it->setBinary(binaryPattern.match(relPath).hasMatch());
|
||||||
|
bool found = IProjectManager::managerForMimeType(Utils::mimeTypeForFile(relPath));
|
||||||
Utils::MimeType mt = Utils::mimeTypeForFile(relPath);
|
|
||||||
if (mt.isValid()) {
|
|
||||||
bool found = Utils::anyOf(projectManagers, [mt](IProjectManager *m) {
|
|
||||||
return mt.matchesName(m->mimeType());
|
|
||||||
});
|
|
||||||
if (found && !(onlyFirst && projectCount++))
|
if (found && !(onlyFirst && projectCount++))
|
||||||
it->setAttributes(it->attributes() | Core::GeneratedFile::OpenProjectAttribute);
|
it->setAttributes(it->attributes() | Core::GeneratedFile::OpenProjectAttribute);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -122,6 +122,7 @@
|
|||||||
#include <utils/fileutils.h>
|
#include <utils/fileutils.h>
|
||||||
#include <utils/macroexpander.h>
|
#include <utils/macroexpander.h>
|
||||||
#include <utils/mimetypes/mimedatabase.h>
|
#include <utils/mimetypes/mimedatabase.h>
|
||||||
|
#include <utils/objectpool.h>
|
||||||
#include <utils/parameteraction.h>
|
#include <utils/parameteraction.h>
|
||||||
#include <utils/processhandle.h>
|
#include <utils/processhandle.h>
|
||||||
#include <utils/qtcassert.h>
|
#include <utils/qtcassert.h>
|
||||||
@@ -340,6 +341,7 @@ public:
|
|||||||
QStringList m_profileMimeTypes;
|
QStringList m_profileMimeTypes;
|
||||||
AppOutputPane *m_outputPane = nullptr;
|
AppOutputPane *m_outputPane = nullptr;
|
||||||
|
|
||||||
|
Utils::ObjectPool<IProjectManager> m_projectManagers;
|
||||||
QList<QPair<QString, QString> > m_recentProjects; // pair of filename, displayname
|
QList<QPair<QString, QString> > m_recentProjects; // pair of filename, displayname
|
||||||
static const int m_maxRecentProjects = 25;
|
static const int m_maxRecentProjects = 25;
|
||||||
|
|
||||||
@@ -1453,9 +1455,6 @@ void ProjectExplorerPluginPrivate::closeAllProjects()
|
|||||||
void ProjectExplorerPlugin::extensionsInitialized()
|
void ProjectExplorerPlugin::extensionsInitialized()
|
||||||
{
|
{
|
||||||
// Register factories for all project managers
|
// Register factories for all project managers
|
||||||
QList<IProjectManager*> projectManagers =
|
|
||||||
ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
|
||||||
|
|
||||||
QStringList allGlobPatterns;
|
QStringList allGlobPatterns;
|
||||||
|
|
||||||
const QString filterSeparator = QLatin1String(";;");
|
const QString filterSeparator = QLatin1String(";;");
|
||||||
@@ -1474,7 +1473,7 @@ void ProjectExplorerPlugin::extensionsInitialized()
|
|||||||
});
|
});
|
||||||
|
|
||||||
factory->addMimeType(QStringLiteral("inode/directory"));
|
factory->addMimeType(QStringLiteral("inode/directory"));
|
||||||
foreach (IProjectManager *manager, projectManagers) {
|
dd->m_projectManagers.forEachObject([&](IProjectManager *manager) {
|
||||||
const QString mimeType = manager->mimeType();
|
const QString mimeType = manager->mimeType();
|
||||||
factory->addMimeType(mimeType);
|
factory->addMimeType(mimeType);
|
||||||
Utils::MimeType mime = Utils::mimeTypeForName(mimeType);
|
Utils::MimeType mime = Utils::mimeTypeForName(mimeType);
|
||||||
@@ -1482,7 +1481,7 @@ void ProjectExplorerPlugin::extensionsInitialized()
|
|||||||
filterStrings.append(mime.filterString());
|
filterStrings.append(mime.filterString());
|
||||||
|
|
||||||
dd->m_profileMimeTypes += mimeType;
|
dd->m_profileMimeTypes += mimeType;
|
||||||
}
|
});
|
||||||
|
|
||||||
addAutoReleasedObject(factory);
|
addAutoReleasedObject(factory);
|
||||||
|
|
||||||
@@ -1660,11 +1659,6 @@ void ProjectExplorerPlugin::showOpenProjectError(const OpenProjectResult &result
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static QList<IProjectManager*> allProjectManagers()
|
|
||||||
{
|
|
||||||
return ExtensionSystem::PluginManager::getObjects<IProjectManager>();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void appendError(QString &errorString, const QString &error)
|
static void appendError(QString &errorString, const QString &error)
|
||||||
{
|
{
|
||||||
if (error.isEmpty())
|
if (error.isEmpty())
|
||||||
@@ -1677,8 +1671,6 @@ static void appendError(QString &errorString, const QString &error)
|
|||||||
|
|
||||||
ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProjects(const QStringList &fileNames)
|
ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProjects(const QStringList &fileNames)
|
||||||
{
|
{
|
||||||
const QList<IProjectManager*> projectManagers = allProjectManagers();
|
|
||||||
|
|
||||||
QList<Project*> openedPro;
|
QList<Project*> openedPro;
|
||||||
QList<Project *> alreadyOpen;
|
QList<Project *> alreadyOpen;
|
||||||
QString errorString;
|
QString errorString;
|
||||||
@@ -1702,10 +1694,7 @@ ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProjects(con
|
|||||||
|
|
||||||
Utils::MimeType mt = Utils::mimeTypeForFile(fileName);
|
Utils::MimeType mt = Utils::mimeTypeForFile(fileName);
|
||||||
if (mt.isValid()) {
|
if (mt.isValid()) {
|
||||||
bool foundProjectManager = false;
|
if (IProjectManager *manager = IProjectManager::managerForMimeType(mt)) {
|
||||||
foreach (IProjectManager *manager, projectManagers) {
|
|
||||||
if (mt.matchesName(manager->mimeType())) {
|
|
||||||
foundProjectManager = true;
|
|
||||||
if (!QFileInfo(filePath).isFile()) {
|
if (!QFileInfo(filePath).isFile()) {
|
||||||
appendError(errorString,
|
appendError(errorString,
|
||||||
tr("Failed opening project \"%1\": Project is not a file").arg(fileName));
|
tr("Failed opening project \"%1\": Project is not a file").arg(fileName));
|
||||||
@@ -1726,10 +1715,7 @@ ProjectExplorerPlugin::OpenProjectResult ProjectExplorerPlugin::openProjects(con
|
|||||||
delete pro;
|
delete pro;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
} else {
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!foundProjectManager) {
|
|
||||||
appendError(errorString, tr("Failed opening project \"%1\": No plugin can open project type \"%2\".")
|
appendError(errorString, tr("Failed opening project \"%1\": No plugin can open project type \"%2\".")
|
||||||
.arg(QDir::toNativeSeparators(fileName))
|
.arg(QDir::toNativeSeparators(fileName))
|
||||||
.arg(mt.name()));
|
.arg(mt.name()));
|
||||||
@@ -3401,11 +3387,11 @@ ProjectExplorerSettings ProjectExplorerPlugin::projectExplorerSettings()
|
|||||||
QStringList ProjectExplorerPlugin::projectFilePatterns()
|
QStringList ProjectExplorerPlugin::projectFilePatterns()
|
||||||
{
|
{
|
||||||
QStringList patterns;
|
QStringList patterns;
|
||||||
foreach (const IProjectManager *pm, allProjectManagers()) {
|
dd->m_projectManagers.forEachObject([&](IProjectManager *pm) {
|
||||||
Utils::MimeType mt = Utils::mimeTypeForName(pm->mimeType());
|
Utils::MimeType mt = Utils::mimeTypeForName(pm->mimeType());
|
||||||
if (mt.isValid())
|
if (mt.isValid())
|
||||||
patterns.append(mt.globPatterns());
|
patterns.append(mt.globPatterns());
|
||||||
}
|
});
|
||||||
return patterns;
|
return patterns;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -3422,4 +3408,17 @@ QList<QPair<QString, QString> > ProjectExplorerPlugin::recentProjects()
|
|||||||
return dd->recentProjects();
|
return dd->recentProjects();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
IProjectManager::IProjectManager()
|
||||||
|
{
|
||||||
|
dd->m_projectManagers.addObject(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
IProjectManager *IProjectManager::managerForMimeType(const Utils::MimeType &mt)
|
||||||
|
{
|
||||||
|
if (!mt.isValid())
|
||||||
|
return nullptr;
|
||||||
|
auto pred = [mt](IProjectManager *m) { return mt.matchesName(m->mimeType()); };
|
||||||
|
return dd->m_projectManagers.findObject(pred);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace ProjectExplorer
|
} // namespace ProjectExplorer
|
||||||
|
|||||||
Reference in New Issue
Block a user