From adc8638de0bceafad65555e4f249e88883be01bb Mon Sep 17 00:00:00 2001 From: Andre Hartmann Date: Fri, 29 Dec 2017 07:57:01 +0100 Subject: [PATCH] BaseFileFilter: Rate full matches higher than fuzzy matches MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Searching for "qmap.h" should not have "qcore_mac_p.h" as first search result and "qmap.h" somewhere later in the list. This is only a partial solution for QTCREATORBUG-19531, as the filter might be called multiple times (for "All Included Files" and "Files in Any Project"). So the final result list may contain the best results in the middle. Task-number: QTCREATORBUG-19531 Change-Id: Ib5322824d3e0e8106c2f197169342f18923a894a Reviewed-by: André Hartmann Reviewed-by: Eike Ziller Reviewed-by: Orgad Shaneh --- .../coreplugin/locator/basefilefilter.cpp | 30 ++++++++++++------- .../coreplugin/locator/locator_test.cpp | 22 ++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/src/plugins/coreplugin/locator/basefilefilter.cpp b/src/plugins/coreplugin/locator/basefilefilter.cpp index 39f4d003347..20535286876 100644 --- a/src/plugins/coreplugin/locator/basefilefilter.cpp +++ b/src/plugins/coreplugin/locator/basefilefilter.cpp @@ -93,17 +93,31 @@ void BaseFileFilter::prepareSearch(const QString &entry) d->m_data.forceNewSearchList = false; } +static int matchLevelFor(const QRegularExpressionMatch &match, const QString &matchText) +{ + const int consecutivePos = match.capturedStart(1); + if (consecutivePos == 0) + return 0; + if (consecutivePos > 0) { + const QChar prevChar = matchText.at(consecutivePos - 1); + if (prevChar == '_' || prevChar == '.') + return 1; + } + if (match.capturedStart() == 0) + return 2; + return 3; +} + QList BaseFileFilter::matchesFor(QFutureInterface &future, const QString &origEntry) { - QList betterEntries; - QList goodEntries; + QList entries[4]; const QString entry = QDir::fromNativeSeparators(origEntry); const EditorManager::FilePathInfo fp = EditorManager::splitLineAndColumnNumber(entry); const QRegularExpression regexp = createRegExp(fp.filePath); if (!regexp.isValid()) { d->m_current.clear(); // free memory - return betterEntries; + return entries[0]; } const QChar pathSeparator('/'); const bool hasPathSeparator = fp.filePath.contains(pathSeparator); @@ -141,7 +155,7 @@ QList BaseFileFilter::matchesFor(QFutureInterface BaseFileFilter::matchesFor(QFutureInterfacem_current.previousResultPaths.append(path); d->m_current.previousResultNames.append(name); } } - betterEntries.append(goodEntries); if (canceled) { // we keep the old list of previous search results if this search was canceled // so a later search without forceNewSearchList will use that previous list instead of an @@ -169,7 +179,7 @@ QList BaseFileFilter::matchesFor(QFutureInterfacem_current.iterator.clear(); QTimer::singleShot(0, this, &BaseFileFilter::updatePreviousResultData); } - return betterEntries; + return entries[0] + entries[1] + entries[2] + entries[3]; } void BaseFileFilter::accept(LocatorFilterEntry selection, diff --git a/src/plugins/coreplugin/locator/locator_test.cpp b/src/plugins/coreplugin/locator/locator_test.cpp index 270bc1d50a6..3e26ea92b4d 100644 --- a/src/plugins/coreplugin/locator/locator_test.cpp +++ b/src/plugins/coreplugin/locator/locator_test.cpp @@ -158,4 +158,26 @@ void Core::Internal::CorePlugin::test_basefilefilter_data() << ResultData("main.cpp", testFilesShort.at(1)) << ResultData("main.cpp", testFilesShort.at(2)))) ); + + const QStringList priorityTestFiles({testDir.file("qmap.cpp"), + testDir.file("mid_qcore_mac_p.h"), + testDir.file("qcore_mac_p.h"), + testDir.file("foo_qmap.h"), + testDir.file("qmap.h"), + testDir.file("bar.h")}); + QStringList priorityTestFilesShort; + for (const QString &file : priorityTestFiles) + priorityTestFilesShort << Utils::FileUtils::shortNativePath(Utils::FileName::fromString(file)); + + QTest::newRow("BaseFileFilter-InputPriorizeFullOverFuzzy") + << priorityTestFiles + << (QList() + << ReferenceData( + "qmap.h", + (QList() + << ResultData("qmap.h", priorityTestFilesShort.at(4)) + << ResultData("foo_qmap.h", priorityTestFilesShort.at(3)) + << ResultData("qcore_mac_p.h", priorityTestFilesShort.at(2)) + << ResultData("mid_qcore_mac_p.h", priorityTestFilesShort.at(1)))) + ); }