forked from qt-creator/qt-creator
Locator: Add highlighting of the search text
Change-Id: Ia166e9667076e46770a754b626ceb28080139e79 Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
@@ -62,18 +62,16 @@ CppCurrentDocumentFilter::CppCurrentDocumentFilter(CppTools::CppModelManager *ma
|
||||
}
|
||||
|
||||
QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor(
|
||||
QFutureInterface<Core::LocatorFilterEntry> &future, const QString & origEntry)
|
||||
QFutureInterface<Core::LocatorFilterEntry> &future, const QString & entry)
|
||||
{
|
||||
QString entry = trimWildcards(origEntry);
|
||||
QList<Core::LocatorFilterEntry> goodEntries;
|
||||
QList<Core::LocatorFilterEntry> betterEntries;
|
||||
QStringMatcher matcher(entry, Qt::CaseInsensitive);
|
||||
const QChar asterisk = QLatin1Char('*');
|
||||
QRegExp regexp(asterisk + entry + asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
|
||||
const Qt::CaseSensitivity cs = caseSensitivity(entry);
|
||||
QStringMatcher matcher(entry, cs);
|
||||
QRegExp regexp(entry, cs, QRegExp::Wildcard);
|
||||
if (!regexp.isValid())
|
||||
return goodEntries;
|
||||
bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?')));
|
||||
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
|
||||
bool hasWildcard = containsWildcard(entry);
|
||||
|
||||
foreach (IndexItem::Ptr info, itemsOfCurrentDocument()) {
|
||||
if (future.isCanceled())
|
||||
@@ -85,20 +83,30 @@ QList<Core::LocatorFilterEntry> CppCurrentDocumentFilter::matchesFor(
|
||||
else if (info->type() == IndexItem::Function)
|
||||
matchString += info->symbolType();
|
||||
|
||||
if ((hasWildcard && regexp.exactMatch(matchString))
|
||||
|| (!hasWildcard && matcher.indexIn(matchString) != -1))
|
||||
{
|
||||
int index = hasWildcard ? regexp.indexIn(matchString) : matcher.indexIn(matchString);
|
||||
if (index >= 0) {
|
||||
const bool betterMatch = index == 0;
|
||||
QVariant id = qVariantFromValue(info);
|
||||
QString name = matchString;
|
||||
QString extraInfo = info->symbolScope();
|
||||
if (info->type() == IndexItem::Function) {
|
||||
if (info->unqualifiedNameAndScope(matchString, &name, &extraInfo))
|
||||
if (info->unqualifiedNameAndScope(matchString, &name, &extraInfo)) {
|
||||
name += info->symbolType();
|
||||
index = hasWildcard ? regexp.indexIn(name) : matcher.indexIn(name);
|
||||
}
|
||||
}
|
||||
|
||||
Core::LocatorFilterEntry filterEntry(this, name, id, info->icon());
|
||||
filterEntry.extraInfo = extraInfo;
|
||||
|
||||
if (matchString.startsWith(entry, caseSensitivityForPrefix))
|
||||
if (index < 0) {
|
||||
index = hasWildcard ? regexp.indexIn(extraInfo) : matcher.indexIn(extraInfo);
|
||||
filterEntry.highlightInfo.dataType = Core::LocatorFilterEntry::HighlightInfo::ExtraInfo;
|
||||
}
|
||||
if (index >= 0) {
|
||||
filterEntry.highlightInfo.startIndex = index;
|
||||
filterEntry.highlightInfo.length = hasWildcard ? regexp.matchedLength() : entry.length();
|
||||
}
|
||||
if (betterMatch)
|
||||
betterEntries.append(filterEntry);
|
||||
else
|
||||
goodEntries.append(filterEntry);
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "cppmodelmanager.h"
|
||||
|
||||
#include <coreplugin/editormanager/editormanager.h>
|
||||
#include <utils/algorithm.h>
|
||||
|
||||
#include <QRegExp>
|
||||
#include <QStringMatcher>
|
||||
@@ -66,26 +67,18 @@ void CppLocatorFilter::refresh(QFutureInterface<void> &future)
|
||||
Q_UNUSED(future)
|
||||
}
|
||||
|
||||
static bool compareLexigraphically(const Core::LocatorFilterEntry &a,
|
||||
const Core::LocatorFilterEntry &b)
|
||||
{
|
||||
return a.displayName < b.displayName;
|
||||
}
|
||||
|
||||
QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
|
||||
QFutureInterface<Core::LocatorFilterEntry> &future, const QString &origEntry)
|
||||
QFutureInterface<Core::LocatorFilterEntry> &future, const QString &entry)
|
||||
{
|
||||
QString entry = trimWildcards(origEntry);
|
||||
QList<Core::LocatorFilterEntry> goodEntries;
|
||||
QList<Core::LocatorFilterEntry> betterEntries;
|
||||
const QChar asterisk = QLatin1Char('*');
|
||||
QStringMatcher matcher(entry, Qt::CaseInsensitive);
|
||||
QRegExp regexp(asterisk + entry+ asterisk, Qt::CaseInsensitive, QRegExp::Wildcard);
|
||||
const Qt::CaseSensitivity cs = caseSensitivity(entry);
|
||||
QStringMatcher matcher(entry, cs);
|
||||
QRegExp regexp(entry, cs, QRegExp::Wildcard);
|
||||
if (!regexp.isValid())
|
||||
return goodEntries;
|
||||
bool hasWildcard = (entry.contains(asterisk) || entry.contains(QLatin1Char('?')));
|
||||
bool hasWildcard = containsWildcard(entry);
|
||||
bool hasColonColon = entry.contains(QLatin1String("::"));
|
||||
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
|
||||
const IndexItem::ItemType wanted = matchTypes();
|
||||
|
||||
m_data->filterAllFiles([&](const IndexItem::Ptr &info) -> IndexItem::VisitorResult {
|
||||
@@ -93,10 +86,20 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
|
||||
return IndexItem::Break;
|
||||
if (info->type() & wanted) {
|
||||
const QString matchString = hasColonColon ? info->scopedSymbolName() : info->symbolName();
|
||||
if ((hasWildcard && regexp.exactMatch(matchString)) ||
|
||||
(!hasWildcard && matcher.indexIn(matchString) != -1)) {
|
||||
const Core::LocatorFilterEntry filterEntry = filterEntryFromIndexItem(info);
|
||||
if (matchString.startsWith(entry, caseSensitivityForPrefix))
|
||||
int index = hasWildcard ? regexp.indexIn(matchString) : matcher.indexIn(matchString);
|
||||
if (index >= 0) {
|
||||
const bool betterMatch = index == 0;
|
||||
Core::LocatorFilterEntry filterEntry = filterEntryFromIndexItem(info);
|
||||
|
||||
if (matchString != filterEntry.displayName) {
|
||||
index = hasWildcard ? regexp.indexIn(filterEntry.displayName)
|
||||
: matcher.indexIn(filterEntry.displayName);
|
||||
}
|
||||
|
||||
if (index >= 0)
|
||||
filterEntry.highlightInfo = {index, (hasWildcard ? regexp.matchedLength() : entry.length())};
|
||||
|
||||
if (betterMatch)
|
||||
betterEntries.append(filterEntry);
|
||||
else
|
||||
goodEntries.append(filterEntry);
|
||||
@@ -110,9 +113,9 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
|
||||
});
|
||||
|
||||
if (goodEntries.size() < 1000)
|
||||
std::stable_sort(goodEntries.begin(), goodEntries.end(), compareLexigraphically);
|
||||
Utils::sort(goodEntries, Core::LocatorFilterEntry::compareLexigraphically);
|
||||
if (betterEntries.size() < 1000)
|
||||
std::stable_sort(betterEntries.begin(), betterEntries.end(), compareLexigraphically);
|
||||
Utils::sort(betterEntries, Core::LocatorFilterEntry::compareLexigraphically);
|
||||
|
||||
betterEntries += goodEntries;
|
||||
return betterEntries;
|
||||
|
||||
Reference in New Issue
Block a user