| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/gui/text/qsyntaxhighlighter.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtGui module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||
| 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 http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at http://www.qt.io/contact-us. | - | ||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 19 | ** General Public License version 2.1 or version 3 as published by the Free | - | ||||||||||||||||||
| 20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||
| 21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||
| 22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||
| 23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||
| 24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||
| 25 | ** | - | ||||||||||||||||||
| 26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||
| 27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||
| 28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||
| 29 | ** | - | ||||||||||||||||||
| 30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 31 | ** | - | ||||||||||||||||||
| 32 | ****************************************************************************/ | - | ||||||||||||||||||
| 33 | - | |||||||||||||||||||
| 34 | #include "qsyntaxhighlighter.h" | - | ||||||||||||||||||
| 35 | - | |||||||||||||||||||
| 36 | #ifndef QT_NO_SYNTAXHIGHLIGHTER | - | ||||||||||||||||||
| 37 | #include <private/qobject_p.h> | - | ||||||||||||||||||
| 38 | #include <qtextdocument.h> | - | ||||||||||||||||||
| 39 | #include <private/qtextdocument_p.h> | - | ||||||||||||||||||
| 40 | #include <qtextlayout.h> | - | ||||||||||||||||||
| 41 | #include <qpointer.h> | - | ||||||||||||||||||
| 42 | #include <qtextobject.h> | - | ||||||||||||||||||
| 43 | #include <qtextcursor.h> | - | ||||||||||||||||||
| 44 | #include <qdebug.h> | - | ||||||||||||||||||
| 45 | #include <qtimer.h> | - | ||||||||||||||||||
| 46 | - | |||||||||||||||||||
| 47 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 48 | - | |||||||||||||||||||
| 49 | class QSyntaxHighlighterPrivate : public QObjectPrivate | - | ||||||||||||||||||
| 50 | { | - | ||||||||||||||||||
| 51 | Q_DECLARE_PUBLIC(QSyntaxHighlighter) | - | ||||||||||||||||||
| 52 | public: | - | ||||||||||||||||||
| 53 | inline QSyntaxHighlighterPrivate() | - | ||||||||||||||||||
| 54 | : rehighlightPending(false), inReformatBlocks(false) | - | ||||||||||||||||||
| 55 | {} never executed: end of block | 0 | ||||||||||||||||||
| 56 | - | |||||||||||||||||||
| 57 | QPointer<QTextDocument> doc; | - | ||||||||||||||||||
| 58 | - | |||||||||||||||||||
| 59 | void _q_reformatBlocks(int from, int charsRemoved, int charsAdded); | - | ||||||||||||||||||
| 60 | void reformatBlocks(int from, int charsRemoved, int charsAdded); | - | ||||||||||||||||||
| 61 | void reformatBlock(const QTextBlock &block); | - | ||||||||||||||||||
| 62 | - | |||||||||||||||||||
| 63 | inline void rehighlight(QTextCursor &cursor, QTextCursor::MoveOperation operation) { | - | ||||||||||||||||||
| 64 | inReformatBlocks = true; | - | ||||||||||||||||||
| 65 | cursor.beginEditBlock(); | - | ||||||||||||||||||
| 66 | int from = cursor.position(); | - | ||||||||||||||||||
| 67 | cursor.movePosition(operation); | - | ||||||||||||||||||
| 68 | reformatBlocks(from, 0, cursor.position() - from); | - | ||||||||||||||||||
| 69 | cursor.endEditBlock(); | - | ||||||||||||||||||
| 70 | inReformatBlocks = false; | - | ||||||||||||||||||
| 71 | } never executed: end of block | 0 | ||||||||||||||||||
| 72 | - | |||||||||||||||||||
| 73 | inline void _q_delayedRehighlight() { | - | ||||||||||||||||||
| 74 | if (!rehighlightPending)
| 0 | ||||||||||||||||||
| 75 | return; never executed: return; | 0 | ||||||||||||||||||
| 76 | rehighlightPending = false; | - | ||||||||||||||||||
| 77 | q_func()->rehighlight(); | - | ||||||||||||||||||
| 78 | } never executed: end of block | 0 | ||||||||||||||||||
| 79 | - | |||||||||||||||||||
| 80 | void applyFormatChanges(); | - | ||||||||||||||||||
| 81 | QVector<QTextCharFormat> formatChanges; | - | ||||||||||||||||||
| 82 | QTextBlock currentBlock; | - | ||||||||||||||||||
| 83 | bool rehighlightPending; | - | ||||||||||||||||||
| 84 | bool inReformatBlocks; | - | ||||||||||||||||||
| 85 | }; | - | ||||||||||||||||||
| 86 | - | |||||||||||||||||||
| 87 | void QSyntaxHighlighterPrivate::applyFormatChanges() | - | ||||||||||||||||||
| 88 | { | - | ||||||||||||||||||
| 89 | bool formatsChanged = false; | - | ||||||||||||||||||
| 90 | - | |||||||||||||||||||
| 91 | QTextLayout *layout = currentBlock.layout(); | - | ||||||||||||||||||
| 92 | - | |||||||||||||||||||
| 93 | QVector<QTextLayout::FormatRange> ranges = layout->formats(); | - | ||||||||||||||||||
| 94 | - | |||||||||||||||||||
| 95 | const int preeditAreaStart = layout->preeditAreaPosition(); | - | ||||||||||||||||||
| 96 | const int preeditAreaLength = layout->preeditAreaText().length(); | - | ||||||||||||||||||
| 97 | - | |||||||||||||||||||
| 98 | if (preeditAreaLength != 0) {
| 0 | ||||||||||||||||||
| 99 | QVector<QTextLayout::FormatRange>::Iterator it = ranges.begin(); | - | ||||||||||||||||||
| 100 | while (it != ranges.end()) {
| 0 | ||||||||||||||||||
| 101 | if (it->start >= preeditAreaStart
| 0 | ||||||||||||||||||
| 102 | && it->start + it->length <= preeditAreaStart + preeditAreaLength) {
| 0 | ||||||||||||||||||
| 103 | ++it; | - | ||||||||||||||||||
| 104 | } else { never executed: end of block | 0 | ||||||||||||||||||
| 105 | it = ranges.erase(it); | - | ||||||||||||||||||
| 106 | formatsChanged = true; | - | ||||||||||||||||||
| 107 | } never executed: end of block | 0 | ||||||||||||||||||
| 108 | } | - | ||||||||||||||||||
| 109 | } else if (!ranges.isEmpty()) { never executed: end of block
| 0 | ||||||||||||||||||
| 110 | ranges.clear(); | - | ||||||||||||||||||
| 111 | formatsChanged = true; | - | ||||||||||||||||||
| 112 | } never executed: end of block | 0 | ||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | int i = 0; | - | ||||||||||||||||||
| 115 | while (i < formatChanges.count()) {
| 0 | ||||||||||||||||||
| 116 | QTextLayout::FormatRange r; | - | ||||||||||||||||||
| 117 | - | |||||||||||||||||||
| 118 | while (i < formatChanges.count() && formatChanges.at(i) == r.format)
| 0 | ||||||||||||||||||
| 119 | ++i; never executed: ++i; | 0 | ||||||||||||||||||
| 120 | - | |||||||||||||||||||
| 121 | if (i == formatChanges.count())
| 0 | ||||||||||||||||||
| 122 | break; never executed: break; | 0 | ||||||||||||||||||
| 123 | - | |||||||||||||||||||
| 124 | r.start = i; | - | ||||||||||||||||||
| 125 | r.format = formatChanges.at(i); | - | ||||||||||||||||||
| 126 | - | |||||||||||||||||||
| 127 | while (i < formatChanges.count() && formatChanges.at(i) == r.format)
| 0 | ||||||||||||||||||
| 128 | ++i; never executed: ++i; | 0 | ||||||||||||||||||
| 129 | - | |||||||||||||||||||
| 130 | Q_ASSERT(i <= formatChanges.count()); | - | ||||||||||||||||||
| 131 | r.length = i - r.start; | - | ||||||||||||||||||
| 132 | - | |||||||||||||||||||
| 133 | if (preeditAreaLength != 0) {
| 0 | ||||||||||||||||||
| 134 | if (r.start >= preeditAreaStart)
| 0 | ||||||||||||||||||
| 135 | r.start += preeditAreaLength; never executed: r.start += preeditAreaLength; | 0 | ||||||||||||||||||
| 136 | else if (r.start + r.length >= preeditAreaStart)
| 0 | ||||||||||||||||||
| 137 | r.length += preeditAreaLength; never executed: r.length += preeditAreaLength; | 0 | ||||||||||||||||||
| 138 | } never executed: end of block | 0 | ||||||||||||||||||
| 139 | - | |||||||||||||||||||
| 140 | ranges << r; | - | ||||||||||||||||||
| 141 | formatsChanged = true; | - | ||||||||||||||||||
| 142 | } never executed: end of block | 0 | ||||||||||||||||||
| 143 | - | |||||||||||||||||||
| 144 | if (formatsChanged) {
| 0 | ||||||||||||||||||
| 145 | layout->setFormats(ranges); | - | ||||||||||||||||||
| 146 | doc->markContentsDirty(currentBlock.position(), currentBlock.length()); | - | ||||||||||||||||||
| 147 | } never executed: end of block | 0 | ||||||||||||||||||
| 148 | } never executed: end of block | 0 | ||||||||||||||||||
| 149 | - | |||||||||||||||||||
| 150 | void QSyntaxHighlighterPrivate::_q_reformatBlocks(int from, int charsRemoved, int charsAdded) | - | ||||||||||||||||||
| 151 | { | - | ||||||||||||||||||
| 152 | if (!inReformatBlocks)
| 0 | ||||||||||||||||||
| 153 | reformatBlocks(from, charsRemoved, charsAdded); never executed: reformatBlocks(from, charsRemoved, charsAdded); | 0 | ||||||||||||||||||
| 154 | } never executed: end of block | 0 | ||||||||||||||||||
| 155 | - | |||||||||||||||||||
| 156 | void QSyntaxHighlighterPrivate::reformatBlocks(int from, int charsRemoved, int charsAdded) | - | ||||||||||||||||||
| 157 | { | - | ||||||||||||||||||
| 158 | rehighlightPending = false; | - | ||||||||||||||||||
| 159 | - | |||||||||||||||||||
| 160 | QTextBlock block = doc->findBlock(from); | - | ||||||||||||||||||
| 161 | if (!block.isValid())
| 0 | ||||||||||||||||||
| 162 | return; never executed: return; | 0 | ||||||||||||||||||
| 163 | - | |||||||||||||||||||
| 164 | int endPosition; | - | ||||||||||||||||||
| 165 | QTextBlock lastBlock = doc->findBlock(from + charsAdded + (charsRemoved > 0 ? 1 : 0)); | - | ||||||||||||||||||
| 166 | if (lastBlock.isValid())
| 0 | ||||||||||||||||||
| 167 | endPosition = lastBlock.position() + lastBlock.length(); never executed: endPosition = lastBlock.position() + lastBlock.length(); | 0 | ||||||||||||||||||
| 168 | else | - | ||||||||||||||||||
| 169 | endPosition = doc->docHandle()->length(); never executed: endPosition = doc->docHandle()->length(); | 0 | ||||||||||||||||||
| 170 | - | |||||||||||||||||||
| 171 | bool forceHighlightOfNextBlock = false; | - | ||||||||||||||||||
| 172 | - | |||||||||||||||||||
| 173 | while (block.isValid() && (block.position() < endPosition || forceHighlightOfNextBlock)) {
| 0 | ||||||||||||||||||
| 174 | const int stateBeforeHighlight = block.userState(); | - | ||||||||||||||||||
| 175 | - | |||||||||||||||||||
| 176 | reformatBlock(block); | - | ||||||||||||||||||
| 177 | - | |||||||||||||||||||
| 178 | forceHighlightOfNextBlock = (block.userState() != stateBeforeHighlight); | - | ||||||||||||||||||
| 179 | - | |||||||||||||||||||
| 180 | block = block.next(); | - | ||||||||||||||||||
| 181 | } never executed: end of block | 0 | ||||||||||||||||||
| 182 | - | |||||||||||||||||||
| 183 | formatChanges.clear(); | - | ||||||||||||||||||
| 184 | } never executed: end of block | 0 | ||||||||||||||||||
| 185 | - | |||||||||||||||||||
| 186 | void QSyntaxHighlighterPrivate::reformatBlock(const QTextBlock &block) | - | ||||||||||||||||||
| 187 | { | - | ||||||||||||||||||
| 188 | Q_Q(QSyntaxHighlighter); | - | ||||||||||||||||||
| 189 | - | |||||||||||||||||||
| 190 | Q_ASSERT_X(!currentBlock.isValid(), "QSyntaxHighlighter::reformatBlock()", "reFormatBlock() called recursively"); | - | ||||||||||||||||||
| 191 | - | |||||||||||||||||||
| 192 | currentBlock = block; | - | ||||||||||||||||||
| 193 | - | |||||||||||||||||||
| 194 | formatChanges.fill(QTextCharFormat(), block.length() - 1); | - | ||||||||||||||||||
| 195 | q->highlightBlock(block.text()); | - | ||||||||||||||||||
| 196 | applyFormatChanges(); | - | ||||||||||||||||||
| 197 | - | |||||||||||||||||||
| 198 | currentBlock = QTextBlock(); | - | ||||||||||||||||||
| 199 | } never executed: end of block | 0 | ||||||||||||||||||
| 200 | - | |||||||||||||||||||
| 201 | /*! | - | ||||||||||||||||||
| 202 | \class QSyntaxHighlighter | - | ||||||||||||||||||
| 203 | \reentrant | - | ||||||||||||||||||
| 204 | \inmodule QtGui | - | ||||||||||||||||||
| 205 | - | |||||||||||||||||||
| 206 | \brief The QSyntaxHighlighter class allows you to define syntax | - | ||||||||||||||||||
| 207 | highlighting rules, and in addition you can use the class to query | - | ||||||||||||||||||
| 208 | a document's current formatting or user data. | - | ||||||||||||||||||
| 209 | - | |||||||||||||||||||
| 210 | \since 4.1 | - | ||||||||||||||||||
| 211 | - | |||||||||||||||||||
| 212 | \ingroup richtext-processing | - | ||||||||||||||||||
| 213 | - | |||||||||||||||||||
| 214 | The QSyntaxHighlighter class is a base class for implementing | - | ||||||||||||||||||
| 215 | QTextDocument syntax highlighters. A syntax highligher automatically | - | ||||||||||||||||||
| 216 | highlights parts of the text in a QTextDocument. Syntax highlighters are | - | ||||||||||||||||||
| 217 | often used when the user is entering text in a specific format (for example source code) | - | ||||||||||||||||||
| 218 | and help the user to read the text and identify syntax errors. | - | ||||||||||||||||||
| 219 | - | |||||||||||||||||||
| 220 | To provide your own syntax highlighting, you must subclass | - | ||||||||||||||||||
| 221 | QSyntaxHighlighter and reimplement highlightBlock(). | - | ||||||||||||||||||
| 222 | - | |||||||||||||||||||
| 223 | When you create an instance of your QSyntaxHighlighter subclass, | - | ||||||||||||||||||
| 224 | pass it the QTextDocument that you want the syntax | - | ||||||||||||||||||
| 225 | highlighting to be applied to. For example: | - | ||||||||||||||||||
| 226 | - | |||||||||||||||||||
| 227 | \snippet code/src_gui_text_qsyntaxhighlighter.cpp 0 | - | ||||||||||||||||||
| 228 | - | |||||||||||||||||||
| 229 | After this your highlightBlock() function will be called | - | ||||||||||||||||||
| 230 | automatically whenever necessary. Use your highlightBlock() | - | ||||||||||||||||||
| 231 | function to apply formatting (e.g. setting the font and color) to | - | ||||||||||||||||||
| 232 | the text that is passed to it. QSyntaxHighlighter provides the | - | ||||||||||||||||||
| 233 | setFormat() function which applies a given QTextCharFormat on | - | ||||||||||||||||||
| 234 | the current text block. For example: | - | ||||||||||||||||||
| 235 | - | |||||||||||||||||||
| 236 | \snippet code/src_gui_text_qsyntaxhighlighter.cpp 1 | - | ||||||||||||||||||
| 237 | - | |||||||||||||||||||
| 238 | Some syntaxes can have constructs that span several text | - | ||||||||||||||||||
| 239 | blocks. For example, a C++ syntax highlighter should be able to | - | ||||||||||||||||||
| 240 | cope with \c{/}\c{*...*}\c{/} multiline comments. To deal with | - | ||||||||||||||||||
| 241 | these cases it is necessary to know the end state of the previous | - | ||||||||||||||||||
| 242 | text block (e.g. "in comment"). | - | ||||||||||||||||||
| 243 | - | |||||||||||||||||||
| 244 | Inside your highlightBlock() implementation you can query the end | - | ||||||||||||||||||
| 245 | state of the previous text block using the previousBlockState() | - | ||||||||||||||||||
| 246 | function. After parsing the block you can save the last state | - | ||||||||||||||||||
| 247 | using setCurrentBlockState(). | - | ||||||||||||||||||
| 248 | - | |||||||||||||||||||
| 249 | The currentBlockState() and previousBlockState() functions return | - | ||||||||||||||||||
| 250 | an int value. If no state is set, the returned value is -1. You | - | ||||||||||||||||||
| 251 | can designate any other value to identify any given state using | - | ||||||||||||||||||
| 252 | the setCurrentBlockState() function. Once the state is set the | - | ||||||||||||||||||
| 253 | QTextBlock keeps that value until it is set set again or until the | - | ||||||||||||||||||
| 254 | corresponding paragraph of text is deleted. | - | ||||||||||||||||||
| 255 | - | |||||||||||||||||||
| 256 | For example, if you're writing a simple C++ syntax highlighter, | - | ||||||||||||||||||
| 257 | you might designate 1 to signify "in comment": | - | ||||||||||||||||||
| 258 | - | |||||||||||||||||||
| 259 | \snippet code/src_gui_text_qsyntaxhighlighter.cpp 2 | - | ||||||||||||||||||
| 260 | - | |||||||||||||||||||
| 261 | In the example above, we first set the current block state to | - | ||||||||||||||||||
| 262 | 0. Then, if the previous block ended within a comment, we higlight | - | ||||||||||||||||||
| 263 | from the beginning of the current block (\c {startIndex = | - | ||||||||||||||||||
| 264 | 0}). Otherwise, we search for the given start expression. If the | - | ||||||||||||||||||
| 265 | specified end expression cannot be found in the text block, we | - | ||||||||||||||||||
| 266 | change the current block state by calling setCurrentBlockState(), | - | ||||||||||||||||||
| 267 | and make sure that the rest of the block is higlighted. | - | ||||||||||||||||||
| 268 | - | |||||||||||||||||||
| 269 | In addition you can query the current formatting and user data | - | ||||||||||||||||||
| 270 | using the format() and currentBlockUserData() functions | - | ||||||||||||||||||
| 271 | respectively. You can also attach user data to the current text | - | ||||||||||||||||||
| 272 | block using the setCurrentBlockUserData() function. | - | ||||||||||||||||||
| 273 | QTextBlockUserData can be used to store custom settings. In the | - | ||||||||||||||||||
| 274 | case of syntax highlighting, it is in particular interesting as | - | ||||||||||||||||||
| 275 | cache storage for information that you may figure out while | - | ||||||||||||||||||
| 276 | parsing the paragraph's text. For an example, see the | - | ||||||||||||||||||
| 277 | setCurrentBlockUserData() documentation. | - | ||||||||||||||||||
| 278 | - | |||||||||||||||||||
| 279 | \sa QTextDocument, {Syntax Highlighter Example} | - | ||||||||||||||||||
| 280 | */ | - | ||||||||||||||||||
| 281 | - | |||||||||||||||||||
| 282 | /*! | - | ||||||||||||||||||
| 283 | Constructs a QSyntaxHighlighter with the given \a parent. | - | ||||||||||||||||||
| 284 | - | |||||||||||||||||||
| 285 | If the parent is a QTextEdit, it installs the syntaxhighlighter on the | - | ||||||||||||||||||
| 286 | parents document. The specified QTextEdit also becomes the owner of | - | ||||||||||||||||||
| 287 | the QSyntaxHighlighter. | - | ||||||||||||||||||
| 288 | */ | - | ||||||||||||||||||
| 289 | QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent) | - | ||||||||||||||||||
| 290 | : QObject(*new QSyntaxHighlighterPrivate, parent) | - | ||||||||||||||||||
| 291 | { | - | ||||||||||||||||||
| 292 | if (parent->inherits("QTextEdit")) {
| 0 | ||||||||||||||||||
| 293 | QTextDocument *doc = parent->property("document").value<QTextDocument *>(); | - | ||||||||||||||||||
| 294 | if (doc)
| 0 | ||||||||||||||||||
| 295 | setDocument(doc); never executed: setDocument(doc); | 0 | ||||||||||||||||||
| 296 | } never executed: end of block | 0 | ||||||||||||||||||
| 297 | } never executed: end of block | 0 | ||||||||||||||||||
| 298 | - | |||||||||||||||||||
| 299 | /*! | - | ||||||||||||||||||
| 300 | Constructs a QSyntaxHighlighter and installs it on \a parent. | - | ||||||||||||||||||
| 301 | The specified QTextDocument also becomes the owner of the | - | ||||||||||||||||||
| 302 | QSyntaxHighlighter. | - | ||||||||||||||||||
| 303 | */ | - | ||||||||||||||||||
| 304 | QSyntaxHighlighter::QSyntaxHighlighter(QTextDocument *parent) | - | ||||||||||||||||||
| 305 | : QObject(*new QSyntaxHighlighterPrivate, parent) | - | ||||||||||||||||||
| 306 | { | - | ||||||||||||||||||
| 307 | setDocument(parent); | - | ||||||||||||||||||
| 308 | } never executed: end of block | 0 | ||||||||||||||||||
| 309 | - | |||||||||||||||||||
| 310 | /*! | - | ||||||||||||||||||
| 311 | Destructor. Uninstalls this syntax highlighter from the text document. | - | ||||||||||||||||||
| 312 | */ | - | ||||||||||||||||||
| 313 | QSyntaxHighlighter::~QSyntaxHighlighter() | - | ||||||||||||||||||
| 314 | { | - | ||||||||||||||||||
| 315 | setDocument(0); | - | ||||||||||||||||||
| 316 | } never executed: end of block | 0 | ||||||||||||||||||
| 317 | - | |||||||||||||||||||
| 318 | /*! | - | ||||||||||||||||||
| 319 | Installs the syntax highlighter on the given QTextDocument \a doc. | - | ||||||||||||||||||
| 320 | A QSyntaxHighlighter can only be used with one document at a time. | - | ||||||||||||||||||
| 321 | */ | - | ||||||||||||||||||
| 322 | void QSyntaxHighlighter::setDocument(QTextDocument *doc) | - | ||||||||||||||||||
| 323 | { | - | ||||||||||||||||||
| 324 | Q_D(QSyntaxHighlighter); | - | ||||||||||||||||||
| 325 | if (d->doc) {
| 0 | ||||||||||||||||||
| 326 | disconnect(d->doc, SIGNAL(contentsChange(int,int,int)), | - | ||||||||||||||||||
| 327 | this, SLOT(_q_reformatBlocks(int,int,int))); | - | ||||||||||||||||||
| 328 | - | |||||||||||||||||||
| 329 | QTextCursor cursor(d->doc); | - | ||||||||||||||||||
| 330 | cursor.beginEditBlock(); | - | ||||||||||||||||||
| 331 | for (QTextBlock blk = d->doc->begin(); blk.isValid(); blk = blk.next())
| 0 | ||||||||||||||||||
| 332 | blk.layout()->clearFormats(); never executed: blk.layout()->clearFormats(); | 0 | ||||||||||||||||||
| 333 | cursor.endEditBlock(); | - | ||||||||||||||||||
| 334 | } never executed: end of block | 0 | ||||||||||||||||||
| 335 | d->doc = doc; | - | ||||||||||||||||||
| 336 | if (d->doc) {
| 0 | ||||||||||||||||||
| 337 | connect(d->doc, SIGNAL(contentsChange(int,int,int)), | - | ||||||||||||||||||
| 338 | this, SLOT(_q_reformatBlocks(int,int,int))); | - | ||||||||||||||||||
| 339 | d->rehighlightPending = true; | - | ||||||||||||||||||
| 340 | QTimer::singleShot(0, this, SLOT(_q_delayedRehighlight())); | - | ||||||||||||||||||
| 341 | } never executed: end of block | 0 | ||||||||||||||||||
| 342 | } never executed: end of block | 0 | ||||||||||||||||||
| 343 | - | |||||||||||||||||||
| 344 | /*! | - | ||||||||||||||||||
| 345 | Returns the QTextDocument on which this syntax highlighter is | - | ||||||||||||||||||
| 346 | installed. | - | ||||||||||||||||||
| 347 | */ | - | ||||||||||||||||||
| 348 | QTextDocument *QSyntaxHighlighter::document() const | - | ||||||||||||||||||
| 349 | { | - | ||||||||||||||||||
| 350 | Q_D(const QSyntaxHighlighter); | - | ||||||||||||||||||
| 351 | return d->doc; never executed: return d->doc; | 0 | ||||||||||||||||||
| 352 | } | - | ||||||||||||||||||
| 353 | - | |||||||||||||||||||
| 354 | /*! | - | ||||||||||||||||||
| 355 | \since 4.2 | - | ||||||||||||||||||
| 356 | - | |||||||||||||||||||
| 357 | Reapplies the highlighting to the whole document. | - | ||||||||||||||||||
| 358 | - | |||||||||||||||||||
| 359 | \sa rehighlightBlock() | - | ||||||||||||||||||
| 360 | */ | - | ||||||||||||||||||
| 361 | void QSyntaxHighlighter::rehighlight() | - | ||||||||||||||||||
| 362 | { | - | ||||||||||||||||||
| 363 | Q_D(QSyntaxHighlighter); | - | ||||||||||||||||||
| 364 | if (!d->doc)
| 0 | ||||||||||||||||||
| 365 | return; never executed: return; | 0 | ||||||||||||||||||
| 366 | - | |||||||||||||||||||
| 367 | QTextCursor cursor(d->doc); | - | ||||||||||||||||||
| 368 | d->rehighlight(cursor, QTextCursor::End); | - | ||||||||||||||||||
| 369 | } never executed: end of block | 0 | ||||||||||||||||||
| 370 | - | |||||||||||||||||||
| 371 | /*! | - | ||||||||||||||||||
| 372 | \since 4.6 | - | ||||||||||||||||||
| 373 | - | |||||||||||||||||||
| 374 | Reapplies the highlighting to the given QTextBlock \a block. | - | ||||||||||||||||||
| 375 | - | |||||||||||||||||||
| 376 | \sa rehighlight() | - | ||||||||||||||||||
| 377 | */ | - | ||||||||||||||||||
| 378 | void QSyntaxHighlighter::rehighlightBlock(const QTextBlock &block) | - | ||||||||||||||||||
| 379 | { | - | ||||||||||||||||||
| 380 | Q_D(QSyntaxHighlighter); | - | ||||||||||||||||||
| 381 | if (!d->doc || !block.isValid() || block.document() != d->doc)
| 0 | ||||||||||||||||||
| 382 | return; never executed: return; | 0 | ||||||||||||||||||
| 383 | - | |||||||||||||||||||
| 384 | const bool rehighlightPending = d->rehighlightPending; | - | ||||||||||||||||||
| 385 | - | |||||||||||||||||||
| 386 | QTextCursor cursor(block); | - | ||||||||||||||||||
| 387 | d->rehighlight(cursor, QTextCursor::EndOfBlock); | - | ||||||||||||||||||
| 388 | - | |||||||||||||||||||
| 389 | if (rehighlightPending)
| 0 | ||||||||||||||||||
| 390 | d->rehighlightPending = rehighlightPending; never executed: d->rehighlightPending = rehighlightPending; | 0 | ||||||||||||||||||
| 391 | } never executed: end of block | 0 | ||||||||||||||||||
| 392 | - | |||||||||||||||||||
| 393 | /*! | - | ||||||||||||||||||
| 394 | \fn void QSyntaxHighlighter::highlightBlock(const QString &text) | - | ||||||||||||||||||
| 395 | - | |||||||||||||||||||
| 396 | Highlights the given text block. This function is called when | - | ||||||||||||||||||
| 397 | necessary by the rich text engine, i.e. on text blocks which have | - | ||||||||||||||||||
| 398 | changed. | - | ||||||||||||||||||
| 399 | - | |||||||||||||||||||
| 400 | To provide your own syntax highlighting, you must subclass | - | ||||||||||||||||||
| 401 | QSyntaxHighlighter and reimplement highlightBlock(). In your | - | ||||||||||||||||||
| 402 | reimplementation you should parse the block's \a text and call | - | ||||||||||||||||||
| 403 | setFormat() as often as necessary to apply any font and color | - | ||||||||||||||||||
| 404 | changes that you require. For example: | - | ||||||||||||||||||
| 405 | - | |||||||||||||||||||
| 406 | \snippet code/src_gui_text_qsyntaxhighlighter.cpp 3 | - | ||||||||||||||||||
| 407 | - | |||||||||||||||||||
| 408 | Some syntaxes can have constructs that span several text | - | ||||||||||||||||||
| 409 | blocks. For example, a C++ syntax highlighter should be able to | - | ||||||||||||||||||
| 410 | cope with \c{/}\c{*...*}\c{/} multiline comments. To deal with | - | ||||||||||||||||||
| 411 | these cases it is necessary to know the end state of the previous | - | ||||||||||||||||||
| 412 | text block (e.g. "in comment"). | - | ||||||||||||||||||
| 413 | - | |||||||||||||||||||
| 414 | Inside your highlightBlock() implementation you can query the end | - | ||||||||||||||||||
| 415 | state of the previous text block using the previousBlockState() | - | ||||||||||||||||||
| 416 | function. After parsing the block you can save the last state | - | ||||||||||||||||||
| 417 | using setCurrentBlockState(). | - | ||||||||||||||||||
| 418 | - | |||||||||||||||||||
| 419 | The currentBlockState() and previousBlockState() functions return | - | ||||||||||||||||||
| 420 | an int value. If no state is set, the returned value is -1. You | - | ||||||||||||||||||
| 421 | can designate any other value to identify any given state using | - | ||||||||||||||||||
| 422 | the setCurrentBlockState() function. Once the state is set the | - | ||||||||||||||||||
| 423 | QTextBlock keeps that value until it is set set again or until the | - | ||||||||||||||||||
| 424 | corresponding paragraph of text gets deleted. | - | ||||||||||||||||||
| 425 | - | |||||||||||||||||||
| 426 | For example, if you're writing a simple C++ syntax highlighter, | - | ||||||||||||||||||
| 427 | you might designate 1 to signify "in comment". For a text block | - | ||||||||||||||||||
| 428 | that ended in the middle of a comment you'd set 1 using | - | ||||||||||||||||||
| 429 | setCurrentBlockState, and for other paragraphs you'd set 0. | - | ||||||||||||||||||
| 430 | In your parsing code if the return value of previousBlockState() | - | ||||||||||||||||||
| 431 | is 1, you would highlight the text as a C++ comment until you | - | ||||||||||||||||||
| 432 | reached the closing \c{*}\c{/}. | - | ||||||||||||||||||
| 433 | - | |||||||||||||||||||
| 434 | \sa previousBlockState(), setFormat(), setCurrentBlockState() | - | ||||||||||||||||||
| 435 | */ | - | ||||||||||||||||||
| 436 | - | |||||||||||||||||||
| 437 | /*! | - | ||||||||||||||||||
| 438 | This function is applied to the syntax highlighter's current text | - | ||||||||||||||||||
| 439 | block (i.e. the text that is passed to the highlightBlock() | - | ||||||||||||||||||
| 440 | function). | - | ||||||||||||||||||
| 441 | - | |||||||||||||||||||
| 442 | The specified \a format is applied to the text from the \a start | - | ||||||||||||||||||
| 443 | position for a length of \a count characters (if \a count is 0, | - | ||||||||||||||||||
| 444 | nothing is done). The formatting properties set in \a format are | - | ||||||||||||||||||
| 445 | merged at display time with the formatting information stored | - | ||||||||||||||||||
| 446 | directly in the document, for example as previously set with | - | ||||||||||||||||||
| 447 | QTextCursor's functions. Note that the document itself remains | - | ||||||||||||||||||
| 448 | unmodified by the format set through this function. | - | ||||||||||||||||||
| 449 | - | |||||||||||||||||||
| 450 | \sa format(), highlightBlock() | - | ||||||||||||||||||
| 451 | */ | - | ||||||||||||||||||
| 452 | void QSyntaxHighlighter::setFormat(int start, int count, const QTextCharFormat &format) | - | ||||||||||||||||||
| 453 | { | - | ||||||||||||||||||
| 454 | Q_D(QSyntaxHighlighter); | - | ||||||||||||||||||
| 455 | if (start < 0 || start >= d->formatChanges.count())
| 0 | ||||||||||||||||||
| 456 | return; never executed: return; | 0 | ||||||||||||||||||
| 457 | - | |||||||||||||||||||
| 458 | const int end = qMin(start + count, d->formatChanges.count()); | - | ||||||||||||||||||
| 459 | for (int i = start; i < end; ++i)
| 0 | ||||||||||||||||||
| 460 | d->formatChanges[i] = format; never executed: d->formatChanges[i] = format; | 0 | ||||||||||||||||||
| 461 | } never executed: end of block | 0 | ||||||||||||||||||
| 462 | - | |||||||||||||||||||
| 463 | /*! | - | ||||||||||||||||||
| 464 | \overload | - | ||||||||||||||||||
| 465 | - | |||||||||||||||||||
| 466 | The specified \a color is applied to the current text block from | - | ||||||||||||||||||
| 467 | the \a start position for a length of \a count characters. | - | ||||||||||||||||||
| 468 | - | |||||||||||||||||||
| 469 | The other attributes of the current text block, e.g. the font and | - | ||||||||||||||||||
| 470 | background color, are reset to default values. | - | ||||||||||||||||||
| 471 | - | |||||||||||||||||||
| 472 | \sa format(), highlightBlock() | - | ||||||||||||||||||
| 473 | */ | - | ||||||||||||||||||
| 474 | void QSyntaxHighlighter::setFormat(int start, int count, const QColor &color) | - | ||||||||||||||||||
| 475 | { | - | ||||||||||||||||||
| 476 | QTextCharFormat format; | - | ||||||||||||||||||
| 477 | format.setForeground(color); | - | ||||||||||||||||||
| 478 | setFormat(start, count, format); | - | ||||||||||||||||||
| 479 | } never executed: end of block | 0 | ||||||||||||||||||
| 480 | - | |||||||||||||||||||
| 481 | /*! | - | ||||||||||||||||||
| 482 | \overload | - | ||||||||||||||||||
| 483 | - | |||||||||||||||||||
| 484 | The specified \a font is applied to the current text block from | - | ||||||||||||||||||
| 485 | the \a start position for a length of \a count characters. | - | ||||||||||||||||||
| 486 | - | |||||||||||||||||||
| 487 | The other attributes of the current text block, e.g. the font and | - | ||||||||||||||||||
| 488 | background color, are reset to default values. | - | ||||||||||||||||||
| 489 | - | |||||||||||||||||||
| 490 | \sa format(), highlightBlock() | - | ||||||||||||||||||
| 491 | */ | - | ||||||||||||||||||
| 492 | void QSyntaxHighlighter::setFormat(int start, int count, const QFont &font) | - | ||||||||||||||||||
| 493 | { | - | ||||||||||||||||||
| 494 | QTextCharFormat format; | - | ||||||||||||||||||
| 495 | format.setFont(font); | - | ||||||||||||||||||
| 496 | setFormat(start, count, format); | - | ||||||||||||||||||
| 497 | } never executed: end of block | 0 | ||||||||||||||||||
| 498 | - | |||||||||||||||||||
| 499 | /*! | - | ||||||||||||||||||
| 500 | \fn QTextCharFormat QSyntaxHighlighter::format(int position) const | - | ||||||||||||||||||
| 501 | - | |||||||||||||||||||
| 502 | Returns the format at \a position inside the syntax highlighter's | - | ||||||||||||||||||
| 503 | current text block. | - | ||||||||||||||||||
| 504 | */ | - | ||||||||||||||||||
| 505 | QTextCharFormat QSyntaxHighlighter::format(int pos) const | - | ||||||||||||||||||
| 506 | { | - | ||||||||||||||||||
| 507 | Q_D(const QSyntaxHighlighter); | - | ||||||||||||||||||
| 508 | if (pos < 0 || pos >= d->formatChanges.count())
| 0 | ||||||||||||||||||
| 509 | return QTextCharFormat(); never executed: return QTextCharFormat(); | 0 | ||||||||||||||||||
| 510 | return d->formatChanges.at(pos); never executed: return d->formatChanges.at(pos); | 0 | ||||||||||||||||||
| 511 | } | - | ||||||||||||||||||
| 512 | - | |||||||||||||||||||
| 513 | /*! | - | ||||||||||||||||||
| 514 | Returns the end state of the text block previous to the | - | ||||||||||||||||||
| 515 | syntax highlighter's current block. If no value was | - | ||||||||||||||||||
| 516 | previously set, the returned value is -1. | - | ||||||||||||||||||
| 517 | - | |||||||||||||||||||
| 518 | \sa highlightBlock(), setCurrentBlockState() | - | ||||||||||||||||||
| 519 | */ | - | ||||||||||||||||||
| 520 | int QSyntaxHighlighter::previousBlockState() const | - | ||||||||||||||||||
| 521 | { | - | ||||||||||||||||||
| 522 | Q_D(const QSyntaxHighlighter); | - | ||||||||||||||||||
| 523 | if (!d->currentBlock.isValid())
| 0 | ||||||||||||||||||
| 524 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
| 525 | - | |||||||||||||||||||
| 526 | const QTextBlock previous = d->currentBlock.previous(); | - | ||||||||||||||||||
| 527 | if (!previous.isValid())
| 0 | ||||||||||||||||||
| 528 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
| 529 | - | |||||||||||||||||||
| 530 | return previous.userState(); never executed: return previous.userState(); | 0 | ||||||||||||||||||
| 531 | } | - | ||||||||||||||||||
| 532 | - | |||||||||||||||||||
| 533 | /*! | - | ||||||||||||||||||
| 534 | Returns the state of the current text block. If no value is set, | - | ||||||||||||||||||
| 535 | the returned value is -1. | - | ||||||||||||||||||
| 536 | */ | - | ||||||||||||||||||
| 537 | int QSyntaxHighlighter::currentBlockState() const | - | ||||||||||||||||||
| 538 | { | - | ||||||||||||||||||
| 539 | Q_D(const QSyntaxHighlighter); | - | ||||||||||||||||||
| 540 | if (!d->currentBlock.isValid())
| 0 | ||||||||||||||||||
| 541 | return -1; never executed: return -1; | 0 | ||||||||||||||||||
| 542 | - | |||||||||||||||||||
| 543 | return d->currentBlock.userState(); never executed: return d->currentBlock.userState(); | 0 | ||||||||||||||||||
| 544 | } | - | ||||||||||||||||||
| 545 | - | |||||||||||||||||||
| 546 | /*! | - | ||||||||||||||||||
| 547 | Sets the state of the current text block to \a newState. | - | ||||||||||||||||||
| 548 | - | |||||||||||||||||||
| 549 | \sa highlightBlock() | - | ||||||||||||||||||
| 550 | */ | - | ||||||||||||||||||
| 551 | void QSyntaxHighlighter::setCurrentBlockState(int newState) | - | ||||||||||||||||||
| 552 | { | - | ||||||||||||||||||
| 553 | Q_D(QSyntaxHighlighter); | - | ||||||||||||||||||
| 554 | if (!d->currentBlock.isValid())
| 0 | ||||||||||||||||||
| 555 | return; never executed: return; | 0 | ||||||||||||||||||
| 556 | - | |||||||||||||||||||
| 557 | d->currentBlock.setUserState(newState); | - | ||||||||||||||||||
| 558 | } never executed: end of block | 0 | ||||||||||||||||||
| 559 | - | |||||||||||||||||||
| 560 | /*! | - | ||||||||||||||||||
| 561 | Attaches the given \a data to the current text block. The | - | ||||||||||||||||||
| 562 | ownership is passed to the underlying text document, i.e. the | - | ||||||||||||||||||
| 563 | provided QTextBlockUserData object will be deleted if the | - | ||||||||||||||||||
| 564 | corresponding text block gets deleted. | - | ||||||||||||||||||
| 565 | - | |||||||||||||||||||
| 566 | QTextBlockUserData can be used to store custom settings. In the | - | ||||||||||||||||||
| 567 | case of syntax highlighting, it is in particular interesting as | - | ||||||||||||||||||
| 568 | cache storage for information that you may figure out while | - | ||||||||||||||||||
| 569 | parsing the paragraph's text. | - | ||||||||||||||||||
| 570 | - | |||||||||||||||||||
| 571 | For example while parsing the text, you can keep track of | - | ||||||||||||||||||
| 572 | parenthesis characters that you encounter ('{[(' and the like), | - | ||||||||||||||||||
| 573 | and store their relative position and the actual QChar in a simple | - | ||||||||||||||||||
| 574 | class derived from QTextBlockUserData: | - | ||||||||||||||||||
| 575 | - | |||||||||||||||||||
| 576 | \snippet code/src_gui_text_qsyntaxhighlighter.cpp 4 | - | ||||||||||||||||||
| 577 | - | |||||||||||||||||||
| 578 | During cursor navigation in the associated editor, you can ask the | - | ||||||||||||||||||
| 579 | current QTextBlock (retrieved using the QTextCursor::block() | - | ||||||||||||||||||
| 580 | function) if it has a user data object set and cast it to your \c | - | ||||||||||||||||||
| 581 | BlockData object. Then you can check if the current cursor | - | ||||||||||||||||||
| 582 | position matches with a previously recorded parenthesis position, | - | ||||||||||||||||||
| 583 | and, depending on the type of parenthesis (opening or closing), | - | ||||||||||||||||||
| 584 | find the next opening or closing parenthesis on the same level. | - | ||||||||||||||||||
| 585 | - | |||||||||||||||||||
| 586 | In this way you can do a visual parenthesis matching and highlight | - | ||||||||||||||||||
| 587 | from the current cursor position to the matching parenthesis. That | - | ||||||||||||||||||
| 588 | makes it easier to spot a missing parenthesis in your code and to | - | ||||||||||||||||||
| 589 | find where a corresponding opening/closing parenthesis is when | - | ||||||||||||||||||
| 590 | editing parenthesis intensive code. | - | ||||||||||||||||||
| 591 | - | |||||||||||||||||||
| 592 | \sa QTextBlock::setUserData() | - | ||||||||||||||||||
| 593 | */ | - | ||||||||||||||||||
| 594 | void QSyntaxHighlighter::setCurrentBlockUserData(QTextBlockUserData *data) | - | ||||||||||||||||||
| 595 | { | - | ||||||||||||||||||
| 596 | Q_D(QSyntaxHighlighter); | - | ||||||||||||||||||
| 597 | if (!d->currentBlock.isValid())
| 0 | ||||||||||||||||||
| 598 | return; never executed: return; | 0 | ||||||||||||||||||
| 599 | - | |||||||||||||||||||
| 600 | d->currentBlock.setUserData(data); | - | ||||||||||||||||||
| 601 | } never executed: end of block | 0 | ||||||||||||||||||
| 602 | - | |||||||||||||||||||
| 603 | /*! | - | ||||||||||||||||||
| 604 | Returns the QTextBlockUserData object previously attached to the | - | ||||||||||||||||||
| 605 | current text block. | - | ||||||||||||||||||
| 606 | - | |||||||||||||||||||
| 607 | \sa QTextBlock::userData(), setCurrentBlockUserData() | - | ||||||||||||||||||
| 608 | */ | - | ||||||||||||||||||
| 609 | QTextBlockUserData *QSyntaxHighlighter::currentBlockUserData() const | - | ||||||||||||||||||
| 610 | { | - | ||||||||||||||||||
| 611 | Q_D(const QSyntaxHighlighter); | - | ||||||||||||||||||
| 612 | if (!d->currentBlock.isValid())
| 0 | ||||||||||||||||||
| 613 | return 0; never executed: return 0; | 0 | ||||||||||||||||||
| 614 | - | |||||||||||||||||||
| 615 | return d->currentBlock.userData(); never executed: return d->currentBlock.userData(); | 0 | ||||||||||||||||||
| 616 | } | - | ||||||||||||||||||
| 617 | - | |||||||||||||||||||
| 618 | /*! | - | ||||||||||||||||||
| 619 | \since 4.4 | - | ||||||||||||||||||
| 620 | - | |||||||||||||||||||
| 621 | Returns the current text block. | - | ||||||||||||||||||
| 622 | */ | - | ||||||||||||||||||
| 623 | QTextBlock QSyntaxHighlighter::currentBlock() const | - | ||||||||||||||||||
| 624 | { | - | ||||||||||||||||||
| 625 | Q_D(const QSyntaxHighlighter); | - | ||||||||||||||||||
| 626 | return d->currentBlock; never executed: return d->currentBlock; | 0 | ||||||||||||||||||
| 627 | } | - | ||||||||||||||||||
| 628 | - | |||||||||||||||||||
| 629 | QT_END_NAMESPACE | - | ||||||||||||||||||
| 630 | - | |||||||||||||||||||
| 631 | #include "moc_qsyntaxhighlighter.cpp" | - | ||||||||||||||||||
| 632 | - | |||||||||||||||||||
| 633 | #endif // QT_NO_SYNTAXHIGHLIGHTER | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |