diff --git a/src/plugins/cpptools/cpplocatorfilter.cpp b/src/plugins/cpptools/cpplocatorfilter.cpp index ba31251bf37..a2b94c2df17 100644 --- a/src/plugins/cpptools/cpplocatorfilter.cpp +++ b/src/plugins/cpptools/cpplocatorfilter.cpp @@ -73,19 +73,23 @@ QList CppLocatorFilter::matchesFor( QList betterEntries; QList 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 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); diff --git a/src/plugins/cpptools/cpplocatorfilter_test.cpp b/src/plugins/cpptools/cpplocatorfilter_test.cpp index 1b309fbe8ae..b5cc9041b1c 100644 --- a/src/plugins/cpptools/cpplocatorfilter_test.cpp +++ b/src/plugins/cpptools/cpplocatorfilter_test.cpp @@ -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(_("functionDefinedInClass(bool, int)"), _("MyClass (file1.cpp)")) + << ResultData(_("functionDefinedOutSideClass(char)"), _("MyClass (file1.cpp)")) + << ResultData(_("functionDefinedInClass(bool, int)"), + _("MyNamespace::MyClass (file1.cpp)")) + << ResultData(_("functionDefinedInClass(bool, int)"), + _("::MyClass (file1.cpp)")) + << ResultData(_("functionDefinedOutSideClass(char)"), + _("MyNamespace::MyClass (file1.cpp)")) + << ResultData(_("functionDefinedOutSideClass(char)"), + _("::MyClass (file1.cpp)")) + << ResultData(_("functionDefinedOutSideClassAndNamespace(float)"), + _("MyNamespace::MyClass (file1.cpp)")) + ); + QTest::newRow("CppClassesFilter") << testFile << cppClassesFilter