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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* ============================================================
 *
 * This file is a part of digiKam project
 * https://www.digikam.org
 *
 * Date        : 2009-11-19
 * Description : syntax highlighter for AdvancedRename utility
 *
 * SPDX-FileCopyrightText: 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
 *
 * SPDX-License-Identifier: GPL-2.0-or-later
 *
 * ============================================================ */

#include "highlighter.h"

// Qt includes

#include <QTextDocument>
#include <QRegularExpression>

// Local includes

#include "parser.h"

namespace Digikam
{

class Q_DECL_HIDDEN Highlighter::Private
{
public:

    enum PatternType
    {
        OptionPattern = 0,
        ModifierPattern,
        QuotedTextPattern,
        ParameterPattern
    };

    class Q_DECL_HIDDEN HighlightingRule
    {
    public:

        PatternType           type      = OptionPattern;
        QRegularExpression    pattern;
        QTextCharFormat       format;
    };

public:

    Private() = default;

public:

    QVector<HighlightingRule> highlightingRules;
    HighlightingRule          quotationRule;

    QTextCharFormat           optionFormat;
    QTextCharFormat           parameterFormat;
    QTextCharFormat           modifierFormat;
    QTextCharFormat           quotationFormat;
    QTextCharFormat           errorFormat;

    Parser*                   parser  = nullptr;
};

Highlighter::Highlighter(QTextDocument* const document, Parser* const _parser)
    : QSyntaxHighlighter(document),
      d                 (new Private)
{
    d->parser = _parser;
    setupHighlightingGrammar();
}

Highlighter::~Highlighter()
{
    delete d;
}

void Highlighter::highlightBlock(const QString& text)
{
    for (const Private::HighlightingRule& rule : std::as_const(d->highlightingRules))
    {
        QRegularExpression      expression(rule.pattern);
        QRegularExpressionMatch match;
        int index = text.indexOf(expression, 0, &match);

        while (index >= 0)
        {
            int length = match.capturedLength();
            setFormat(index, length, rule.format);

            switch (rule.type)
            {
                case Private::OptionPattern:
                case Private::ModifierPattern:
                {
                    // highlight parameters in options and modifiers

                    if ((expression.captureCount() > 0) && !match.captured(1).isEmpty())
                    {
                        QString fullmatched  = match.captured(0);
                        QString parameters   = match.captured(1);

                        if (parameters.startsWith(QLatin1Char(':')))
                        {
                            parameters.remove(0, 1);

                            if (!parameters.isEmpty())
                            {
                                int pindex = fullmatched.indexOf(parameters);

                                while (pindex >= 0)
                                {
                                    int plength = parameters.length();
                                    setFormat(index + pindex, plength, d->parameterFormat);
                                    pindex      = fullmatched.indexOf(parameters, pindex + plength);
                                }
                            }
                        }
                    }

                    break;
                }

                default:
                {
                    break;
                }
            }

            index = text.indexOf(expression, index + length, &match);
        }
    }

    // mark invalid modifiers in the parse string

    ParseSettings settings;
    settings.parseString = text;
    ParseResults invalid = d->parser->invalidModifiers(settings);
    const auto keys      = invalid.keys();

    for (const ParseResults::ResultsKey& key : keys)
    {
        setFormat(key.first, key.second, d->errorFormat);
    }

    // highlight quoted text in options and modifiers

    {
        QRegularExpression      expression(d->quotationRule.pattern);
        QRegularExpressionMatch match;
        int index = text.indexOf(expression, 0, &match);

        while (index >= 0)
        {
            QString fullmatched = match.captured(0);
            Q_UNUSED(fullmatched);
            int qlength         = match.capturedLength();
            setFormat(index, qlength, d->quotationFormat);
            index               = text.indexOf(expression, index + qlength, &match);
        }
    }
}

void Highlighter::setupHighlightingGrammar()
{
    if (!d->parser)
    {
        return;
    }

    Private::HighlightingRule rule;

    // --------------------------------------------------------

    d->optionFormat.setForeground(Qt::red);
    const auto list = d->parser->options();

    for (Rule* const option : list)<--- Variable 'option' can be declared as pointer to const
    {
        QRegularExpression r    = option->regExp();
        rule.type               = Private::OptionPattern;
        rule.pattern            = r;
        rule.format             = d->optionFormat;
        d->highlightingRules.append(rule);
    }

    // --------------------------------------------------------

    d->modifierFormat.setForeground(Qt::darkGreen);
    const auto list2 = d->parser->modifiers();

    for (Rule* const modifier : list2)<--- Variable 'modifier' can be declared as pointer to const
    {
        QRegularExpression r    = modifier->regExp();
        rule.type               = Private::ModifierPattern;
        rule.pattern            = r;
        rule.format             = d->modifierFormat;
        d->highlightingRules.append(rule);
    }

    // --------------------------------------------------------

    d->quotationFormat.setForeground(QColor(0x50, 0x50, 0xff)); // light blue
    d->quotationFormat.setFontItalic(true);
    d->quotationRule.pattern = QRegularExpression(QLatin1String("\".*\""));
    d->quotationRule.pattern.setPatternOptions(QRegularExpression::InvertedGreedinessOption);
    d->quotationRule.format  = d->quotationFormat;
    d->quotationRule.type    = Private::QuotedTextPattern;

    // --------------------------------------------------------

    d->parameterFormat.setForeground(Qt::darkYellow);
    d->parameterFormat.setFontItalic(true);

    // --------------------------------------------------------

    d->errorFormat.setForeground(Qt::white);
    d->errorFormat.setBackground(Qt::red);
}

} // namespace Digikam

#include "moc_highlighter.cpp"