Commit e3f340f7 authored by Erik Verbruggen's avatar Erik Verbruggen

C++: Add child items to ModelItemInfo.

Change-Id: I849e0819a54dc8d6c49675c78d6668daf5c40af4
Reviewed-by: default avatarNikolai Kosjar <nikolai.kosjar@digia.com>
Reviewed-by: default avatarDavid Schulz <david.schulz@digia.com>
parent 6a514c8b
...@@ -125,10 +125,8 @@ public: ...@@ -125,10 +125,8 @@ public:
break; break;
if (m_fileNames.isEmpty() || m_fileNames.contains(it.value()->fileName())) { if (m_fileNames.isEmpty() || m_fileNames.contains(it.value()->fileName())) {
QVector<Core::SearchResultItem> resultItems; QVector<Core::SearchResultItem> resultItems;
QList<ModelItemInfo::Ptr> modelInfos = search(it.value()); search(it.value())->visitAllChildren([&](const ModelItemInfo::Ptr &info) {
foreach (const ModelItemInfo::Ptr &info, modelInfos) { if (matcher.indexIn(info->symbolName()) != -1) {
int index = matcher.indexIn(info->symbolName());
if (index != -1) {
QString text = info->symbolName(); QString text = info->symbolName();
QString scope = info->symbolScope(); QString scope = info->symbolScope();
if (info->type() == ModelItemInfo::Function) { if (info->type() == ModelItemInfo::Function) {
...@@ -150,7 +148,7 @@ public: ...@@ -150,7 +148,7 @@ public:
item.userData = qVariantFromValue(info); item.userData = qVariantFromValue(info);
resultItems << item; resultItems << item;
} }
} });
if (!resultItems.isEmpty()) if (!resultItems.isEmpty())
future.reportResults(resultItems); future.reportResults(resultItems);
} }
......
...@@ -79,7 +79,9 @@ QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor( ...@@ -79,7 +79,9 @@ QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor(
Snapshot snapshot = m_modelManager->snapshot(); Snapshot snapshot = m_modelManager->snapshot();
Document::Ptr thisDocument = snapshot.document(m_currentFileName); Document::Ptr thisDocument = snapshot.document(m_currentFileName);
if (thisDocument) if (thisDocument)
m_itemsOfCurrentDoc = search(thisDocument); search(thisDocument)->visitAllChildren([&](const ModelItemInfo::Ptr &info){
m_itemsOfCurrentDoc.append(info);
});
} }
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry); const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
......
...@@ -126,8 +126,7 @@ void CppLocatorData::flushPendingDocument(bool force) ...@@ -126,8 +126,7 @@ void CppLocatorData::flushPendingDocument(bool force)
const int sizeHint = m_allEnums[fileName].size() + m_allClasses[fileName].size() const int sizeHint = m_allEnums[fileName].size() + m_allClasses[fileName].size()
+ m_allFunctions[fileName].size() + 10; + m_allFunctions[fileName].size() + 10;
const QList<ModelItemInfo::Ptr> results = m_search(doc, sizeHint); m_search(doc, sizeHint)->visitAllChildren([&](const ModelItemInfo::Ptr &info) {
foreach (ModelItemInfo::Ptr info, results) {
switch (info->type()) { switch (info->type()) {
case ModelItemInfo::Enum: case ModelItemInfo::Enum:
resultsEnums.append(info); resultsEnums.append(info);
...@@ -141,7 +140,7 @@ void CppLocatorData::flushPendingDocument(bool force) ...@@ -141,7 +140,7 @@ void CppLocatorData::flushPendingDocument(bool force)
default: default:
break; break;
} }
} });
m_allEnums[fileName] = resultsEnums; m_allEnums[fileName] = resultsEnums;
m_allClasses[fileName] = resultsClasses; m_allClasses[fileName] = resultsClasses;
......
...@@ -30,12 +30,17 @@ ...@@ -30,12 +30,17 @@
#include "searchsymbols.h" #include "searchsymbols.h"
#include <cplusplus/LookupContext.h> #include <cplusplus/LookupContext.h>
#include <utils/qtcassert.h>
#include <utils/scopedswap.h>
#include <QDebug> #include <QDebug>
using namespace CPlusPlus; using namespace CPlusPlus;
using namespace CppTools; using namespace CppTools;
typedef Utils::ScopedSwap<ModelItemInfo::Ptr> ScopedModelItemInfoPtr;
typedef Utils::ScopedSwap<QString> ScopedScope;
SearchSymbols::SymbolTypes SearchSymbols::AllTypes = SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
SymbolSearcher::Classes SymbolSearcher::Classes
| SymbolSearcher::Functions | SymbolSearcher::Functions
...@@ -53,28 +58,29 @@ void SearchSymbols::setSymbolsToSearchFor(const SymbolTypes &types) ...@@ -53,28 +58,29 @@ void SearchSymbols::setSymbolsToSearchFor(const SymbolTypes &types)
symbolsToSearchFor = types; symbolsToSearchFor = types;
} }
QList<ModelItemInfo::Ptr> SearchSymbols::operator()(Document::Ptr doc, int sizeHint, ModelItemInfo::Ptr SearchSymbols::operator()(Document::Ptr doc, int sizeHint, const QString &scope)
const QString &scope)
{ {
QString previousScope = switchScope(scope); ModelItemInfo::Ptr root = ModelItemInfo::create(findOrInsert(doc->fileName()), sizeHint);
items.clear();
items.reserve(sizeHint); { // RAII scope
for (unsigned i = 0; i < doc->globalSymbolCount(); ++i) { ScopedModelItemInfoPtr parentRaii(_parent, root);
accept(doc->globalSymbolAt(i)); QString newScope = scope;
ScopedScope scopeRaii(_scope, newScope);
QTC_ASSERT(_parent, return ModelItemInfo::Ptr());
QTC_ASSERT(root, return ModelItemInfo::Ptr());
QTC_ASSERT(_parent->fileName() == findOrInsert(doc->fileName()),
return ModelItemInfo::Ptr());
for (unsigned i = 0, ei = doc->globalSymbolCount(); i != ei; ++i)
accept(doc->globalSymbolAt(i));
strings.scheduleGC();
m_paths.clear();
} }
(void) switchScope(previousScope);
QList<ModelItemInfo::Ptr> result = items;
strings.scheduleGC();
items.clear();
m_paths.clear();
return result;
}
QString SearchSymbols::switchScope(const QString &scope) root->squeeze();
{ return root;
QString previousScope = _scope;
_scope = scope;
return previousScope;
} }
bool SearchSymbols::visit(Enum *symbol) bool SearchSymbols::visit(Enum *symbol)
...@@ -83,13 +89,18 @@ bool SearchSymbols::visit(Enum *symbol) ...@@ -83,13 +89,18 @@ bool SearchSymbols::visit(Enum *symbol)
return false; return false;
QString name = overview.prettyName(symbol->name()); QString name = overview.prettyName(symbol->name());
QString scopedName = scopedSymbolName(name, symbol); ModelItemInfo::Ptr newParent =
QString previousScope = switchScope(scopedName); addChildItem(name, QString(), _scope, ModelItemInfo::Enum, symbol);
appendItem(name, QString(), previousScope, ModelItemInfo::Enum, symbol); if (!newParent)
for (unsigned i = 0; i < symbol->memberCount(); ++i) { newParent = _parent;
ScopedModelItemInfoPtr parentRaii(_parent, newParent);
QString newScope = scopedSymbolName(name, symbol);
ScopedScope scopeRaii(_scope, newScope);
for (unsigned i = 0, ei = symbol->memberCount(); i != ei; ++i)
accept(symbol->memberAt(i)); accept(symbol->memberAt(i));
}
(void) switchScope(previousScope);
return false; return false;
} }
...@@ -99,18 +110,18 @@ bool SearchSymbols::visit(Function *symbol) ...@@ -99,18 +110,18 @@ bool SearchSymbols::visit(Function *symbol)
return false; return false;
QString name = overview.prettyName(symbol->name()); QString name = overview.prettyName(symbol->name());
QString type = overview.prettyType(symbol->type()); QString type = overview.prettyType(symbol->type());
appendItem(name, type, _scope, ModelItemInfo::Function, symbol); addChildItem(name, type, _scope, ModelItemInfo::Function, symbol);
return false; return false;
} }
bool SearchSymbols::visit(Namespace *symbol) bool SearchSymbols::visit(Namespace *symbol)
{ {
QString name = scopedSymbolName(symbol); QString name = scopedSymbolName(symbol);
QString previousScope = switchScope(name); QString newScope = name;
ScopedScope raii(_scope, newScope);
for (unsigned i = 0; i < symbol->memberCount(); ++i) { for (unsigned i = 0; i < symbol->memberCount(); ++i) {
accept(symbol->memberAt(i)); accept(symbol->memberAt(i));
} }
(void) switchScope(previousScope);
return false; return false;
} }
...@@ -132,10 +143,10 @@ bool SearchSymbols::visit(Declaration *symbol) ...@@ -132,10 +143,10 @@ bool SearchSymbols::visit(Declaration *symbol)
if (symbol->name()) { if (symbol->name()) {
QString name = overview.prettyName(symbol->name()); QString name = overview.prettyName(symbol->name());
QString type = overview.prettyType(symbol->type()); QString type = overview.prettyType(symbol->type());
appendItem(name, type, _scope, addChildItem(name, type, _scope,
symbol->type()->asFunctionType() ? ModelItemInfo::Function symbol->type()->asFunctionType() ? ModelItemInfo::Function
: ModelItemInfo::Declaration, : ModelItemInfo::Declaration,
symbol); symbol);
} }
return false; return false;
...@@ -144,14 +155,19 @@ bool SearchSymbols::visit(Declaration *symbol) ...@@ -144,14 +155,19 @@ bool SearchSymbols::visit(Declaration *symbol)
bool SearchSymbols::visit(Class *symbol) bool SearchSymbols::visit(Class *symbol)
{ {
QString name = overview.prettyName(symbol->name()); QString name = overview.prettyName(symbol->name());
QString scopedName = scopedSymbolName(name, symbol);
QString previousScope = switchScope(scopedName); ModelItemInfo::Ptr newParent;
if (symbolsToSearchFor & SymbolSearcher::Classes) if (symbolsToSearchFor & SymbolSearcher::Classes)
appendItem(name, QString(), previousScope, ModelItemInfo::Class, symbol); newParent = addChildItem(name, QString(), _scope, ModelItemInfo::Class, symbol);
for (unsigned i = 0; i < symbol->memberCount(); ++i) { if (!newParent)
newParent = _parent;
ScopedModelItemInfoPtr parentRaii(_parent, newParent);
QString newScope = scopedSymbolName(name, symbol);
ScopedScope scopeRaii(_scope, newScope);
for (unsigned i = 0, ei = symbol->memberCount(); i != ei; ++i)
accept(symbol->memberAt(i)); accept(symbol->memberAt(i));
}
(void) switchScope(previousScope);
return false; return false;
} }
...@@ -275,12 +291,13 @@ QString SearchSymbols::scopeName(const QString &name, const Symbol *symbol) cons ...@@ -275,12 +291,13 @@ QString SearchSymbols::scopeName(const QString &name, const Symbol *symbol) cons
} }
} }
void SearchSymbols::appendItem(const QString &symbolName, const QString &symbolType, ModelItemInfo::Ptr SearchSymbols::addChildItem(const QString &symbolName, const QString &symbolType,
const QString &symbolScope, ModelItemInfo::ItemType itemType, const QString &symbolScope,
Symbol *symbol) ModelItemInfo::ItemType itemType,
Symbol *symbol)
{ {
if (!symbol->name() || symbol->isGenerated()) if (!symbol->name() || symbol->isGenerated())
return; return ModelItemInfo::Ptr();
QString path = m_paths.value(symbol->fileId(), QString()); QString path = m_paths.value(symbol->fileId(), QString());
if (path.isEmpty()) { if (path.isEmpty()) {
...@@ -289,12 +306,30 @@ void SearchSymbols::appendItem(const QString &symbolName, const QString &symbolT ...@@ -289,12 +306,30 @@ void SearchSymbols::appendItem(const QString &symbolName, const QString &symbolT
} }
const QIcon icon = icons.iconForSymbol(symbol); const QIcon icon = icons.iconForSymbol(symbol);
items.append(ModelItemInfo::create(findOrInsert(symbolName), ModelItemInfo::Ptr newItem = ModelItemInfo::create(findOrInsert(symbolName),
findOrInsert(symbolType), findOrInsert(symbolType),
findOrInsert(symbolScope), findOrInsert(symbolScope),
itemType, itemType,
findOrInsert(path), findOrInsert(path),
symbol->line(), symbol->line(),
symbol->column() - 1, // 1-based vs 0-based column symbol->column() - 1, // 1-based vs 0-based column
icon)); icon);
_parent->addChild(newItem);
return newItem;
}
void ModelItemInfo::squeeze()
{
m_children.squeeze();
for (int i = 0, ei = m_children.size(); i != ei; ++i)
m_children[i]->squeeze();
}
void ModelItemInfo::visitAllChildren(std::function<void (const ModelItemInfo::Ptr &)> f) const
{
foreach (const ModelItemInfo::Ptr &child, m_children) {
f(child);
if (!child->m_children.isEmpty())
child->visitAllChildren(f);
}
} }
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <cplusplus/Overview.h> #include <cplusplus/Overview.h>
#include <utils/fileutils.h> #include <utils/fileutils.h>
#include <utils/function.h>
#include <QIcon> #include <QIcon>
#include <QString> #include <QString>
...@@ -46,8 +47,6 @@ ...@@ -46,8 +47,6 @@
#include <QSharedPointer> #include <QSharedPointer>
#include <QHash> #include <QHash>
#include <functional>
namespace CppTools { namespace CppTools {
class CPPTOOLS_EXPORT ModelItemInfo class CPPTOOLS_EXPORT ModelItemInfo
...@@ -76,6 +75,13 @@ private: ...@@ -76,6 +75,13 @@ private:
m_column(column) m_column(column)
{} {}
ModelItemInfo(const QString &fileName, int sizeHint)
: m_fileName(fileName)
, m_type(Declaration)
, m_line(0)
, m_column(0)
{ m_children.reserve(sizeHint); }
public: public:
typedef QSharedPointer<ModelItemInfo> Ptr; typedef QSharedPointer<ModelItemInfo> Ptr;
static Ptr create(const QString &symbolName, static Ptr create(const QString &symbolName,
...@@ -91,6 +97,11 @@ public: ...@@ -91,6 +97,11 @@ public:
symbolName, symbolType, symbolScope, type, fileName, line, column, icon)); symbolName, symbolType, symbolScope, type, fileName, line, column, icon));
} }
static Ptr create(const QString &fileName, int sizeHint)
{
return Ptr(new ModelItemInfo(fileName, sizeHint));
}
QString scopedSymbolName() const QString scopedSymbolName() const
{ {
return m_symbolScope.isEmpty() return m_symbolScope.isEmpty()
...@@ -135,6 +146,11 @@ public: ...@@ -135,6 +146,11 @@ public:
int line() const { return m_line; } int line() const { return m_line; }
int column() const { return m_column; } int column() const { return m_column; }
void addChild(ModelItemInfo::Ptr childItem) { m_children.append(childItem); }
void squeeze();
void visitAllChildren(std::function<void (const ModelItemInfo::Ptr &)> f) const;
private: private:
QString m_symbolName; // as found in the code, therefore might be qualified QString m_symbolName; // as found in the code, therefore might be qualified
QString m_symbolType; QString m_symbolType;
...@@ -144,10 +160,10 @@ private: ...@@ -144,10 +160,10 @@ private:
ItemType m_type; ItemType m_type;
int m_line; int m_line;
int m_column; int m_column;
QVector<ModelItemInfo::Ptr> m_children;
}; };
class SearchSymbols: public std::binary_function<CPlusPlus::Document::Ptr, int, QList<ModelItemInfo> >, class SearchSymbols: protected CPlusPlus::SymbolVisitor
protected CPlusPlus::SymbolVisitor
{ {
public: public:
typedef SymbolSearcher::SymbolTypes SymbolTypes; typedef SymbolSearcher::SymbolTypes SymbolTypes;
...@@ -158,11 +174,10 @@ public: ...@@ -158,11 +174,10 @@ public:
void setSymbolsToSearchFor(const SymbolTypes &types); void setSymbolsToSearchFor(const SymbolTypes &types);
QList<ModelItemInfo::Ptr> operator()(CPlusPlus::Document::Ptr doc, int sizeHint = 500) ModelItemInfo::Ptr operator()(CPlusPlus::Document::Ptr doc, int sizeHint = 500)
{ return operator()(doc, sizeHint, QString()); } { return operator()(doc, sizeHint, QString()); }
QList<ModelItemInfo::Ptr> operator()(CPlusPlus::Document::Ptr doc, int sizeHint, ModelItemInfo::Ptr operator()(CPlusPlus::Document::Ptr doc, int sizeHint, const QString &scope);
const QString &scope);
protected: protected:
using SymbolVisitor::visit; using SymbolVisitor::visit;
...@@ -170,8 +185,6 @@ protected: ...@@ -170,8 +185,6 @@ protected:
void accept(CPlusPlus::Symbol *symbol) void accept(CPlusPlus::Symbol *symbol)
{ CPlusPlus::Symbol::visitSymbol(symbol, this); } { CPlusPlus::Symbol::visitSymbol(symbol, this); }
QString switchScope(const QString &scope);
virtual bool visit(CPlusPlus::UsingNamespaceDirective *); virtual bool visit(CPlusPlus::UsingNamespaceDirective *);
virtual bool visit(CPlusPlus::UsingDeclaration *); virtual bool visit(CPlusPlus::UsingDeclaration *);
virtual bool visit(CPlusPlus::NamespaceAlias *); virtual bool visit(CPlusPlus::NamespaceAlias *);
...@@ -200,11 +213,11 @@ protected: ...@@ -200,11 +213,11 @@ protected:
QString scopedSymbolName(const QString &symbolName, const CPlusPlus::Symbol *symbol) const; QString scopedSymbolName(const QString &symbolName, const CPlusPlus::Symbol *symbol) const;
QString scopedSymbolName(const CPlusPlus::Symbol *symbol) const; QString scopedSymbolName(const CPlusPlus::Symbol *symbol) const;
QString scopeName(const QString &name, const CPlusPlus::Symbol *symbol) const; QString scopeName(const QString &name, const CPlusPlus::Symbol *symbol) const;
void appendItem(const QString &symbolName, ModelItemInfo::Ptr addChildItem(const QString &symbolName,
const QString &symbolType, const QString &symbolType,
const QString &symbolScope, const QString &symbolScope,
ModelItemInfo::ItemType type, ModelItemInfo::ItemType type,
CPlusPlus::Symbol *symbol); CPlusPlus::Symbol *symbol);
private: private:
QString findOrInsert(const QString &s) QString findOrInsert(const QString &s)
...@@ -212,10 +225,10 @@ private: ...@@ -212,10 +225,10 @@ private:
Internal::StringTable &strings; // Used to avoid QString duplication Internal::StringTable &strings; // Used to avoid QString duplication
ModelItemInfo::Ptr _parent;
QString _scope; QString _scope;
CPlusPlus::Overview overview; CPlusPlus::Overview overview;
CPlusPlus::Icons icons; CPlusPlus::Icons icons;
QList<ModelItemInfo::Ptr> items;
SymbolTypes symbolsToSearchFor; SymbolTypes symbolsToSearchFor;
QHash<const CPlusPlus::StringLiteral *, QString> m_paths; QHash<const CPlusPlus::StringLiteral *, QString> m_paths;
}; };
......
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