aboutsummaryrefslogtreecommitdiffstats
path: root/src/libs/utils/fuzzymatcher.cpp
blob: 7b4d02bdf5c2dc8f102415a2fcb8bb38ff7895b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (C) 2017 The Qt Company Ltd.
// Copyright (C) 2017 BlackBerry Limited <qt@blackberry.com>
// Copyright (C) 2017 Andre Hartmann <aha_1980@gmx.de>
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#include "fuzzymatcher.h"

#include <QRegularExpression>
#include <QString>

/**
 * \brief Creates a regexp that represents a specified wildcard and camel hump pattern,
 *        underscores are not included.
 *
 * \param pattern the camel hump pattern
 * \param caseSensitivity case sensitivity used for camel hump matching
 * \return the regexp
 */
QRegularExpression FuzzyMatcher::createRegExp(
        const QString &pattern, FuzzyMatcher::CaseSensitivity caseSensitivity, bool multiWord)
{
    if (pattern.isEmpty())
        return QRegularExpression();

    /*
     * 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:
     *   gAC matches getActionController (case sensitive mode)
     *   gac matches get_action_controller (case sensitive mode)
     *   gac matches Get Action Container (case insensitive multi word mode)
     *
     * It also implements the fully and first-letter-only case sensitivity.
     */

    QString keyRegExp;
    QString plainRegExp;
    bool first = true;
    const QChar asterisk = '*';
    const QChar question = '?';
    const QLatin1String uppercaseWordFirst("(?<=\\b|[a-z0-9_])");
    const QLatin1String lowercaseWordFirst("(?<=\\b|[A-Z0-9_])");
    const QLatin1String uppercaseWordContinuation("[a-z0-9_]*");
    const QLatin1String lowercaseWordContinuation("(?:[a-zA-Z0-9]*_)?");
    const QLatin1String upperSnakeWordContinuation("[A-Z0-9]*_?");

    const QLatin1String multiWordFirst("\\b");
    const QLatin1String multiWordContinuation("(?:.*?\\b)*?");

    keyRegExp += "(?:";
    for (const QChar &c : pattern) {
        if (!c.isLetterOrNumber()) {
            if (c == question) {
                keyRegExp += '.';
                plainRegExp += ").(";
            } else if (c == asterisk) {
                keyRegExp += ".*";
                plainRegExp += ").*(";
            } else if (multiWord && c == QChar::Space) {
                // ignore spaces in keyRegExp
                plainRegExp += QRegularExpression::escape(c);
            } else {
                const QString escaped = QRegularExpression::escape(c);
                keyRegExp += '(' + escaped + ')';
                plainRegExp += escaped;
            }
        } else if (caseSensitivity == CaseSensitivity::CaseInsensitive ||
            (caseSensitivity == CaseSensitivity::FirstLetterCaseSensitive && !first)) {
            const QString upper = QRegularExpression::escape(c.toUpper());
            const QString lower = QRegularExpression::escape(c.toLower());
            if (multiWord) {
                keyRegExp += first ? multiWordFirst : multiWordContinuation;
                keyRegExp += '(' + upper + '|' + lower + ')';
            } else {
                keyRegExp += "(?:";
                keyRegExp += first ? uppercaseWordFirst : uppercaseWordContinuation;
                keyRegExp += '(' + upper + ')';
                if (first) {
                    keyRegExp += '|' + lowercaseWordFirst + '(' + lower + ')';
                } else {
                    keyRegExp += '|' + lowercaseWordContinuation + '(' + lower + ')';
                    keyRegExp += '|' + upperSnakeWordContinuation + '(' + upper + ')';
                }
                keyRegExp += ')';
            }
            plainRegExp += '[' + upper + lower + ']';
        } else {
            if (!first) {
                if (multiWord)
                    keyRegExp += multiWordContinuation;
                if (c.isUpper())
                    keyRegExp += uppercaseWordContinuation;
                else
                    keyRegExp += lowercaseWordContinuation;
            } else if (multiWord) {
                keyRegExp += multiWordFirst;
            }
            const QString escaped = QRegularExpression::escape(c);
            keyRegExp += escaped;
            plainRegExp += escaped;
        }

        first = false;
    }
    keyRegExp += ')';

    return QRegularExpression('(' + plainRegExp + ")|" + keyRegExp);
}

/**
    \overload
    This overload eases the construction of a fuzzy regexp from a given
    Qt::CaseSensitivity.
 */
QRegularExpression FuzzyMatcher::createRegExp(const QString &pattern,
                                              Qt::CaseSensitivity caseSensitivity,
                                              bool multiWord)
{
    const CaseSensitivity sensitivity = (caseSensitivity == Qt::CaseSensitive)
            ? CaseSensitivity::CaseSensitive
            : CaseSensitivity::CaseInsensitive;

    return createRegExp(pattern, sensitivity, multiWord);
}

/*!
 * \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.
 */
FuzzyMatcher::HighlightingPositions FuzzyMatcher::highlightingPositions(
        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;
}