OpenCoverage

qsyntaxhighlighter.cpp

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

Generated by Squish Coco Non-Commercial 4.3.0-BETA-master-30-08-2018-4cb69e9