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