|
| 1 | +/**************************************************************************** |
| 2 | +** |
| 3 | +** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | +** Contact: https://www.qt.io/licensing/ |
| 5 | +** |
| 6 | +** This file is part of the examples of the Qt Toolkit. |
| 7 | +** |
| 8 | +** $QT_BEGIN_LICENSE:BSD$ |
| 9 | +** Commercial License Usage |
| 10 | +** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | +** accordance with the commercial license agreement provided with the |
| 12 | +** Software or, alternatively, in accordance with the terms contained in |
| 13 | +** a written agreement between you and The Qt Company. For licensing terms |
| 14 | +** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | +** information use the contact form at https://www.qt.io/contact-us. |
| 16 | +** |
| 17 | +** BSD License Usage |
| 18 | +** Alternatively, you may use this file under the terms of the BSD license |
| 19 | +** as follows: |
| 20 | +** |
| 21 | +** "Redistribution and use in source and binary forms, with or without |
| 22 | +** modification, are permitted provided that the following conditions are |
| 23 | +** met: |
| 24 | +** * Redistributions of source code must retain the above copyright |
| 25 | +** notice, this list of conditions and the following disclaimer. |
| 26 | +** * Redistributions in binary form must reproduce the above copyright |
| 27 | +** notice, this list of conditions and the following disclaimer in |
| 28 | +** the documentation and/or other materials provided with the |
| 29 | +** distribution. |
| 30 | +** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | +** contributors may be used to endorse or promote products derived |
| 32 | +** from this software without specific prior written permission. |
| 33 | +** |
| 34 | +** |
| 35 | +** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | +** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | +** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | +** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | +** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | +** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | +** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | +** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | +** |
| 47 | +** $QT_END_LICENSE$ |
| 48 | +** |
| 49 | +****************************************************************************/ |
| 50 | + |
| 51 | +#include "highlighter.h" |
| 52 | + |
| 53 | +Highlighter::Highlighter(QTextDocument *parent) |
| 54 | + : QSyntaxHighlighter(parent) |
| 55 | +{ |
| 56 | + HighlightingRule rule; |
| 57 | + |
| 58 | + keywordFormat.setForeground(Qt::darkBlue); |
| 59 | + keywordFormat.setFontWeight(QFont::Bold); |
| 60 | + // const QString keywordPatterns[] = { |
| 61 | + // QStringLiteral("\\bchar\\b"), QStringLiteral("\\bclass\\b"), QStringLiteral("\\bconst\\b"), |
| 62 | + // QStringLiteral("\\bdouble\\b"), QStringLiteral("\\benum\\b"), QStringLiteral("\\bexplicit\\b"), |
| 63 | + // QStringLiteral("\\bfriend\\b"), QStringLiteral("\\binline\\b"), QStringLiteral("\\bint\\b"), |
| 64 | + // QStringLiteral("\\blong\\b"), QStringLiteral("\\bnamespace\\b"), QStringLiteral("\\boperator\\b"), |
| 65 | + // QStringLiteral("\\bprivate\\b"), QStringLiteral("\\bprotected\\b"), QStringLiteral("\\bpublic\\b"), |
| 66 | + // QStringLiteral("\\bshort\\b"), QStringLiteral("\\bsignals\\b"), QStringLiteral("\\bsigned\\b"), |
| 67 | + // QStringLiteral("\\bslots\\b"), QStringLiteral("\\bstatic\\b"), QStringLiteral("\\bstruct\\b"), |
| 68 | + // QStringLiteral("\\btemplate\\b"), QStringLiteral("\\btypedef\\b"), QStringLiteral("\\btypename\\b"), |
| 69 | + // QStringLiteral("\\bunion\\b"), QStringLiteral("\\bunsigned\\b"), QStringLiteral("\\bvirtual\\b"), |
| 70 | + // QStringLiteral("\\bvoid\\b"), QStringLiteral("\\bvolatile\\b"), QStringLiteral("\\bbool\\b") |
| 71 | + // }; |
| 72 | + // for (const QString &pattern : keywordPatterns) { |
| 73 | + // rule.pattern = QRegularExpression(pattern); |
| 74 | + // rule.format = keywordFormat; |
| 75 | + // highlightingRules.append(rule); |
| 76 | + // } |
| 77 | + |
| 78 | + classFormat.setFontWeight(QFont::Bold); |
| 79 | + classFormat.setForeground(Qt::darkMagenta); |
| 80 | + rule.pattern = QRegularExpression(QStringLiteral("\\bQ[A-Za-z]+\\b")); |
| 81 | + rule.format = classFormat; |
| 82 | + highlightingRules.append(rule); |
| 83 | + |
| 84 | + singleLineCommentFormat.setForeground(Qt::red); |
| 85 | + rule.pattern = QRegularExpression(QStringLiteral("//[^\n]*")); |
| 86 | + rule.format = singleLineCommentFormat; |
| 87 | + highlightingRules.append(rule); |
| 88 | + |
| 89 | + multiLineCommentFormat.setForeground(Qt::red); |
| 90 | + |
| 91 | + quotationFormat.setForeground(Qt::darkGreen); |
| 92 | + rule.pattern = QRegularExpression(QStringLiteral("\".*\"")); |
| 93 | + rule.format = quotationFormat; |
| 94 | + highlightingRules.append(rule); |
| 95 | + |
| 96 | + functionFormat.setFontItalic(true); |
| 97 | + functionFormat.setForeground(Qt::blue); |
| 98 | + rule.pattern = QRegularExpression(QStringLiteral("\\b[A-Za-z0-9_]+(?=\\()")); |
| 99 | + rule.format = functionFormat; |
| 100 | + highlightingRules.append(rule); |
| 101 | + |
| 102 | + commentStartExpression = QRegularExpression(QStringLiteral("/\\*")); |
| 103 | + commentEndExpression = QRegularExpression(QStringLiteral("\\*/")); |
| 104 | +} |
| 105 | + |
| 106 | +void Highlighter::highlightBlock(const QString &text) |
| 107 | +{ |
| 108 | + for (const HighlightingRule &rule : qAsConst(highlightingRules)) |
| 109 | + { |
| 110 | + QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text,0, |
| 111 | + QRegularExpression::MatchType::PartialPreferCompleteMatch, |
| 112 | + QRegularExpression::MatchOption::NoMatchOption); |
| 113 | + if (matchIterator.hasNext()) |
| 114 | + { |
| 115 | + QRegularExpressionMatch match = matchIterator.next(); |
| 116 | + setFormat(match.capturedStart(), match.capturedLength(), rule.format); |
| 117 | + } |
| 118 | + } |
| 119 | + setCurrentBlockState(0); |
| 120 | + |
| 121 | + int startIndex = 0; |
| 122 | + if (previousBlockState() != 1) |
| 123 | + startIndex = text.indexOf(commentStartExpression); |
| 124 | + |
| 125 | + while (startIndex >= 0) |
| 126 | + { |
| 127 | + QRegularExpressionMatch match = commentEndExpression.match(text, startIndex); |
| 128 | + int endIndex = match.capturedStart(); |
| 129 | + int commentLength = 0; |
| 130 | + if (endIndex == -1) |
| 131 | + { |
| 132 | + setCurrentBlockState(1); |
| 133 | + commentLength = text.length() - startIndex; |
| 134 | + } |
| 135 | + else |
| 136 | + { |
| 137 | + commentLength = endIndex - startIndex + match.capturedLength(); |
| 138 | + } |
| 139 | + setFormat(startIndex, commentLength, multiLineCommentFormat); |
| 140 | + startIndex = text.indexOf(commentStartExpression, startIndex + commentLength); |
| 141 | + } |
| 142 | +} |
0 commit comments