CppTools: Fix highlighting when matching with scope

class Foo {
  void funcInside() {}
  void funcOutside();
};

void Foo::funcOutside() {}

Search for Foo::func in the locator. func was not highlighted.

Change-Id: I923bd3ef2df47c5fa030b8899d1b4df7437b1820
Reviewed-by: André Hartmann <aha_1980@gmx.de>
Reviewed-by: Nikolai Kosjar <nikolai.kosjar@qt.io>
This commit is contained in:
Orgad Shaneh
2017-10-31 08:25:12 +02:00
committed by Orgad Shaneh
parent 1a361a39ad
commit d2b8076fec
2 changed files with 33 additions and 4 deletions

View File

@@ -73,19 +73,23 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
QList<Core::LocatorFilterEntry> betterEntries;
QList<Core::LocatorFilterEntry> bestEntries;
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
bool hasColonColon = entry.contains(QLatin1String("::"));
const IndexItem::ItemType wanted = matchTypes();
const QRegularExpression regexp = createRegExp(entry);
if (!regexp.isValid())
return goodEntries;
const bool hasColonColon = entry.contains("::");
const QRegularExpression shortRegexp =
hasColonColon ? createRegExp(entry.mid(entry.lastIndexOf("::") + 2)) : regexp;
m_data->filterAllFiles([&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
if (future.isCanceled())
return IndexItem::Break;
const IndexItem::ItemType type = info->type();
if (type & wanted) {
QString matchString = hasColonColon ? info->scopedSymbolName() : info->symbolName();
const QString symbolName = info->symbolName();
QString matchString = hasColonColon ? info->scopedSymbolName() : symbolName;
int matchOffset = hasColonColon ? matchString.size() - symbolName.size() : 0;
if (type == IndexItem::Function)
matchString += info->symbolType();
QRegularExpressionMatch match = regexp.match(matchString);
@@ -94,9 +98,15 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
// Highlight the matched characters, therefore it may be necessary
// to update the match if the displayName is different from matchString
if (matchString != filterEntry.displayName)
match = regexp.match(filterEntry.displayName);
if (matchString.midRef(matchOffset) != filterEntry.displayName) {
match = shortRegexp.match(filterEntry.displayName);
matchOffset = 0;
}
filterEntry.highlightInfo = highlightInfo(match);
if (matchOffset > 0) {
for (int &start : filterEntry.highlightInfo.starts)
start -= matchOffset;
}
if (matchString.startsWith(entry, caseSensitivityForPrefix))
bestEntries.append(filterEntry);

View File

@@ -231,6 +231,25 @@ void CppToolsPlugin::test_cpplocatorfilters_CppLocatorFilter_data()
<< ResultData(_("myFunction(bool, int)"), _("MyNamespace (file1.cpp)"))
);
QTest::newRow("CppFunctionsFilter-WithClassPrefix")
<< testFile
<< cppFunctionsFilter
<< _("MyClass::func")
<< (QList<ResultData>()
<< ResultData(_("functionDefinedInClass(bool, int)"), _("MyClass (file1.cpp)"))
<< ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass (file1.cpp)"))
<< ResultData(_("functionDefinedInClass(bool, int)"),
_("MyNamespace::MyClass (file1.cpp)"))
<< ResultData(_("functionDefinedInClass(bool, int)"),
_("<anonymous namespace>::MyClass (file1.cpp)"))
<< ResultData(_("functionDefinedOutSideClass(char)"),
_("MyNamespace::MyClass (file1.cpp)"))
<< ResultData(_("functionDefinedOutSideClass(char)"),
_("<anonymous namespace>::MyClass (file1.cpp)"))
<< ResultData(_("functionDefinedOutSideClassAndNamespace(float)"),
_("MyNamespace::MyClass (file1.cpp)"))
);
QTest::newRow("CppClassesFilter")
<< testFile
<< cppClassesFilter