File indexing completed on 2024-05-19 05:11:51

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "akonadisearchsyntaxhighlighter.h"
0008 using namespace Akonadi::Search;
0009 
0010 AkonadiSearchSyntaxHighlighter::AkonadiSearchSyntaxHighlighter(QTextDocument *doc)
0011     : QSyntaxHighlighter(doc)
0012 {
0013     init();
0014 }
0015 
0016 AkonadiSearchSyntaxHighlighter::~AkonadiSearchSyntaxHighlighter() = default;
0017 
0018 void AkonadiSearchSyntaxHighlighter::highlightBlock(const QString &text)
0019 {
0020     for (const Rule &rule : std::as_const(m_rules)) {
0021         QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
0022         while (matchIterator.hasNext()) {
0023             QRegularExpressionMatch match = matchIterator.next();
0024             setFormat(match.capturedStart(), match.capturedLength(), rule.format);
0025         }
0026     }
0027 }
0028 
0029 void AkonadiSearchSyntaxHighlighter::init()
0030 {
0031     QTextCharFormat testFormat;
0032     testFormat.setForeground(Qt::black);
0033     testFormat.setFontWeight(QFont::Bold);
0034     QStringList testType;
0035     // Collection
0036     testType << QStringLiteral("C\\d+");
0037 
0038     // Emails:
0039     // From
0040     testType << QStringLiteral("\\bF");
0041     // To
0042     testType << QStringLiteral("\\bT");
0043     // CC
0044     testType << QStringLiteral("\\bCC");
0045     // BC
0046     testType << QStringLiteral("\\bBC");
0047     // Organization
0048     testType << QStringLiteral("\\bO");
0049     // Reply To
0050     testType << QStringLiteral("\\bRT");
0051     // Resent-from
0052     testType << QStringLiteral("\\bRF");
0053     // List Id
0054     testType << QStringLiteral("\\bLI");
0055     // X-Loop
0056     testType << QStringLiteral("\\bXL");
0057     // X-Mailing-List
0058     testType << QStringLiteral("\\bXML");
0059     // X-Spam-Flag
0060     testType << QStringLiteral("\\bXSF");
0061     // BO body element
0062     testType << QStringLiteral("\\bBO");
0063 
0064     // Contacts:
0065     // Name
0066     testType << QStringLiteral("\\bNA");
0067     // NickName
0068     testType << QStringLiteral("\\bNI");
0069 
0070     // Calendar
0071     testType << QStringLiteral("\\bO");
0072     testType << QStringLiteral("\\bPS");
0073     testType << QStringLiteral("\\bS");
0074     testType << QStringLiteral("\\bL");
0075     for (const QString &s : std::as_const(testType)) {
0076         const QRegularExpression regex(s);
0077         m_rules.append(Rule(regex, testFormat));
0078     }
0079 }
0080 
0081 #include "moc_akonadisearchsyntaxhighlighter.cpp"