Commit 760aa0f8 authored by Nikolai Kosjar's avatar Nikolai Kosjar

CppTools: Get rid of SearchSymbols::setSeparateScope()

This will make it easier to use a single SearchSymbols instance and a
single run serving all locator filters.

Change-Id: Idb6a3693ad356227d46d0b28fb4c3a5db62b4ac4
Reviewed-by: default avatarErik Verbruggen <erik.verbruggen@digia.com>
parent 7e09b907
......@@ -100,7 +100,6 @@ public:
SearchSymbols search;
search.setSymbolsToSearchFor(m_parameters.types);
search.setSeparateScope(true);
CPlusPlus::Snapshot::const_iterator it = m_snapshot.begin();
QString findString = (m_parameters.flags & Find::FindRegularExpression
......@@ -120,11 +119,14 @@ public:
foreach (const ModelItemInfo &info, modelInfos) {
int index = matcher.indexIn(info.symbolName);
if (index != -1) {
QStringList path = info.fullyQualifiedName.mid(0,
info.fullyQualifiedName.size() - 1);
QString text = info.typeNameRepresentation();
if (text.isEmpty())
text = info.symbolName;
Find::SearchResultItem item;
item.path = path;
item.text = info.symbolName;
item.path = info.symbolScope.split(QLatin1String("::"),
QString::SkipEmptyParts);
item.text = text;
item.textMarkPos = -1;
item.textMarkLength = 0;
item.icon = info.icon;
......
......@@ -41,9 +41,24 @@ CppClassesFilter::CppClassesFilter(CppModelManager *manager)
setDisplayName(tr("C++ Classes"));
search.setSymbolsToSearchFor(SymbolSearcher::Classes);
search.setSeparateScope(true);
}
CppClassesFilter::~CppClassesFilter()
{
}
QString CppClassesFilter::stringToMatchUserInputAgainst(const ModelItemInfo &info)
{
return info.symbolName;
}
Locator::FilterEntry CppClassesFilter::filterEntryFromModelItemInfo(const ModelItemInfo &info)
{
const QVariant id = qVariantFromValue(info);
Locator::FilterEntry filterEntry(this, info.symbolName, id, info.icon);
filterEntry.extraInfo = info.symbolScope.isEmpty()
? info.shortNativeFilePath()
: info.symbolScope;
return filterEntry;
}
......@@ -42,6 +42,10 @@ class CPPTOOLS_EXPORT CppClassesFilter : public Internal::CppLocatorFilter
public:
CppClassesFilter(Internal::CppModelManager *manager);
~CppClassesFilter();
private:
QString stringToMatchUserInputAgainst(const ModelItemInfo &info);
Locator::FilterEntry filterEntryFromModelItemInfo(const ModelItemInfo &info);
};
} // namespace CppTools
......
......@@ -49,8 +49,6 @@ CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppModelManager *manager, Cor
SymbolSearcher::Functions |
SymbolSearcher::Classes);
search.setSeparateScope(true);
connect(manager, SIGNAL(documentUpdated(CPlusPlus::Document::Ptr)),
this, SLOT(onDocumentUpdated(CPlusPlus::Document::Ptr)));
connect(editorManager, SIGNAL(currentEditorChanged(Core::IEditor*)),
......@@ -88,15 +86,18 @@ QList<Locator::FilterEntry> CppCurrentDocumentFilter::matchesFor(QFutureInterfac
if (future.isCanceled())
break;
if ((hasWildcard && regexp.exactMatch(info.symbolName))
|| (!hasWildcard && matcher.indexIn(info.symbolName) != -1))
QString matchString = info.typeNameRepresentation();
if (matchString.isEmpty())
matchString = info.symbolName;
if ((hasWildcard && regexp.exactMatch(matchString))
|| (!hasWildcard && matcher.indexIn(matchString) != -1))
{
QString symbolName = info.symbolName;// + (info.type == ModelItemInfo::Declaration ? ";" : " {...}");
QVariant id = qVariantFromValue(info);
Locator::FilterEntry filterEntry(this, symbolName, id, info.icon);
filterEntry.extraInfo = info.symbolType;
Locator::FilterEntry filterEntry(this, matchString, id, info.icon);
filterEntry.extraInfo = info.symbolScope;
if (info.symbolName.startsWith(entry, caseSensitivityForPrefix))
if (matchString.startsWith(entry, caseSensitivityForPrefix))
betterEntries.append(filterEntry);
else
goodEntries.append(filterEntry);
......
......@@ -40,9 +40,25 @@ CppFunctionsFilter::CppFunctionsFilter(CppModelManager *manager)
setIncludedByDefault(false);
search.setSymbolsToSearchFor(SymbolSearcher::Functions);
search.setSeparateScope(true);
}
CppFunctionsFilter::~CppFunctionsFilter()
{
}
QString CppFunctionsFilter::stringToMatchUserInputAgainst(const CppTools::ModelItemInfo &info)
{
return info.symbolName;
}
Locator::FilterEntry CppFunctionsFilter::filterEntryFromModelItemInfo(const CppTools::ModelItemInfo &info)
{
const QVariant id = qVariantFromValue(info);
Locator::FilterEntry filterEntry(this, info.symbolName + info.symbolType, id, info.icon);
filterEntry.extraInfo = info.symbolScope.isEmpty()
? info.shortNativeFilePath()
: info.symbolScope;
return filterEntry;
}
......@@ -42,6 +42,10 @@ class CppFunctionsFilter : public CppLocatorFilter
public:
CppFunctionsFilter(CppModelManager *manager);
~CppFunctionsFilter();
private:
QString stringToMatchUserInputAgainst(const ModelItemInfo &info);
Locator::FilterEntry filterEntryFromModelItemInfo(const ModelItemInfo &info);
};
} // namespace Internal
......
......@@ -30,12 +30,9 @@
#include "cpplocatorfilter.h"
#include "cppmodelmanager.h"
#include <utils/fileutils.h>
#include <QStringMatcher>
using namespace CppTools::Internal;
using namespace Utils;
static const int MaxPendingDocuments = 10;
......@@ -111,6 +108,23 @@ void CppLocatorFilter::onAboutToRemoveFiles(const QStringList &files)
m_searchList.remove(file);
}
QString CppLocatorFilter::stringToMatchUserInputAgainst(const CppTools::ModelItemInfo &info)
{
return info.scopedSymbolName();
}
Locator::FilterEntry CppLocatorFilter::filterEntryFromModelItemInfo(const CppTools::ModelItemInfo &info)
{
const QVariant id = qVariantFromValue(info);
const QString name = info.symbolScope.isEmpty() ? info.symbolName : info.scopedSymbolName();
Locator::FilterEntry filterEntry(this, name, id, info.icon);
filterEntry.extraInfo = info.type == ModelItemInfo::Class || info.type == ModelItemInfo::Enum
? info.shortNativeFilePath()
: info.symbolType;
return filterEntry;
}
void CppLocatorFilter::refresh(QFutureInterface<void> &future)
{
Q_UNUSED(future)
......@@ -146,19 +160,11 @@ QList<Locator::FilterEntry> CppLocatorFilter::matchesFor(QFutureInterface<Locato
const QList<ModelItemInfo> items = it.value();
foreach (const ModelItemInfo &info, items) {
if ((hasWildcard && regexp.exactMatch(info.symbolName))
|| (!hasWildcard && matcher.indexIn(info.symbolName) != -1)) {
QVariant id = qVariantFromValue(info);
Locator::FilterEntry filterEntry(this, info.symbolName, id, info.icon);
if (!info.symbolType.isEmpty()) {
filterEntry.extraInfo = info.symbolType;
} else {
filterEntry.extraInfo = FileUtils::shortNativePath(
FileName::fromString(info.fileName));
}
if (info.symbolName.startsWith(entry, caseSensitivityForPrefix))
const QString matchString = stringToMatchUserInputAgainst(info);
if ((hasWildcard && regexp.exactMatch(matchString))
|| (!hasWildcard && matcher.indexIn(matchString) != -1)) {
const Locator::FilterEntry filterEntry = filterEntryFromModelItemInfo(info);
if (matchString.startsWith(entry, caseSensitivityForPrefix))
betterEntries.append(filterEntry);
else
goodEntries.append(filterEntry);
......
......@@ -62,6 +62,10 @@ private slots:
void onDocumentUpdated(CPlusPlus::Document::Ptr updatedDoc);
void onAboutToRemoveFiles(const QStringList &files);
private:
virtual QString stringToMatchUserInputAgainst(const ModelItemInfo &info);
virtual Locator::FilterEntry filterEntryFromModelItemInfo(const ModelItemInfo &info);
private:
CppModelManager *m_manager;
......
......@@ -152,7 +152,7 @@ void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter()
ResultDataList results = ResultData::fromFilterEntryList(test.matchesFor(searchText));
// ResultData::printFilterEntries(results);
QVERIFY(!results.isEmpty());
QCOMPARE(expectedResults, results);
QCOMPARE(results, expectedResults);
}
void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter_data()
......
......@@ -42,9 +42,8 @@ SearchSymbols::SymbolTypes SearchSymbols::AllTypes =
| SymbolSearcher::Enums
| SymbolSearcher::Declarations;
SearchSymbols::SearchSymbols():
symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums),
separateScope(false)
SearchSymbols::SearchSymbols() :
symbolsToSearchFor(SymbolSearcher::Classes | SymbolSearcher::Functions | SymbolSearcher::Enums)
{
}
......@@ -53,11 +52,6 @@ void SearchSymbols::setSymbolsToSearchFor(SymbolTypes types)
symbolsToSearchFor = types;
}
void SearchSymbols::setSeparateScope(bool separateScope)
{
this->separateScope = separateScope;
}
QList<ModelItemInfo> SearchSymbols::operator()(Document::Ptr doc, int sizeHint, const QString &scope)
{
QString previousScope = switchScope(scope);
......@@ -89,9 +83,7 @@ bool SearchSymbols::visit(Enum *symbol)
QString name = symbolName(symbol);
QString scopedName = scopedSymbolName(name);
QString previousScope = switchScope(scopedName);
appendItem(separateScope ? name : scopedName,
separateScope ? previousScope : QString(),
ModelItemInfo::Enum, symbol);
appendItem(name, QString(), previousScope, ModelItemInfo::Enum, symbol);
for (unsigned i = 0; i < symbol->memberCount(); ++i) {
accept(symbol->memberAt(i));
}
......@@ -116,12 +108,8 @@ bool SearchSymbols::visit(Function *symbol)
fullScope += QLatin1String("::");
fullScope += extraScope;
QString name = symbolName(symbol);
QString scopedName = scopedSymbolName(name);
QString type = overview.prettyType(symbol->type(),
separateScope ? symbol->unqualifiedName() : 0);
appendItem(separateScope ? type : scopedName,
separateScope ? fullScope : type,
ModelItemInfo::Method, symbol);
QString type = overview.prettyType(symbol->type());
appendItem(name, type, fullScope, ModelItemInfo::Method, symbol);
return false;
}
......@@ -152,11 +140,8 @@ bool SearchSymbols::visit(Declaration *symbol)
}
QString name = symbolName(symbol);
QString scopedName = scopedSymbolName(name);
QString type = overview.prettyType(symbol->type(),
separateScope ? symbol->unqualifiedName() : 0);
appendItem(separateScope ? type : scopedName,
separateScope ? _scope : type,
QString type = overview.prettyType(symbol->type());
appendItem(name, type, _scope,
symbol->type()->asFunctionType() ? ModelItemInfo::Method
: ModelItemInfo::Declaration,
symbol);
......@@ -169,9 +154,7 @@ bool SearchSymbols::visit(Class *symbol)
QString scopedName = scopedSymbolName(name);
QString previousScope = switchScope(scopedName);
if (symbolsToSearchFor & SymbolSearcher::Classes) {
appendItem(separateScope ? name : scopedName,
separateScope ? previousScope : QString(),
ModelItemInfo::Class, symbol);
appendItem(name, QString(), previousScope, ModelItemInfo::Class, symbol);
}
for (unsigned i = 0; i < symbol->memberCount(); ++i) {
accept(symbol->memberAt(i));
......@@ -305,18 +288,13 @@ QString SearchSymbols::symbolName(const Symbol *symbol) const
return symbolName;
}
void SearchSymbols::appendItem(const QString &name,
const QString &info,
ModelItemInfo::ItemType type,
void SearchSymbols::appendItem(const QString &symbolName, const QString &symbolType,
const QString &symbolScope, ModelItemInfo::ItemType itemType,
Symbol *symbol)
{
if (!symbol->name())
return;
QStringList fullyQualifiedName;
foreach (const Name *name, LookupContext::fullyQualifiedName(symbol))
fullyQualifiedName.append(findOrInsert(overview.prettyName(name)));
QString path = m_paths.value(symbol->fileId(), QString());
if (path.isEmpty()) {
path = QString::fromUtf8(symbol->fileName(), symbol->fileNameLength());
......@@ -324,8 +302,10 @@ void SearchSymbols::appendItem(const QString &name,
}
const QIcon icon = icons.iconForSymbol(symbol);
items.append(ModelItemInfo(findOrInsert(name), findOrInsert(info), type,
fullyQualifiedName,
items.append(ModelItemInfo(findOrInsert(symbolName),
findOrInsert(symbolType),
findOrInsert(symbolScope),
itemType,
path,
symbol->line(),
symbol->column() - 1, // 1-based vs 0-based column
......
......@@ -37,6 +37,8 @@
#include <cplusplus/Icons.h>
#include <cplusplus/Overview.h>
#include <utils/fileutils.h>
#include <QIcon>
#include <QString>
#include <QSet>
......@@ -58,15 +60,15 @@ struct CPPTOOLS_EXPORT ModelItemInfo
ModelItemInfo(const QString &symbolName,
const QString &symbolType,
const QString &symbolScope,
ItemType type,
QStringList fullyQualifiedName,
const QString &fileName,
int line,
int column,
const QIcon &icon)
: symbolName(symbolName),
symbolType(symbolType),
fullyQualifiedName(fullyQualifiedName),
symbolScope(symbolScope),
fileName(fileName),
icon(icon),
type(type),
......@@ -77,17 +79,42 @@ struct CPPTOOLS_EXPORT ModelItemInfo
ModelItemInfo(const ModelItemInfo &otherInfo)
: symbolName(otherInfo.symbolName),
symbolType(otherInfo.symbolType),
fullyQualifiedName(otherInfo.fullyQualifiedName),
symbolScope(otherInfo.symbolScope),
fileName(otherInfo.fileName),
icon(otherInfo.icon),
type(otherInfo.type),
line(otherInfo.line),
column(otherInfo.column)
{ }
{ }
QString scopedSymbolName() const
{
return symbolScope.isEmpty()
? symbolName
: symbolScope + QLatin1String("::") + symbolName;
}
QString typeNameRepresentation() const
{
if (type == ModelItemInfo::Declaration) {
if (!symbolType.isEmpty()) {
const QString padding = symbolType.endsWith(QLatin1Char('*'))
? QString()
: QString(QLatin1Char(' '));
return symbolType + padding + symbolName;
}
} else if (type == ModelItemInfo::Method) {
return symbolName + symbolType;
}
return QString();
}
QString shortNativeFilePath() const
{ return Utils::FileUtils::shortNativePath(Utils::FileName::fromString(fileName)); }
QString symbolName;
QString symbolType;
QStringList fullyQualifiedName;
QString symbolScope;
QString fileName;
QIcon icon;
ItemType type;
......@@ -106,7 +133,6 @@ public:
SearchSymbols();
void setSymbolsToSearchFor(SymbolTypes types);
void setSeparateScope(bool separateScope);
QList<ModelItemInfo> operator()(CPlusPlus::Document::Ptr doc, int sizeHint = 500)
{ return operator()(doc, sizeHint, QString()); }
......@@ -149,8 +175,9 @@ protected:
QString scopedSymbolName(const QString &symbolName) const;
QString scopedSymbolName(const CPlusPlus::Symbol *symbol) const;
QString symbolName(const CPlusPlus::Symbol *symbol) const;
void appendItem(const QString &name,
const QString &info,
void appendItem(const QString &symbolName,
const QString &symbolType,
const QString &symbolScope,
ModelItemInfo::ItemType type,
CPlusPlus::Symbol *symbol);
......@@ -166,7 +193,6 @@ private:
QList<ModelItemInfo> items;
SymbolTypes symbolsToSearchFor;
QHash<const CPlusPlus::StringLiteral *, QString> m_paths;
bool separateScope;
};
} // namespace CppTools
......
......@@ -143,6 +143,18 @@ inline QString _(const QByteArray &ba) { return QString::fromLatin1(ba, ba.size(
Q_DECLARE_METATYPE(ResultData)
Q_DECLARE_METATYPE(ResultDataList)
QT_BEGIN_NAMESPACE
namespace QTest {
template<> char *toString(const ResultData &data)
{
QByteArray ba = "\"" + data.m_symbolName.toUtf8() + "\", \"" + data.m_scope.toUtf8() + "\"";
return qstrdup(ba.data());
}
} // namespace QTest
QT_END_NAMESPACE
void CppToolsPlugin::test_builtinsymbolsearcher()
{
QFETCH(QString, testFile);
......@@ -182,8 +194,8 @@ void CppToolsPlugin::test_builtinsymbolsearcher_data()
<< ResultData(_("int myVariable"), _(""))
<< ResultData(_("myFunction(bool, int)"), _(""))
<< ResultData(_("MyEnum"), _(""))
<< ResultData(_("int V1"), _(""))
<< ResultData(_("int V2"), _(""))
<< ResultData(_("int V1"), _("MyEnum"))
<< ResultData(_("int V2"), _("MyEnum"))
<< ResultData(_("MyClass"), _(""))
<< ResultData(_("MyClass()"), _("MyClass"))
<< ResultData(_("function1()"), _("MyClass"))
......@@ -191,21 +203,21 @@ void CppToolsPlugin::test_builtinsymbolsearcher_data()
<< ResultData(_("int myVariable"), _("MyNamespace"))
<< ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
<< ResultData(_("MyEnum"), _("MyNamespace"))
<< ResultData(_("int V1"), _("MyNamespace"))
<< ResultData(_("int V2"), _("MyNamespace"))
<< ResultData(_("int V1"), _("MyNamespace::MyEnum"))
<< ResultData(_("int V2"), _("MyNamespace::MyEnum"))
<< ResultData(_("MyClass"), _("MyNamespace"))
<< ResultData(_("MyClass()"), _("MyNamespace::MyClass"))
<< ResultData(_("function1()"), _("MyNamespace::MyClass"))
<< ResultData(_("function2(bool, int)"), _("MyNamespace::MyClass"))
<< ResultData(_("int myVariable"), _(""))
<< ResultData(_("myFunction(bool, int)"), _(""))
<< ResultData(_("MyEnum"), _(""))
<< ResultData(_("int V1"), _(""))
<< ResultData(_("int V2"), _(""))
<< ResultData(_("MyClass"), _(""))
<< ResultData(_("MyClass()"), _("MyClass"))
<< ResultData(_("function1()"), _("MyClass"))
<< ResultData(_("function2(bool, int)"), _("MyClass"))
<< ResultData(_("int myVariable"), _("<anonymous namespace>"))
<< ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
<< ResultData(_("MyEnum"), _("<anonymous namespace>"))
<< ResultData(_("int V1"), _("<anonymous namespace>::MyEnum"))
<< ResultData(_("int V2"), _("<anonymous namespace>::MyEnum"))
<< ResultData(_("MyClass"), _("<anonymous namespace>"))
<< ResultData(_("MyClass()"), _("<anonymous namespace>::MyClass"))
<< ResultData(_("function1()"), _("<anonymous namespace>::MyClass"))
<< ResultData(_("function2(bool, int)"), _("<anonymous namespace>::MyClass"))
);
// Check Classes
......@@ -220,7 +232,7 @@ void CppToolsPlugin::test_builtinsymbolsearcher_data()
<< (ResultDataList()
<< ResultData(_("MyClass"), _(""))
<< ResultData(_("MyClass"), _("MyNamespace"))
<< ResultData(_("MyClass"), _(""))
<< ResultData(_("MyClass"), _("<anonymous namespace>"))
);
// Check Functions
......@@ -237,8 +249,8 @@ void CppToolsPlugin::test_builtinsymbolsearcher_data()
<< ResultData(_("function2(bool, int)"), _("MyClass"))
<< ResultData(_("myFunction(bool, int)"), _("MyNamespace"))
<< ResultData(_("function2(bool, int)"), _("MyNamespace::MyClass"))
<< ResultData(_("myFunction(bool, int)"), _(""))
<< ResultData(_("function2(bool, int)"), _("MyClass"))
<< ResultData(_("myFunction(bool, int)"), _("<anonymous namespace>"))
<< ResultData(_("function2(bool, int)"), _("<anonymous namespace>::MyClass"))
);
// Check Enums
......@@ -253,7 +265,7 @@ void CppToolsPlugin::test_builtinsymbolsearcher_data()
<< (ResultDataList()
<< ResultData(_("MyEnum"), _(""))
<< ResultData(_("MyEnum"), _("MyNamespace"))
<< ResultData(_("MyEnum"), _(""))
<< ResultData(_("MyEnum"), _("<anonymous namespace>"))
);
// Check Declarations
......@@ -268,6 +280,6 @@ void CppToolsPlugin::test_builtinsymbolsearcher_data()
<< (ResultDataList()
<< ResultData(_("int myVariable"), _(""))
<< ResultData(_("int myVariable"), _("MyNamespace"))
<< ResultData(_("int myVariable"), _(""))
<< ResultData(_("int myVariable"), _("<anonymous namespace>"))
);
}
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