forked from qt-creator/qt-creator
FuzzyMatcher: Improve wildcard matching
Make it equivalent to current wildcard implementation, and replace the current one with FuzzyMatcher. Change-Id: If83a4a8dfc7c8930bd92b49972647a519886a831 Reviewed-by: André Hartmann <aha_1980@gmx.de>
This commit is contained in:
committed by
Orgad Shaneh
parent
357caf3b44
commit
4300041d24
@@ -78,10 +78,10 @@ QRegularExpression FuzzyMatcher::createRegExp(
|
||||
if (!c.isLetter()) {
|
||||
if (c == question) {
|
||||
keyRegExp += '.';
|
||||
plainRegExp += '.';
|
||||
plainRegExp += ").(";
|
||||
} else if (c == asterisk) {
|
||||
keyRegExp += ".*";
|
||||
plainRegExp += ".*";
|
||||
plainRegExp += ").*(";
|
||||
} else {
|
||||
const QString escaped = QRegularExpression::escape(c);
|
||||
keyRegExp += '(' + escaped + ')';
|
||||
|
||||
@@ -194,38 +194,9 @@ Qt::CaseSensitivity ILocatorFilter::caseSensitivity(const QString &str)
|
||||
return str == str.toLower() ? Qt::CaseInsensitive : Qt::CaseSensitive;
|
||||
}
|
||||
|
||||
/*!
|
||||
Returns whether the search term \a str contains wildcard characters.
|
||||
Can be used for choosing an optimal matching strategy.
|
||||
*/
|
||||
bool ILocatorFilter::containsWildcard(const QString &str)
|
||||
{
|
||||
return str.contains(QLatin1Char('*')) || str.contains(QLatin1Char('?'));
|
||||
}
|
||||
|
||||
/*!
|
||||
* \brief Returns a simple regular expression to search for \a text.
|
||||
*
|
||||
* \a text may contain the simple '?' and '*' wildcards known from the shell.
|
||||
* '?' matches exactly one character, '*' matches a number of characters
|
||||
* (including none).
|
||||
*
|
||||
* The regular expression contains capture groups to allow highlighting
|
||||
* matched characters after a match.
|
||||
*/
|
||||
static QRegularExpression createWildcardRegExp(const QString &text)
|
||||
{
|
||||
QString pattern = '(' + text + ')';
|
||||
pattern.replace('?', ").(");
|
||||
pattern.replace('*', ").*(");
|
||||
pattern.remove("()");
|
||||
return QRegularExpression(pattern, QRegularExpression::CaseInsensitiveOption);
|
||||
}
|
||||
|
||||
QRegularExpression ILocatorFilter::createRegExp(const QString &text)
|
||||
{
|
||||
return containsWildcard(text) ? createWildcardRegExp(text)
|
||||
: FuzzyMatcher::createRegExp(text);
|
||||
return FuzzyMatcher::createRegExp(text);
|
||||
}
|
||||
|
||||
LocatorFilterEntry::HighlightInfo ILocatorFilter::highlightInfo(
|
||||
|
||||
@@ -143,7 +143,6 @@ public:
|
||||
bool isEnabled() const;
|
||||
|
||||
static Qt::CaseSensitivity caseSensitivity(const QString &str);
|
||||
static bool containsWildcard(const QString &str);
|
||||
static QRegularExpression createRegExp(const QString &text);
|
||||
LocatorFilterEntry::HighlightInfo highlightInfo(const QRegularExpressionMatch &match,
|
||||
LocatorFilterEntry::HighlightInfo::DataType dataType = LocatorFilterEntry::HighlightInfo::DisplayName);
|
||||
|
||||
@@ -141,9 +141,9 @@ void tst_FuzzyMatcher::highlighting_data()
|
||||
QTest::newRow("numbers") << "4" << "TestJust4Fun"
|
||||
<< MatchStart{8} << MatchLength{1};
|
||||
QTest::newRow("wildcard-asterisk") << "Lo*Hu" << "VeryLongCamelHump"
|
||||
<< MatchStart{4} << MatchLength{11};
|
||||
<< MatchStart{4, 13} << MatchLength{2, 2};
|
||||
QTest::newRow("wildcard-question") << "Lo?g" << "VeryLongCamelHump"
|
||||
<< MatchStart{4} << MatchLength{4};
|
||||
<< MatchStart{4, 7} << MatchLength{2, 1};
|
||||
QTest::newRow("middle-no-hump") << "window" << "mainwindow.cpp"
|
||||
<< MatchStart{4} << MatchLength{6};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user