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