Commit fc731a3a authored by Nikolai Kosjar's avatar Nikolai Kosjar Committed by Erik Verbruggen

CppTools: Introduce LocatorData

Until now the locator filters CppLocatorFilter, CppClassesFilter and
CppFunctionsFilter used unnecessarily their own SearchSymbols instance.
The results were also saved separately, which was unfavorable since e.g.
the functions and classes data could be shared.

Starting with this patch an instance of LocatorData will serve all
mentioned filters.

This saves about 20MB of memory after indexing the Qt Creator project.

Change-Id: I8a34b67eb9fe0e5c68ba6e7c8f576389c78efc6f
Reviewed-by: default avatarThomas Hartmann <Thomas.Hartmann@digia.com>
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent de1ec224
......@@ -32,21 +32,24 @@
using namespace CppTools;
using namespace CppTools::Internal;
CppClassesFilter::CppClassesFilter(CppModelManager *manager)
: CppLocatorFilter(manager)
CppClassesFilter::CppClassesFilter(CppLocatorData *locatorData)
: CppLocatorFilter(locatorData)
{
setId("Classes");
setShortcutString(QLatin1String("c"));
setIncludedByDefault(false);
setDisplayName(tr("C++ Classes"));
search.setSymbolsToSearchFor(SymbolSearcher::Classes);
}
CppClassesFilter::~CppClassesFilter()
{
}
QList<QList<CppTools::ModelItemInfo> > CppClassesFilter::itemsToMatchUserInputAgainst() const
{
return QList<QList<CppTools::ModelItemInfo> >() << m_data->classes();
}
Locator::FilterEntry CppClassesFilter::filterEntryFromModelItemInfo(const ModelItemInfo &info)
{
const QVariant id = qVariantFromValue(info);
......
......@@ -31,6 +31,7 @@
#define CPPCLASSESFILTER_H
#include "cpptools_global.h"
#include "cpplocatordata.h"
#include "cpplocatorfilter.h"
namespace CppTools {
......@@ -40,10 +41,11 @@ class CPPTOOLS_EXPORT CppClassesFilter : public Internal::CppLocatorFilter
Q_OBJECT
public:
CppClassesFilter(Internal::CppModelManager *manager);
CppClassesFilter(Internal::CppLocatorData *locatorData);
~CppClassesFilter();
private:
QList<QList<CppTools::ModelItemInfo> > itemsToMatchUserInputAgainst() const;
Locator::FilterEntry filterEntryFromModelItemInfo(const ModelItemInfo &info);
};
......
......@@ -31,21 +31,24 @@
using namespace CppTools::Internal;
CppFunctionsFilter::CppFunctionsFilter(CppModelManager *manager)
: CppLocatorFilter(manager)
CppFunctionsFilter::CppFunctionsFilter(CppLocatorData *locatorData)
: CppLocatorFilter(locatorData)
{
setId("Methods");
setDisplayName(tr("C++ Methods and Functions"));
setShortcutString(QString(QLatin1Char('m')));
setIncludedByDefault(false);
search.setSymbolsToSearchFor(SymbolSearcher::Functions);
}
CppFunctionsFilter::~CppFunctionsFilter()
{
}
QList<QList<CppTools::ModelItemInfo> > CppFunctionsFilter::itemsToMatchUserInputAgainst() const
{
return QList<QList<CppTools::ModelItemInfo> >() << m_data->functions();
}
Locator::FilterEntry CppFunctionsFilter::filterEntryFromModelItemInfo(const CppTools::ModelItemInfo &info)
{
const QVariant id = qVariantFromValue(info);
......
......@@ -30,6 +30,7 @@
#ifndef CPPFUNCTIONSFILTER_H
#define CPPFUNCTIONSFILTER_H
#include "cpplocatordata.h"
#include "cpplocatorfilter.h"
namespace CppTools {
......@@ -40,10 +41,11 @@ class CppFunctionsFilter : public CppLocatorFilter
Q_OBJECT
public:
CppFunctionsFilter(CppModelManager *manager);
CppFunctionsFilter(CppLocatorData *locatorData);
~CppFunctionsFilter();
private:
QList<QList<ModelItemInfo> > itemsToMatchUserInputAgainst() const;
Locator::FilterEntry filterEntryFromModelItemInfo(const ModelItemInfo &info);
};
......
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#include "cpplocatordata.h"
using namespace CppTools;
using namespace CppTools::Internal;
static const int MaxPendingDocuments = 10;
CppLocatorData::CppLocatorData(CppModelManager *modelManager)
: m_modelManager(modelManager)
, m_pendingDocumentsMutex(QMutex::Recursive)
{
m_search.setSymbolsToSearchFor(SymbolSearcher::Enums
| SymbolSearcher::Classes
| SymbolSearcher::Functions);
m_pendingDocuments.reserve(MaxPendingDocuments);
connect(m_modelManager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
connect(m_modelManager, SIGNAL(aboutToRemoveFiles(QStringList)),
this, SLOT(onAboutToRemoveFiles(QStringList)));
}
QList<ModelItemInfo> CppLocatorData::enums()
{
flushPendingDocument(true);
return allModelItemInfos(m_allEnums);
}
QList<ModelItemInfo> CppLocatorData::classes()
{
flushPendingDocument(true);
return allModelItemInfos(m_allClasses);
}
QList<ModelItemInfo> CppLocatorData::functions()
{
flushPendingDocument(true);
return allModelItemInfos(m_allFunctions);
}
void CppLocatorData::onDocumentUpdated(const CPlusPlus::Document::Ptr &document)
{
QMutexLocker locker(&m_pendingDocumentsMutex);
int i = 0, ei = m_pendingDocuments.size();
for (; i < ei; ++i) {
const CPlusPlus::Document::Ptr &doc = m_pendingDocuments.at(i);
if (doc->fileName() == document->fileName()
&& doc->revision() < document->revision()) {
m_pendingDocuments[i] = document;
break;
}
}
if (i == ei)
m_pendingDocuments.append(document);
flushPendingDocument(false);
}
void CppLocatorData::onAboutToRemoveFiles(const QStringList &files)
{
QMutexLocker locker(&m_pendingDocumentsMutex);
for (int i = 0; i < m_pendingDocuments.size(); ) {
if (files.contains(m_pendingDocuments.at(i)->fileName()))
m_pendingDocuments.remove(i);
else
++i;
}
foreach (const QString &file, files) {
m_allEnums.remove(file);
m_allClasses.remove(file);
m_allFunctions.remove(file);
removeFilePath(file);
}
}
void CppLocatorData::flushPendingDocument(bool force)
{
QMutexLocker locker(&m_pendingDocumentsMutex);
if (!force && m_pendingDocuments.size() < MaxPendingDocuments)
return;
foreach (CPlusPlus::Document::Ptr doc, m_pendingDocuments) {
const QString fileName = findOrInsertFilePath(doc->fileName());
QList<ModelItemInfo> resultsEnums;
QList<ModelItemInfo> resultsClasses;
QList<ModelItemInfo> resultsFunctions;
const int sizeHint = m_allEnums[fileName].size() + m_allClasses[fileName].size()
+ m_allFunctions[fileName].size() + 10;
const QList<ModelItemInfo> results = m_search(doc, sizeHint);
foreach (const ModelItemInfo &info, results) {
switch (info.type) {
case ModelItemInfo::Enum:
resultsEnums.append(info);
break;
case ModelItemInfo::Class:
resultsClasses.append(info);
break;
case ModelItemInfo::Method:
resultsFunctions.append(info);
break;
default:
break;
}
}
m_allEnums[fileName] = resultsEnums;
m_allClasses[fileName] = resultsClasses;
m_allFunctions[fileName] = resultsFunctions;
}
m_pendingDocuments.clear();
m_pendingDocuments.reserve(MaxPendingDocuments);
}
QList<ModelItemInfo> CppLocatorData::allModelItemInfos(const QHash<QString,
QList<ModelItemInfo> > &items) const
{
QList<ModelItemInfo> result;
QHashIterator<QString, QList<ModelItemInfo> > it(items);
while (it.hasNext()) {
it.next();
result.append(it.value());
}
return result;
}
/****************************************************************************
**
** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
** Contact: http://www.qt-project.org/legal
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Digia. For licensing terms and
** conditions see http://qt.digia.com/licensing. For further information
** use the contact form at http://qt.digia.com/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Digia gives you certain additional
** rights. These rights are described in the Digia Qt LGPL Exception
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
****************************************************************************/
#ifndef CPPLOCATORDATA_H
#define CPPLOCATORDATA_H
#include <QHash>
#include <QVector>
#include <cplusplus/CppDocument.h>
#include "cppmodelmanager.h"
#include "searchsymbols.h"
namespace CppTools {
namespace Internal {
class CppLocatorData : public QObject
{
Q_OBJECT
public:
explicit CppLocatorData(CppModelManager *modelManager);
QList<ModelItemInfo> enums();
QList<ModelItemInfo> classes();
QList<ModelItemInfo> functions();
private slots:
void onDocumentUpdated(const CPlusPlus::Document::Ptr &document);
void onAboutToRemoveFiles(const QStringList &files);
private:
void flushPendingDocument(bool force);
QList<ModelItemInfo> allModelItemInfos(const QHash<QString,
QList<ModelItemInfo> > &items) const;
QString findOrInsertFilePath(const QString &path)
{ return *m_filePaths.insert(path); }
void removeFilePath(const QString &path)
{ m_filePaths.remove(path); }
private:
CppModelManager *m_modelManager;
QSet<QString> m_filePaths; // Used to avoid QString duplication
SearchSymbols m_search;
QHash<QString, QList<ModelItemInfo> > m_allEnums;
QHash<QString, QList<ModelItemInfo> > m_allClasses;
QHash<QString, QList<ModelItemInfo> > m_allFunctions;
mutable QMutex m_pendingDocumentsMutex;
QVector<CPlusPlus::Document::Ptr> m_pendingDocuments;
};
} // namespace Internal
} // namespace CppTools
#endif // CPPLOCATORDATA_H
......@@ -36,76 +36,17 @@ using namespace CppTools::Internal;
static const int MaxPendingDocuments = 10;
CppLocatorFilter::CppLocatorFilter(CppModelManager *manager)
: m_manager(manager)
, m_pendingDocumentsMutex(QMutex::Recursive)
CppLocatorFilter::CppLocatorFilter(CppLocatorData *locatorData)
: m_data(locatorData)
{
setId("Classes and Methods");
setDisplayName(tr("C++ Classes and Methods"));
setShortcutString(QString(QLatin1Char(':')));
setIncludedByDefault(false);
m_pendingDocuments.reserve(MaxPendingDocuments);
connect(manager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
connect(manager, SIGNAL(aboutToRemoveFiles(QStringList)),
this, SLOT(onAboutToRemoveFiles(QStringList)));
}
CppLocatorFilter::~CppLocatorFilter()
{ }
void CppLocatorFilter::flushPendingDocument(bool force)
{
QMutexLocker locker(&m_pendingDocumentsMutex);
if (!force && m_pendingDocuments.size() < MaxPendingDocuments)
return;
foreach (CPlusPlus::Document::Ptr doc, m_pendingDocuments) {
QList<ModelItemInfo> &results = m_searchList[doc->fileName()];
results = search(doc, results.size() + 10);
}
m_pendingDocuments.clear();
m_pendingDocuments.reserve(MaxPendingDocuments);
}
void CppLocatorFilter::onDocumentUpdated(CPlusPlus::Document::Ptr updatedDoc)
{
QMutexLocker locker(&m_pendingDocumentsMutex);
int i = 0, ei = m_pendingDocuments.size();
for (; i < ei; ++i) {
const CPlusPlus::Document::Ptr &doc = m_pendingDocuments.at(i);
if (doc->fileName() == updatedDoc->fileName()
&& doc->revision() < updatedDoc->revision()) {
m_pendingDocuments[i] = updatedDoc;
break;
}
}
if (i == ei)
m_pendingDocuments.append(updatedDoc);
flushPendingDocument(false);
}
void CppLocatorFilter::onAboutToRemoveFiles(const QStringList &files)
{
QMutexLocker locker(&m_pendingDocumentsMutex);
for (int i = 0; i < m_pendingDocuments.size(); ) {
if (files.contains(m_pendingDocuments.at(i)->fileName()))
m_pendingDocuments.remove(i);
else
++i;
}
foreach (const QString &file, files)
m_searchList.remove(file);
}
Locator::FilterEntry CppLocatorFilter::filterEntryFromModelItemInfo(const CppTools::ModelItemInfo &info)
......@@ -124,6 +65,14 @@ void CppLocatorFilter::refresh(QFutureInterface<void> &future)
Q_UNUSED(future)
}
QList<QList<CppTools::ModelItemInfo> > CppLocatorFilter::itemsToMatchUserInputAgainst() const
{
return QList<QList<CppTools::ModelItemInfo> >()
<< m_data->classes()
<< m_data->functions()
<< m_data->enums();
}
static bool compareLexigraphically(const Locator::FilterEntry &a,
const Locator::FilterEntry &b)
{
......@@ -132,8 +81,6 @@ static bool compareLexigraphically(const Locator::FilterEntry &a,
QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &origEntry)
{
flushPendingDocument(true);
QString entry = trimWildcards(origEntry);
QList<Locator::FilterEntry> goodEntries;
QList<Locator::FilterEntry> betterEntries;
......@@ -146,18 +93,14 @@ QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(QFutureInterface<Locato
bool hasColonColon = entry.contains(QLatin1String("::"));
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
QHashIterator<QString, QList<ModelItemInfo> > it(m_searchList);
while (it.hasNext()) {
if (future.isCanceled())
break;
it.next();
const QList<ModelItemInfo> items = it.value();
const QList<QList<CppTools::ModelItemInfo> > itemLists = itemsToMatchUserInputAgainst();
foreach (const QList<CppTools::ModelItemInfo> &items, itemLists) {
foreach (const ModelItemInfo &info, items) {
if (future.isCanceled())
break;
const QString matchString = hasColonColon ? info.scopedSymbolName() : info.symbolName;
if ((hasWildcard && regexp.exactMatch(matchString))
|| (!hasWildcard && matcher.indexIn(matchString) != -1)) {
|| (!hasWildcard && matcher.indexIn(matchString) != -1)) {
const Locator::FilterEntry filterEntry = filterEntryFromModelItemInfo(info);
if (matchString.startsWith(entry, caseSensitivityForPrefix))
betterEntries.append(filterEntry);
......@@ -168,9 +111,9 @@ QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(QFutureInterface<Locato
}
if (goodEntries.size() < 1000)
qSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
qStableSort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
if (betterEntries.size() < 1000)
qSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
qStableSort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
betterEntries += goodEntries;
return betterEntries;
......@@ -181,9 +124,3 @@ void CppLocatorFilter::accept(Locator::FilterEntry selection) const
ModelItemInfo info = qvariant_cast<CppTools::ModelItemInfo>(selection.internalData);
Core::EditorManager::openEditorAt(info.fileName, info.line, info.column);
}
void CppLocatorFilter::reset()
{
m_searchList.clear();
m_previousEntry.clear();
}
......@@ -30,6 +30,7 @@
#ifndef CPPLOCATORFILTER_H
#define CPPLOCATORFILTER_H
#include "cpplocatordata.h"
#include "searchsymbols.h"
#include <locator/ilocatorfilter.h>
......@@ -44,35 +45,19 @@ class CppLocatorFilter : public Locator::ILocatorFilter
Q_OBJECT
public:
CppLocatorFilter(CppModelManager *manager);
CppLocatorFilter(CppLocatorData *locatorData);
~CppLocatorFilter();
QList<Locator::FilterEntry> matchesFor(QFutureInterface<Locator::FilterEntry> &future, const QString &entry);
void accept(Locator::FilterEntry selection) const;
void refresh(QFutureInterface<void> &future);
void reset();
protected:
SearchSymbols search;
void flushPendingDocument(bool force);
private slots:
void onDocumentUpdated(CPlusPlus::Document::Ptr updatedDoc);
void onAboutToRemoveFiles(const QStringList &files);
private:
virtual QList<QList<ModelItemInfo> > itemsToMatchUserInputAgainst() const;
virtual Locator::FilterEntry filterEntryFromModelItemInfo(const ModelItemInfo &info);
private:
CppModelManager *m_manager;
QHash<QString, QList<ModelItemInfo> > m_searchList;
QString m_previousEntry;
mutable QMutex m_pendingDocumentsMutex;
QVector<CPlusPlus::Document::Ptr> m_pendingDocuments;
protected:
CppLocatorData *m_data;
};
} // namespace Internal
......
......@@ -213,12 +213,12 @@ void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter_data()
<< cppLocatorFilter
<< _("my")
<< (QList<ResultData>()
<< ResultData(_("MyClass"), testFileShort)
<< ResultData(_("MyClass"), testFileShort)
<< ResultData(_("MyClass"), testFileShort)
<< ResultData(_("MyClass"), _("()"))
<< ResultData(_("MyClass"), _("()"))
<< ResultData(_("MyClass"), testFileShort)
<< ResultData(_("MyClass"), _("()"))
<< ResultData(_("MyClass"), testFileShort)
<< ResultData(_("MyEnum"), testFileShort)
<< ResultData(_("MyEnum"), testFileShort)
<< ResultData(_("MyEnum"), testFileShort)
......
......@@ -45,7 +45,8 @@ HEADERS += completionsettingspage.h \
cpppointerdeclarationformatter.h \
cppprojectfile.h \
cpppreprocessor.h \
includeutils.h
includeutils.h \
cpplocatordata.h
SOURCES += completionsettingspage.cpp \
cppclassesfilter.cpp \
......@@ -89,7 +90,8 @@ SOURCES += completionsettingspage.cpp \
cpppointerdeclarationformatter.cpp \
cppprojectfile.cpp \
cpppreprocessor.cpp \
includeutils.cpp
includeutils.cpp \
cpplocatordata.cpp
FORMS += completionsettingspage.ui \
cppfilesettingspage.ui \
......
......@@ -64,6 +64,8 @@ QtcPlugin {
"cppindexingsupport.h",
"cpplocalsymbols.cpp",
"cpplocalsymbols.h",
"cpplocatordata.cpp",
"cpplocatordata.h",
"cpplocatorfilter.cpp",
"cpplocatorfilter.h",
"cppmodelmanager.cpp",
......
......@@ -40,6 +40,7 @@
#include "cpptoolssettings.h"
#include "cpptoolsreuse.h"
#include "cppprojectfile.h"
#include "cpplocatordata.h"
#include <coreplugin/actionmanager/actioncontainer.h>
#include <coreplugin/actionmanager/actionmanager.h>
......@@ -101,9 +102,11 @@ bool CppToolsPlugin::initialize(const QStringList &arguments, QString *error)
connect(Core::DocumentManager::instance(), SIGNAL(filesChangedInternally(QStringList)),
modelManager, SLOT(updateSourceFiles(QStringList)));
addAutoReleasedObject(new CppLocatorFilter(modelManager));
addAutoReleasedObject(new CppClassesFilter(modelManager));
addAutoReleasedObject(new CppFunctionsFilter(modelManager));
CppLocatorData *locatorData = new CppLocatorData(modelManager);
addAutoReleasedObject(locatorData);
addAutoReleasedObject(new CppLocatorFilter(locatorData));
addAutoReleasedObject(new CppClassesFilter(locatorData));
addAutoReleasedObject(new CppFunctionsFilter(locatorData));
addAutoReleasedObject(new CppCurrentDocumentFilter(modelManager));
addAutoReleasedObject(new CppFileSettingsPage(m_fileSettings));
addAutoReleasedObject(new SymbolsFindFilter(modelManager));
......
......@@ -57,18 +57,6 @@ QList<FilterEntry> BasicLocatorFilterTest::matchesFor(const QString &searchText)
return locatorSearch.results();
}
QT_BEGIN_NAMESPACE
namespace QTest {
template<> char *toString(const Locator::Internal::Tests::ResultData &data)
{
QByteArray ba = "\"" + data.textColumn1.toUtf8() + "\", \"" + data.textColumn2.toUtf8() + "\"";
return qstrdup(ba.data());
}
} // namespace QTest
QT_END_NAMESPACE
ResultData::ResultData()
{
}
......
......@@ -83,4 +83,16 @@ typedef ResultData::ResultDataList ResultDataList;
Q_DECLARE_METATYPE(Locator::Internal::Tests::ResultData)
Q_DECLARE_METATYPE(Locator::Internal::Tests::ResultDataList)
QT_BEGIN_NAMESPACE
namespace QTest {
template<> inline char *toString(const Locator::Internal::Tests::ResultData &data)
{
QByteArray ba = "\"" + data.textColumn1.toUtf8() + "\", \"" + data.textColumn2.toUtf8() + "\"";
return qstrdup(ba.data());
}
} // namespace QTest
QT_END_NAMESPACE
#endif // LOCATORFILTERTEST_H
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