forked from qt-creator/qt-creator
Locator: Extract MatchLevel and use it in several filters
... that already used index-based prioritising. There are a few more with two- or three-level priority, but these still use the old scheme with multiple lists good/better/bestEntries and converting them would not gain much. Change-Id: I21f7cfe07a3ae8183db9cb62311697d03db6e4da Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
committed by
André Hartmann
parent
847108d4f4
commit
3b41b9b24b
@@ -88,24 +88,25 @@ void BaseFileFilter::prepareSearch(const QString &entry)
|
||||
d->m_data.forceNewSearchList = false;
|
||||
}
|
||||
|
||||
static int matchLevelFor(const QRegularExpressionMatch &match, const QString &matchText)
|
||||
ILocatorFilter::MatchLevel BaseFileFilter::matchLevelFor(const QRegularExpressionMatch &match,
|
||||
const QString &matchText) const
|
||||
{
|
||||
const int consecutivePos = match.capturedStart(1);
|
||||
if (consecutivePos == 0)
|
||||
return 0;
|
||||
return MatchLevel::Best;
|
||||
if (consecutivePos > 0) {
|
||||
const QChar prevChar = matchText.at(consecutivePos - 1);
|
||||
if (prevChar == '_' || prevChar == '.')
|
||||
return 1;
|
||||
return MatchLevel::Better;
|
||||
}
|
||||
if (match.capturedStart() == 0)
|
||||
return 2;
|
||||
return 3;
|
||||
return MatchLevel::Good;
|
||||
return MatchLevel::Normal;
|
||||
}
|
||||
|
||||
QList<LocatorFilterEntry> BaseFileFilter::matchesFor(QFutureInterface<LocatorFilterEntry> &future, const QString &origEntry)
|
||||
{
|
||||
QList<LocatorFilterEntry> entries[4];
|
||||
QList<LocatorFilterEntry> entries[int(MatchLevel::Count)];
|
||||
// If search string contains spaces, treat them as wildcard '*' and search in full path
|
||||
const QString entry = QDir::fromNativeSeparators(origEntry).replace(' ', '*');
|
||||
const EditorManager::FilePathInfo fp = EditorManager::splitLineAndColumnNumber(entry);
|
||||
@@ -113,7 +114,7 @@ QList<LocatorFilterEntry> BaseFileFilter::matchesFor(QFutureInterface<LocatorFil
|
||||
const QRegularExpression regexp = createRegExp(fp.filePath);
|
||||
if (!regexp.isValid()) {
|
||||
d->m_current.clear(); // free memory
|
||||
return entries[0];
|
||||
return {};
|
||||
}
|
||||
auto containsPathSeparator = [](const QString &candidate) {
|
||||
return candidate.contains('/') || candidate.contains('*');
|
||||
@@ -151,7 +152,7 @@ QList<LocatorFilterEntry> BaseFileFilter::matchesFor(QFutureInterface<LocatorFil
|
||||
filterEntry.fileName = path.toString();
|
||||
filterEntry.extraInfo = FilePath::fromFileInfo(fi).shortNativePath();
|
||||
|
||||
const int matchLevel = matchLevelFor(match, matchText);
|
||||
const MatchLevel matchLevel = matchLevelFor(match, matchText);
|
||||
if (hasPathSeparator) {
|
||||
match = regexp.match(filterEntry.extraInfo);
|
||||
filterEntry.highlightInfo =
|
||||
@@ -160,7 +161,7 @@ QList<LocatorFilterEntry> BaseFileFilter::matchesFor(QFutureInterface<LocatorFil
|
||||
filterEntry.highlightInfo = highlightInfo(match);
|
||||
}
|
||||
|
||||
entries[matchLevel].append(filterEntry);
|
||||
entries[int(matchLevel)].append(filterEntry);
|
||||
d->m_current.previousResultPaths.append(path);
|
||||
}
|
||||
}
|
||||
@@ -180,7 +181,7 @@ QList<LocatorFilterEntry> BaseFileFilter::matchesFor(QFutureInterface<LocatorFil
|
||||
Utils::sort(entry, Core::LocatorFilterEntry::compareLexigraphically);
|
||||
}
|
||||
|
||||
return entries[0] + entries[1] + entries[2] + entries[3];
|
||||
return std::accumulate(std::begin(entries), std::end(entries), QList<LocatorFilterEntry>());
|
||||
}
|
||||
|
||||
void BaseFileFilter::accept(LocatorFilterEntry selection,
|
||||
|
@@ -76,6 +76,7 @@ protected:
|
||||
QSharedPointer<Iterator> fileIterator();
|
||||
|
||||
private:
|
||||
MatchLevel matchLevelFor(const QRegularExpressionMatch &match, const QString &matchText) const;
|
||||
void updatePreviousResultData();
|
||||
|
||||
Internal::BaseFileFilterPrivate *d = nullptr;
|
||||
|
@@ -40,15 +40,8 @@
|
||||
using namespace Core;
|
||||
using namespace Core::Internal;
|
||||
|
||||
enum class MatchLevel {
|
||||
Best = 0,
|
||||
Better,
|
||||
Good,
|
||||
Normal,
|
||||
Number
|
||||
};
|
||||
|
||||
static MatchLevel matchLevelFor(const QRegularExpressionMatch &match, const QString &matchText)
|
||||
ILocatorFilter::MatchLevel FileSystemFilter::matchLevelFor(const QRegularExpressionMatch &match,
|
||||
const QString &matchText) const
|
||||
{
|
||||
const int consecutivePos = match.capturedStart(1);
|
||||
if (consecutivePos == 0)
|
||||
@@ -80,7 +73,7 @@ void FileSystemFilter::prepareSearch(const QString &entry)
|
||||
QList<LocatorFilterEntry> FileSystemFilter::matchesFor(QFutureInterface<LocatorFilterEntry> &future,
|
||||
const QString &entry)
|
||||
{
|
||||
QList<LocatorFilterEntry> entries[int(MatchLevel::Number)];
|
||||
QList<LocatorFilterEntry> entries[int(MatchLevel::Count)];
|
||||
const QFileInfo entryInfo(entry);
|
||||
const QString entryFileName = entryInfo.fileName();
|
||||
QString directory = entryInfo.path();
|
||||
|
@@ -53,6 +53,8 @@ public:
|
||||
void refresh(QFutureInterface<void> &) override {}
|
||||
|
||||
private:
|
||||
MatchLevel matchLevelFor(const QRegularExpressionMatch &match, const QString &matchText) const;
|
||||
|
||||
bool m_includeHidden = true;
|
||||
QString m_currentDocumentDirectory;
|
||||
};
|
||||
|
@@ -105,6 +105,14 @@ class CORE_EXPORT ILocatorFilter : public QObject
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
enum class MatchLevel {
|
||||
Best = 0,
|
||||
Better,
|
||||
Good,
|
||||
Normal,
|
||||
Count
|
||||
};
|
||||
|
||||
enum Priority {Highest = 0, High = 1, Medium = 2, Low = 3};
|
||||
|
||||
ILocatorFilter(QObject *parent = nullptr);
|
||||
|
@@ -69,8 +69,7 @@ void CppLocatorFilter::refresh(QFutureInterface<void> &future)
|
||||
QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
|
||||
QFutureInterface<Core::LocatorFilterEntry> &future, const QString &entry)
|
||||
{
|
||||
enum { Best = 0, Better, Good, Normal, EntryNumber };
|
||||
QList<Core::LocatorFilterEntry> entries[EntryNumber];
|
||||
QList<Core::LocatorFilterEntry> entries[int(MatchLevel::Count)];
|
||||
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
|
||||
const IndexItem::ItemType wanted = matchTypes();
|
||||
|
||||
@@ -113,13 +112,13 @@ QList<Core::LocatorFilterEntry> CppLocatorFilter::matchesFor(
|
||||
}
|
||||
|
||||
if (matchInParameterList)
|
||||
entries[Normal].append(filterEntry);
|
||||
entries[int(MatchLevel::Normal)].append(filterEntry);
|
||||
else if (filterEntry.displayName.startsWith(entry, caseSensitivityForPrefix))
|
||||
entries[Best].append(filterEntry);
|
||||
entries[int(MatchLevel::Best)].append(filterEntry);
|
||||
else if (filterEntry.displayName.contains(entry, caseSensitivityForPrefix))
|
||||
entries[Better].append(filterEntry);
|
||||
entries[int(MatchLevel::Better)].append(filterEntry);
|
||||
else
|
||||
entries[Good].append(filterEntry);
|
||||
entries[int(MatchLevel::Good)].append(filterEntry);
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -58,8 +58,7 @@ QList<Core::LocatorFilterEntry> FunctionFilter::matchesFor(
|
||||
QFutureInterface<Core::LocatorFilterEntry> &future,
|
||||
const QString &entry)
|
||||
{
|
||||
enum { Best = 0, Better, Good, EntryNumber };
|
||||
QList<Core::LocatorFilterEntry> entries[EntryNumber];
|
||||
QList<Core::LocatorFilterEntry> entries[int(MatchLevel::Count)];
|
||||
const Qt::CaseSensitivity caseSensitivityForPrefix = caseSensitivity(entry);
|
||||
|
||||
const QRegularExpression regexp = createRegExp(entry);
|
||||
@@ -83,11 +82,11 @@ QList<Core::LocatorFilterEntry> FunctionFilter::matchesFor(
|
||||
filterEntry.highlightInfo = highlightInfo(match);
|
||||
|
||||
if (filterEntry.displayName.startsWith(entry, caseSensitivityForPrefix))
|
||||
entries[Best].append(filterEntry);
|
||||
entries[int(MatchLevel::Best)].append(filterEntry);
|
||||
else if (filterEntry.displayName.contains(entry, caseSensitivityForPrefix))
|
||||
entries[Better].append(filterEntry);
|
||||
entries[int(MatchLevel::Better)].append(filterEntry);
|
||||
else
|
||||
entries[Good].append(filterEntry);
|
||||
entries[int(MatchLevel::Good)].append(filterEntry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user