2017-07-26 14:08:26 +02:00
|
|
|
/**************************************************************************
|
|
|
|
|
**
|
|
|
|
|
** Copyright (C) 2017 The Qt Company Ltd.
|
|
|
|
|
** Copyright (C) 2017 BlackBerry Limited <qt@blackberry.com>
|
|
|
|
|
** Copyright (C) 2017 Andre Hartmann <aha_1980@gmx.de>
|
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
|
|
|
|
**
|
|
|
|
|
** This file is part of Qt Creator.
|
|
|
|
|
**
|
|
|
|
|
** Commercial License Usage
|
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
|
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
|
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
|
|
|
|
**
|
|
|
|
|
** GNU General Public License Usage
|
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
|
|
|
|
**
|
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
2017-10-30 20:50:11 +02:00
|
|
|
#include "fuzzymatcher.h"
|
2017-07-26 14:08:26 +02:00
|
|
|
|
2017-07-27 06:44:35 +02:00
|
|
|
#include <QRegularExpression>
|
2017-07-26 14:08:26 +02:00
|
|
|
#include <QString>
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-30 20:50:11 +02:00
|
|
|
* \brief Creates a regexp that represents a specified wildcard and camel hump pattern,
|
2017-07-26 14:08:26 +02:00
|
|
|
* underscores are not included.
|
|
|
|
|
*
|
|
|
|
|
* \param pattern the camel hump pattern
|
2017-10-30 20:50:11 +02:00
|
|
|
* \param caseSensitivity case sensitivity used for camel hump matching
|
2017-07-26 14:08:26 +02:00
|
|
|
* \return the regexp
|
|
|
|
|
*/
|
2017-10-30 20:50:11 +02:00
|
|
|
QRegularExpression FuzzyMatcher::createRegExp(
|
|
|
|
|
const QString &pattern, FuzzyMatcher::CaseSensitivity caseSensitivity)
|
2017-07-26 14:08:26 +02:00
|
|
|
{
|
|
|
|
|
if (pattern.isEmpty())
|
2017-07-27 06:44:35 +02:00
|
|
|
return QRegularExpression();
|
2017-07-26 14:08:26 +02:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This code builds a regular expression in order to more intelligently match
|
|
|
|
|
* camel-case and underscore names.
|
|
|
|
|
*
|
|
|
|
|
* For any but the first letter, the following replacements are made:
|
|
|
|
|
* A => [a-z0-9_]*A
|
|
|
|
|
* a => (?:[a-zA-Z0-9]*_)?a
|
|
|
|
|
*
|
|
|
|
|
* That means any sequence of lower-case or underscore characters can preceed an
|
|
|
|
|
* upper-case character. And any sequence of lower-case or upper case characters -
|
|
|
|
|
* followed by an underscore can preceed a lower-case character.
|
|
|
|
|
*
|
|
|
|
|
* Examples: (case sensitive mode)
|
|
|
|
|
* gAC matches getActionController
|
|
|
|
|
* gac matches get_action_controller
|
|
|
|
|
*
|
|
|
|
|
* It also implements the fully and first-letter-only case sensitivity.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
QString keyRegExp;
|
2017-10-02 23:50:10 +03:00
|
|
|
QString plainRegExp;
|
2017-07-26 14:08:26 +02:00
|
|
|
bool first = true;
|
2017-07-27 06:44:35 +02:00
|
|
|
const QChar asterisk = '*';
|
|
|
|
|
const QChar question = '?';
|
2017-09-10 22:05:11 +03:00
|
|
|
const QLatin1String uppercaseWordFirst("(?<=\\b|[a-z0-9_])");
|
|
|
|
|
const QLatin1String lowercaseWordFirst("(?<=\\b|[A-Z0-9_])");
|
2017-07-26 14:08:26 +02:00
|
|
|
const QLatin1String uppercaseWordContinuation("[a-z0-9_]*");
|
|
|
|
|
const QLatin1String lowercaseWordContinuation("(?:[a-zA-Z0-9]*_)?");
|
2017-09-09 22:15:00 +02:00
|
|
|
const QLatin1String upperSnakeWordContinuation("[A-Z0-9]*_");
|
2017-09-20 21:55:39 +02:00
|
|
|
keyRegExp += "(?:";
|
2017-07-27 06:44:35 +02:00
|
|
|
for (const QChar &c : pattern) {
|
2017-07-26 14:08:26 +02:00
|
|
|
if (!c.isLetter()) {
|
2017-10-02 23:50:10 +03:00
|
|
|
if (c == question) {
|
2017-07-27 06:44:35 +02:00
|
|
|
keyRegExp += '.';
|
2017-10-02 23:50:10 +03:00
|
|
|
plainRegExp += '.';
|
|
|
|
|
} else if (c == asterisk) {
|
2017-07-27 06:44:35 +02:00
|
|
|
keyRegExp += ".*";
|
2017-10-02 23:50:10 +03:00
|
|
|
plainRegExp += ".*";
|
|
|
|
|
} else {
|
|
|
|
|
const QString escaped = QRegularExpression::escape(c);
|
|
|
|
|
keyRegExp += '(' + escaped + ')';
|
|
|
|
|
plainRegExp += escaped;
|
|
|
|
|
}
|
2017-07-26 14:08:26 +02:00
|
|
|
} else if (caseSensitivity == CaseSensitivity::CaseInsensitive ||
|
|
|
|
|
(caseSensitivity == CaseSensitivity::FirstLetterCaseSensitive && !first)) {
|
|
|
|
|
|
2017-10-02 23:50:10 +03:00
|
|
|
const QString upper = QRegularExpression::escape(c.toUpper());
|
|
|
|
|
const QString lower = QRegularExpression::escape(c.toLower());
|
2017-07-27 06:44:35 +02:00
|
|
|
keyRegExp += "(?:";
|
2017-09-10 22:05:11 +03:00
|
|
|
keyRegExp += first ? uppercaseWordFirst : uppercaseWordContinuation;
|
2017-10-02 23:50:10 +03:00
|
|
|
keyRegExp += '(' + upper + ')';
|
2017-07-24 20:55:47 +02:00
|
|
|
if (first) {
|
2017-10-02 23:50:10 +03:00
|
|
|
keyRegExp += '|' + lowercaseWordFirst + '(' + lower + ')';
|
2017-07-24 20:55:47 +02:00
|
|
|
} else {
|
2017-10-02 23:50:10 +03:00
|
|
|
keyRegExp += '|' + lowercaseWordContinuation + '(' + lower + ')';
|
|
|
|
|
keyRegExp += '|' + upperSnakeWordContinuation + '(' + upper + ')';
|
2017-07-24 20:55:47 +02:00
|
|
|
}
|
2017-07-27 06:44:35 +02:00
|
|
|
keyRegExp += ')';
|
2017-10-02 23:50:10 +03:00
|
|
|
plainRegExp += '[' + upper + lower + ']';
|
2017-07-26 14:08:26 +02:00
|
|
|
} else {
|
|
|
|
|
if (!first) {
|
|
|
|
|
if (c.isUpper())
|
|
|
|
|
keyRegExp += uppercaseWordContinuation;
|
|
|
|
|
else
|
|
|
|
|
keyRegExp += lowercaseWordContinuation;
|
|
|
|
|
}
|
2017-10-02 23:50:10 +03:00
|
|
|
const QString escaped = QRegularExpression::escape(c);
|
|
|
|
|
keyRegExp += escaped;
|
|
|
|
|
plainRegExp += escaped;
|
2017-07-26 14:08:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
first = false;
|
|
|
|
|
}
|
2017-10-28 20:32:21 +03:00
|
|
|
keyRegExp += ')';
|
2017-09-20 21:55:39 +02:00
|
|
|
|
2017-10-28 20:32:21 +03:00
|
|
|
return QRegularExpression('(' + plainRegExp + ")|" + keyRegExp);
|
2017-07-26 14:08:26 +02:00
|
|
|
}
|
2017-07-24 20:55:47 +02:00
|
|
|
|
|
|
|
|
/*!
|
|
|
|
|
* \brief Returns a list of matched character positions and their matched lengths for the
|
|
|
|
|
* given regular expression \a match.
|
|
|
|
|
*
|
|
|
|
|
* The list is minimized by combining adjacent highlighting positions to a single position.
|
|
|
|
|
*/
|
2017-10-30 20:50:11 +02:00
|
|
|
FuzzyMatcher::HighlightingPositions FuzzyMatcher::highlightingPositions(
|
2017-07-24 20:55:47 +02:00
|
|
|
const QRegularExpressionMatch &match)
|
|
|
|
|
{
|
|
|
|
|
HighlightingPositions result;
|
|
|
|
|
|
|
|
|
|
for (int i = 1, size = match.capturedTexts().size(); i < size; ++i) {
|
|
|
|
|
// skip unused positions, they can appear because upper- and lowercase
|
|
|
|
|
// checks for one character are done using two capture groups
|
|
|
|
|
if (match.capturedStart(i) < 0)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
// check for possible highlighting continuation to keep the list minimal
|
|
|
|
|
if (!result.starts.isEmpty()
|
|
|
|
|
&& (result.starts.last() + result.lengths.last() == match.capturedStart(i))) {
|
|
|
|
|
result.lengths.last() += match.capturedLength(i);
|
|
|
|
|
} else {
|
|
|
|
|
// no continuation, append as different chunk
|
|
|
|
|
result.starts.append(match.capturedStart(i));
|
|
|
|
|
result.lengths.append(match.capturedLength(i));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|