Extract CamelHumpMatcher for use in the C++/QML locator filters

Started-by: David Kaspar <dkaspar@blackberry.com>
Change-Id: I928ccdc4084f8d07826746c771e7fca6994f95ab
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Andre Hartmann
2017-07-26 14:08:26 +02:00
committed by André Hartmann
parent 7729195d19
commit 3480579943
10 changed files with 268 additions and 55 deletions

View File

@@ -0,0 +1,106 @@
/**************************************************************************
**
** 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.
**
****************************************************************************/
#include "camelhumpmatcher.h"
#include <QRegExp>
#include <QString>
/**
* \brief Creates a regexp that represents a specified camel hump pattern,
* underscores are not included.
*
* \param pattern the camel hump pattern
* \param caseSensitivity case sensitivity used for camel hump matching;
* does not affect wildcard style matching
* \return the regexp
*/
QRegExp CamelHumpMatcher::createCamelHumpRegExp(const QString &pattern,
CamelHumpMatcher::CaseSensitivity caseSensitivity)
{
if (pattern.isEmpty())
return QRegExp();
/*
* 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;
bool first = true;
const QChar asterisk = QLatin1Char('*');
const QChar question = QLatin1Char('?');
const QLatin1String uppercaseWordContinuation("[a-z0-9_]*");
const QLatin1String lowercaseWordContinuation("(?:[a-zA-Z0-9]*_)?");
foreach (const QChar &c, pattern) {
if (!c.isLetter()) {
if (c == question)
keyRegExp += QLatin1Char('.');
else if (c == asterisk)
keyRegExp += QLatin1String(".*");
else
keyRegExp += QRegExp::escape(c);
} else if (caseSensitivity == CaseSensitivity::CaseInsensitive ||
(caseSensitivity == CaseSensitivity::FirstLetterCaseSensitive && !first)) {
keyRegExp += QLatin1String("(?:");
if (!first)
keyRegExp += uppercaseWordContinuation;
keyRegExp += QRegExp::escape(c.toUpper());
keyRegExp += QLatin1Char('|');
if (!first)
keyRegExp += lowercaseWordContinuation;
keyRegExp += QRegExp::escape(c.toLower());
keyRegExp += QLatin1Char(')');
} else {
if (!first) {
if (c.isUpper())
keyRegExp += uppercaseWordContinuation;
else
keyRegExp += lowercaseWordContinuation;
}
keyRegExp += QRegExp::escape(c);
}
first = false;
}
return QRegExp(keyRegExp);
}

View File

@@ -0,0 +1,48 @@
/**************************************************************************
**
** 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.
**
****************************************************************************/
#pragma once
#include "utils_global.h"
QT_BEGIN_NAMESPACE
class QRegExp;
class QString;
QT_END_NAMESPACE
class QTCREATOR_UTILS_EXPORT CamelHumpMatcher
{
public:
enum class CaseSensitivity {
CaseInsensitive,
CaseSensitive,
FirstLetterCaseSensitive
};
static QRegExp createCamelHumpRegExp(const QString &pattern,
CaseSensitivity caseSensitivity = CaseSensitivity::CaseInsensitive);
};

View File

@@ -114,7 +114,8 @@ SOURCES += $$PWD/environment.cpp \
$$PWD/runextensions.cpp \
$$PWD/utilsicons.cpp \
$$PWD/guard.cpp \
$$PWD/highlightingitemdelegate.cpp
$$PWD/highlightingitemdelegate.cpp \
$$PWD/camelhumpmatcher.cpp
win32:SOURCES += $$PWD/consoleprocess_win.cpp
else:SOURCES += $$PWD/consoleprocess_unix.cpp
@@ -242,7 +243,8 @@ HEADERS += \
$$PWD/optional.h \
$$PWD/../3rdparty/optional/optional.hpp \
$$PWD/qtcfallthrough.h \
$$PWD/highlightingitemdelegate.cpp
$$PWD/highlightingitemdelegate.cpp \
$$PWD/camelhumpmatcher.h
FORMS += $$PWD/filewizardpage.ui \
$$PWD/projectintropage.ui \

View File

@@ -52,6 +52,8 @@ Project {
"bracematcher.h",
"buildablehelperlibrary.cpp",
"buildablehelperlibrary.h",
"camelhumpmatcher.cpp",
"camelhumpmatcher.h",
"categorysortfiltermodel.cpp",
"categorysortfiltermodel.h",
"changeset.cpp",

View File

@@ -255,58 +255,9 @@ void GenericProposalModel::filter(const QString &prefix)
if (prefix.isEmpty())
return;
/*
* 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.
*/
const CaseSensitivity caseSensitivity =
TextEditorSettings::completionSettings().m_caseSensitivity;
QString keyRegExp;
keyRegExp += QLatin1Char('^');
bool first = true;
const QLatin1String uppercaseWordContinuation("[a-z0-9_]*");
const QLatin1String lowercaseWordContinuation("(?:[a-zA-Z0-9]*_)?");
foreach (const QChar &c, prefix) {
if (caseSensitivity == CaseInsensitive ||
(caseSensitivity == FirstLetterCaseSensitive && !first)) {
keyRegExp += QLatin1String("(?:");
if (!first)
keyRegExp += uppercaseWordContinuation;
keyRegExp += QRegExp::escape(c.toUpper());
keyRegExp += QLatin1Char('|');
if (!first)
keyRegExp += lowercaseWordContinuation;
keyRegExp += QRegExp::escape(c.toLower());
keyRegExp += QLatin1Char(')');
} else {
if (!first) {
if (c.isUpper())
keyRegExp += uppercaseWordContinuation;
else
keyRegExp += lowercaseWordContinuation;
}
keyRegExp += QRegExp::escape(c);
}
first = false;
}
QRegExp regExp(keyRegExp);
const CamelHumpMatcher::CaseSensitivity caseSensitivity =
convertCaseSensitivity(TextEditorSettings::completionSettings().m_caseSensitivity);
const QRegExp regExp = CamelHumpMatcher::createCamelHumpRegExp(prefix, caseSensitivity);
m_currentItems.clear();
const QString lowerPrefix = prefix.toLower();
@@ -328,6 +279,19 @@ void GenericProposalModel::filter(const QString &prefix)
}
}
CamelHumpMatcher::CaseSensitivity
GenericProposalModel::convertCaseSensitivity(TextEditor::CaseSensitivity textEditorCaseSensitivity)
{
switch (textEditorCaseSensitivity) {
case TextEditor::CaseSensitive:
return CamelHumpMatcher::CaseSensitivity::CaseSensitive;
case TextEditor::FirstLetterCaseSensitive:
return CamelHumpMatcher::CaseSensitivity::FirstLetterCaseSensitive;
default:
return CamelHumpMatcher::CaseSensitivity::CaseInsensitive;
}
}
bool GenericProposalModel::isSortable(const QString &prefix) const
{
Q_UNUSED(prefix);

View File

@@ -28,8 +28,9 @@
#include "iassistproposalmodel.h"
#include "assistenums.h"
#include <texteditor/completionsettings.h>
#include <texteditor/texteditor_global.h>
#include <utils/camelhumpmatcher.h>
#include <QHash>
#include <QList>
@@ -71,6 +72,8 @@ public:
bool isPrefiltered(const QString &prefix) const;
void setPrefilterPrefix(const QString &prefix);
CamelHumpMatcher::CaseSensitivity convertCaseSensitivity(TextEditor::CaseSensitivity textEditorCaseSensitivity);
protected:
QList<AssistProposalItemInterface *> m_currentItems;

View File

@@ -0,0 +1,6 @@
QTC_LIB_DEPENDS += utils
include(../../qttest.pri)
DEFINES += QTCREATOR_UTILS_LIB
SOURCES += tst_camelhumpmatcher.cpp

View File

@@ -0,0 +1,8 @@
import qbs
import "../../autotest.qbs" as Autotest
Autotest {
name: "CamelHumpMatcher autotest"
Depends { name: "Utils" }
files: "tst_camelhumpmatcher.cpp"
}

View File

@@ -0,0 +1,73 @@
/**************************************************************************
**
** 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.
**
****************************************************************************/
#include <utils/camelhumpmatcher.h>
#include <QtTest>
class tst_CamelHumpMatcher : public QObject
{
Q_OBJECT
private slots:
void camelHumpMatcher();
void camelHumpMatcher_data();
};
void tst_CamelHumpMatcher::camelHumpMatcher()
{
QFETCH(QString, pattern);
QFETCH(QString, candidate);
QFETCH(int, expectedIndex);
QCOMPARE(CamelHumpMatcher::createCamelHumpRegExp(pattern).indexIn(candidate), expectedIndex);
}
void tst_CamelHumpMatcher::camelHumpMatcher_data()
{
QTest::addColumn<QString>("pattern");
QTest::addColumn<QString>("candidate");
QTest::addColumn<int>("expectedIndex");
QTest::newRow("underscore") << "vl" << "very_long_camel_hump" << 0;
QTest::newRow("exact") << "VeryLongCamelHump" << "VeryLongCamelHump" << 0;
QTest::newRow("prefix-segments") << "velo" << "very_long_Camel_hump" << 0;
QTest::newRow("humps") << "vlc" << "VeryLongCamelHump" << 0;
QTest::newRow("case-insensitive-humps") << "VlCh" << "VeryLongCamelHump" << 0;
QTest::newRow("incorrect-hump") << "vxc" << "VeryLongCamelHump" << -1;
QTest::newRow("skipped-hump") << "vc" << "VeryLongCamelHump" << -1;
QTest::newRow("middle-humps") << "lc" << "VeryLongCamelHump" << 4;
QTest::newRow("incorrect-hump") << "lyn" << "VeryLongCamelHump" << -1;
QTest::newRow("humps") << "VL" << "VeryLongCamelHump" << 0;
QTest::newRow("skipped-humps-upper") << "VH" << "VeryLongCamelHump" << -1;
QTest::newRow("question-wildcard") << "Lon?Ca" << "VeryLongCamelHump" << 4;
QTest::newRow("unmatched-question-wildcard") << "Long?Ca" << "VeryLongCamelHump" << -1;
QTest::newRow("asterix-wildcard") << "Long*Ca" << "VeryLongCamelHump" << 4;
QTest::newRow("empty-asterix-wildcard") << "Lo*Ca" << "VeryLongCamelHump" << 4;
}
QTEST_APPLESS_MAIN(tst_CamelHumpMatcher)
#include "tst_camelhumpmatcher.moc"

View File

@@ -3,6 +3,7 @@ TEMPLATE = subdirs
SUBDIRS = \
fileutils \
ansiescapecodehandler \
camelhumpmatcher \
objectpool \
stringutils \
templateengine \