qsyntaxhighlighter.cpp

Absolute File Name:/home/qt/qt5_coco/qt5/qtbase/src/gui/text/qsyntaxhighlighter.cpp
Switch to Source codePreprocessed file
LineSourceCount
1-
2-
3-
4-
5-
6-
7-
8class QSyntaxHighlighterPrivate : public QObjectPrivate-
9{-
10 inline QSyntaxHighlighter* q_func() { return static_cast<QSyntaxHighlighter *>(q_ptr); } inline const QSyntaxHighlighter* q_func() const { return static_cast<const QSyntaxHighlighter *>(q_ptr); } friend class QSyntaxHighlighter;-
11public:-
12 inline QSyntaxHighlighterPrivate()-
13 : rehighlightPending(false), inReformatBlocks(false)-
14 {}
never executed: end of block
0
15-
16 QPointer<QTextDocument> doc;-
17-
18 void _q_reformatBlocks(int from, int charsRemoved, int charsAdded);-
19 void reformatBlocks(int from, int charsRemoved, int charsAdded);-
20 void reformatBlock(const QTextBlock &block);-
21-
22 inline void rehighlight(QTextCursor &cursor, QTextCursor::MoveOperation operation) {-
23 inReformatBlocks = true;-
24 cursor.beginEditBlock();-
25 int from = cursor.position();-
26 cursor.movePosition(operation);-
27 reformatBlocks(from, 0, cursor.position() - from);-
28 cursor.endEditBlock();-
29 inReformatBlocks = false;-
30 }
never executed: end of block
0
31-
32 inline void _q_delayedRehighlight() {-
33 if (!rehighlightPending
!rehighlightPendingDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
34 return;
never executed: return;
0
35 rehighlightPending = false;-
36 q_func()->rehighlight();-
37 }
never executed: end of block
0
38-
39 void applyFormatChanges();-
40 QVector<QTextCharFormat> formatChanges;-
41 QTextBlock currentBlock;-
42 bool rehighlightPending;-
43 bool inReformatBlocks;-
44};-
45-
46void QSyntaxHighlighterPrivate::applyFormatChanges()-
47{-
48 bool formatsChanged = false;-
49-
50 QTextLayout *layout = currentBlock.layout();-
51-
52 QVector<QTextLayout::FormatRange> ranges = layout->formats();-
53-
54 const int preeditAreaStart = layout->preeditAreaPosition();-
55 const int preeditAreaLength = layout->preeditAreaText().length();-
56-
57 if (preeditAreaLength != 0
preeditAreaLength != 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
58 auto isOutsidePreeditArea = [=](const QTextLayout::FormatRange &range) {-
59 return
never executed: return range.start < preeditAreaStart || range.start + range.length > preeditAreaStart + preeditAreaLength;
range.start < preeditAreaStart
never executed: return range.start < preeditAreaStart || range.start + range.length > preeditAreaStart + preeditAreaLength;
0
60 || range.start + range.length > preeditAreaStart + preeditAreaLength;
never executed: return range.start < preeditAreaStart || range.start + range.length > preeditAreaStart + preeditAreaLength;
0
61 };-
62 const auto it = std::remove_if(ranges.begin(), ranges.end(),-
63 isOutsidePreeditArea);-
64 if (it != ranges.end()
it != ranges.end()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
65 ranges.erase(it, ranges.end());-
66 formatsChanged = true;-
67 }
never executed: end of block
0
68 }
never executed: end of block
else if (!ranges.isEmpty()
!ranges.isEmpty()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
69 ranges.clear();-
70 formatsChanged = true;-
71 }
never executed: end of block
0
72-
73 int i = 0;-
74 while (i < formatChanges.count()
i < formatChanges.count()Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
75 QTextLayout::FormatRange r;-
76-
77 while (i < formatChanges.count()
i < formatChanges.count()Description
TRUEnever evaluated
FALSEnever evaluated
&& formatChanges.at(i) == r.format
formatChanges....i) == r.formatDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
78 ++
never executed: ++i;
i;
never executed: ++i;
0
79-
80 if (i == formatChanges.count()
i == formatChanges.count()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
81 break;
never executed: break;
0
82-
83 r.start = i;-
84 r.format = formatChanges.at(i);-
85-
86 while (i < formatChanges.count()
i < formatChanges.count()Description
TRUEnever evaluated
FALSEnever evaluated
&& formatChanges.at(i) == r.format
formatChanges....i) == r.formatDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
87 ++
never executed: ++i;
i;
never executed: ++i;
0
88-
89 ((!(i <= formatChanges.count())) ? qt_assert("i <= formatChanges.count()",__FILE__,138) : qt_noop());-
90 r.length = i - r.start;-
91-
92 if (preeditAreaLength != 0
preeditAreaLength != 0Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
93 if (r.start >= preeditAreaStart
r.start >= preeditAreaStartDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
94 r.start += preeditAreaLength;
never executed: r.start += preeditAreaLength;
0
95 else if (r.start + r.length >= preeditAreaStart
r.start + r.le...eeditAreaStartDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
96 r.length += preeditAreaLength;
never executed: r.length += preeditAreaLength;
0
97 }
never executed: end of block
0
98-
99 ranges << r;-
100 formatsChanged = true;-
101 }
never executed: end of block
0
102-
103 if (formatsChanged
formatsChangedDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
104 layout->setFormats(ranges);-
105 doc->markContentsDirty(currentBlock.position(), currentBlock.length());-
106 }
never executed: end of block
0
107}
never executed: end of block
0
108-
109void QSyntaxHighlighterPrivate::_q_reformatBlocks(int from, int charsRemoved, int charsAdded)-
110{-
111 if (!inReformatBlocks
!inReformatBlocksDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
112 reformatBlocks(from, charsRemoved, charsAdded);
never executed: reformatBlocks(from, charsRemoved, charsAdded);
0
113}
never executed: end of block
0
114-
115void QSyntaxHighlighterPrivate::reformatBlocks(int from, int charsRemoved, int charsAdded)-
116{-
117 rehighlightPending = false;-
118-
119 QTextBlock block = doc->findBlock(from);-
120 if (!block.isValid()
!block.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
121 return;
never executed: return;
0
122-
123 int endPosition;-
124 QTextBlock lastBlock = doc->findBlock(from + charsAdded + (charsRemoved > 0 ? 1 : 0));-
125 if (lastBlock.isValid()
lastBlock.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
126 endPosition = lastBlock.position() + lastBlock.length();
never executed: endPosition = lastBlock.position() + lastBlock.length();
0
127 else-
128 endPosition = doc->docHandle()->length();
never executed: endPosition = doc->docHandle()->length();
0
129-
130 bool forceHighlightOfNextBlock = false;-
131-
132 while (block.isValid()
block.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
&& (block.position() < endPosition
block.position() < endPositionDescription
TRUEnever evaluated
FALSEnever evaluated
|| forceHighlightOfNextBlock
forceHighlightOfNextBlockDescription
TRUEnever evaluated
FALSEnever evaluated
)) {
0
133 const int stateBeforeHighlight = block.userState();-
134-
135 reformatBlock(block);-
136-
137 forceHighlightOfNextBlock = (block.userState() != stateBeforeHighlight);-
138-
139 block = block.next();-
140 }
never executed: end of block
0
141-
142 formatChanges.clear();-
143}
never executed: end of block
0
144-
145void QSyntaxHighlighterPrivate::reformatBlock(const QTextBlock &block)-
146{-
147 QSyntaxHighlighter * const q = q_func();-
148-
149 ((!(!currentBlock.isValid())) ? qt_assert_x("QSyntaxHighlighter::reformatBlock()", "reFormatBlock() called recursively",__FILE__,198) : qt_noop());-
150-
151 currentBlock = block;-
152-
153 formatChanges.fill(QTextCharFormat(), block.length() - 1);-
154 q->highlightBlock(block.text());-
155 applyFormatChanges();-
156-
157 currentBlock = QTextBlock();-
158}
never executed: end of block
0
159QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent)-
160 : QObject(*new QSyntaxHighlighterPrivate, parent)-
161{-
162 if (parent->inherits("QTextEdit")
parent->inherits("QTextEdit")Description
TRUEnever evaluated
FALSEnever evaluated
) {
0
163 QTextDocument *doc = parent->property("document").value<QTextDocument *>();-
164 if (doc
docDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
165 setDocument(doc);
never executed: setDocument(doc);
0
166 }
never executed: end of block
0
167}
never executed: end of block
0
168-
169-
170-
171-
172-
173-
174QSyntaxHighlighter::QSyntaxHighlighter(QTextDocument *parent)-
175 : QObject(*new QSyntaxHighlighterPrivate, parent)-
176{-
177 setDocument(parent);-
178}
never executed: end of block
0
179-
180-
181-
182-
183QSyntaxHighlighter::~QSyntaxHighlighter()-
184{-
185 setDocument(0);-
186}
never executed: end of block
0
187-
188-
189-
190-
191-
192void QSyntaxHighlighter::setDocument(QTextDocument *doc)-
193{-
194 QSyntaxHighlighterPrivate * const d = d_func();-
195 if (d->doc
d->docDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
196 disconnect(d->doc, qFlagLocation("2""contentsChange(int,int,int)" "\0" __FILE__ ":" "334"),-
197 this, qFlagLocation("1""_q_reformatBlocks(int,int,int)" "\0" __FILE__ ":" "335"));-
198-
199 QTextCursor cursor(d->doc);-
200 cursor.beginEditBlock();-
201 for (QTextBlock blk = d->doc->begin(); blk.isValid()
blk.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
; blk = blk.next())
0
202 blk.layout()->clearFormats();
never executed: blk.layout()->clearFormats();
0
203 cursor.endEditBlock();-
204 }
never executed: end of block
0
205 d->doc = doc;-
206 if (d->doc
d->docDescription
TRUEnever evaluated
FALSEnever evaluated
) {
0
207 connect(d->doc, qFlagLocation("2""contentsChange(int,int,int)" "\0" __FILE__ ":" "345"),-
208 this, qFlagLocation("1""_q_reformatBlocks(int,int,int)" "\0" __FILE__ ":" "346"));-
209 d->rehighlightPending = true;-
210 QTimer::singleShot(0, this, qFlagLocation("1""_q_delayedRehighlight()" "\0" __FILE__ ":" "348"));-
211 }
never executed: end of block
0
212}
never executed: end of block
0
213-
214-
215-
216-
217-
218QTextDocument *QSyntaxHighlighter::document() const-
219{-
220 const QSyntaxHighlighterPrivate * const d = d_func();-
221 return
never executed: return d->doc;
d->doc;
never executed: return d->doc;
0
222}-
223void QSyntaxHighlighter::rehighlight()-
224{-
225 QSyntaxHighlighterPrivate * const d = d_func();-
226 if (!d->doc
!d->docDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
227 return;
never executed: return;
0
228-
229 QTextCursor cursor(d->doc);-
230 d->rehighlight(cursor, QTextCursor::End);-
231}
never executed: end of block
0
232void QSyntaxHighlighter::rehighlightBlock(const QTextBlock &block)-
233{-
234 QSyntaxHighlighterPrivate * const d = d_func();-
235 if (!d->doc
!d->docDescription
TRUEnever evaluated
FALSEnever evaluated
|| !block.isValid()
!block.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
|| block.document() != d->doc
block.document() != d->docDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
236 return;
never executed: return;
0
237-
238 const bool rehighlightPending = d->rehighlightPending;-
239-
240 QTextCursor cursor(block);-
241 d->rehighlight(cursor, QTextCursor::EndOfBlock);-
242-
243 if (rehighlightPending
rehighlightPendingDescription
TRUEnever evaluated
FALSEnever evaluated
)
0
244 d->rehighlightPending = rehighlightPending;
never executed: d->rehighlightPending = rehighlightPending;
0
245}
never executed: end of block
0
246void QSyntaxHighlighter::setFormat(int start, int count, const QTextCharFormat &format)-
247{-
248 QSyntaxHighlighterPrivate * const d = d_func();-
249 if (start < 0
start < 0Description
TRUEnever evaluated
FALSEnever evaluated
|| start >= d->formatChanges.count()
start >= d->fo...hanges.count()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
250 return;
never executed: return;
0
251-
252 const int end = qMin(start + count, d->formatChanges.count());-
253 for (int i = start; i < end
i < endDescription
TRUEnever evaluated
FALSEnever evaluated
; ++i)
0
254 d->formatChanges[i] = format;
never executed: d->formatChanges[i] = format;
0
255}
never executed: end of block
0
256void QSyntaxHighlighter::setFormat(int start, int count, const QColor &color)-
257{-
258 QTextCharFormat format;-
259 format.setForeground(color);-
260 setFormat(start, count, format);-
261}
never executed: end of block
0
262void QSyntaxHighlighter::setFormat(int start, int count, const QFont &font)-
263{-
264 QTextCharFormat format;-
265 format.setFont(font);-
266 setFormat(start, count, format);-
267}
never executed: end of block
0
268-
269-
270-
271-
272-
273-
274-
275QTextCharFormat QSyntaxHighlighter::format(int pos) const-
276{-
277 const QSyntaxHighlighterPrivate * const d = d_func();-
278 if (pos < 0
pos < 0Description
TRUEnever evaluated
FALSEnever evaluated
|| pos >= d->formatChanges.count()
pos >= d->form...hanges.count()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
279 return
never executed: return QTextCharFormat();
QTextCharFormat();
never executed: return QTextCharFormat();
0
280 return
never executed: return d->formatChanges.at(pos);
d->formatChanges.at(pos);
never executed: return d->formatChanges.at(pos);
0
281}-
282int QSyntaxHighlighter::previousBlockState() const-
283{-
284 const QSyntaxHighlighterPrivate * const d = d_func();-
285 if (!d->currentBlock.isValid()
!d->currentBlock.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
286 return
never executed: return -1;
-1;
never executed: return -1;
0
287-
288 const QTextBlock previous = d->currentBlock.previous();-
289 if (!previous.isValid()
!previous.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
290 return
never executed: return -1;
-1;
never executed: return -1;
0
291-
292 return
never executed: return previous.userState();
previous.userState();
never executed: return previous.userState();
0
293}-
294-
295-
296-
297-
298-
299int QSyntaxHighlighter::currentBlockState() const-
300{-
301 const QSyntaxHighlighterPrivate * const d = d_func();-
302 if (!d->currentBlock.isValid()
!d->currentBlock.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
303 return
never executed: return -1;
-1;
never executed: return -1;
0
304-
305 return
never executed: return d->currentBlock.userState();
d->currentBlock.userState();
never executed: return d->currentBlock.userState();
0
306}-
307-
308-
309-
310-
311-
312-
313void QSyntaxHighlighter::setCurrentBlockState(int newState)-
314{-
315 QSyntaxHighlighterPrivate * const d = d_func();-
316 if (!d->currentBlock.isValid()
!d->currentBlock.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
317 return;
never executed: return;
0
318-
319 d->currentBlock.setUserState(newState);-
320}
never executed: end of block
0
321void QSyntaxHighlighter::setCurrentBlockUserData(QTextBlockUserData *data)-
322{-
323 QSyntaxHighlighterPrivate * const d = d_func();-
324 if (!d->currentBlock.isValid()
!d->currentBlock.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
325 return;
never executed: return;
0
326-
327 d->currentBlock.setUserData(data);-
328}
never executed: end of block
0
329-
330-
331-
332-
333-
334-
335-
336QTextBlockUserData *QSyntaxHighlighter::currentBlockUserData() const-
337{-
338 const QSyntaxHighlighterPrivate * const d = d_func();-
339 if (!d->currentBlock.isValid()
!d->currentBlock.isValid()Description
TRUEnever evaluated
FALSEnever evaluated
)
0
340 return
never executed: return 0;
0;
never executed: return 0;
0
341-
342 return
never executed: return d->currentBlock.userData();
d->currentBlock.userData();
never executed: return d->currentBlock.userData();
0
343}-
344-
345-
346-
347-
348-
349-
350QTextBlock QSyntaxHighlighter::currentBlock() const-
351{-
352 const QSyntaxHighlighterPrivate * const d = d_func();-
353 return
never executed: return d->currentBlock;
d->currentBlock;
never executed: return d->currentBlock;
0
354}-
355-
356-
357-
Switch to Source codePreprocessed file

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