Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
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 | | - |
55 | QT_BEGIN_NAMESPACE | - |
56 | | - |
57 | class QSyntaxHighlighterPrivate : public QObjectPrivate | - |
58 | { | - |
59 | Q_DECLARE_PUBLIC(QSyntaxHighlighter) | - |
60 | public: | - |
61 | inline QSyntaxHighlighterPrivate() | - |
62 | : rehighlightPending(false), inReformatBlocks(false) | - |
63 | {} | - |
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 | } | - |
80 | | - |
81 | inline void _q_delayedRehighlight() { | - |
82 | if (!rehighlightPending) | - |
83 | return; | - |
84 | rehighlightPending = false; | - |
85 | q_func()->rehighlight(); | - |
86 | } | - |
87 | | - |
88 | void applyFormatChanges(); | - |
89 | QVector<QTextCharFormat> formatChanges; | - |
90 | QTextBlock currentBlock; | - |
91 | bool rehighlightPending; | - |
92 | bool inReformatBlocks; | - |
93 | }; | - |
94 | | - |
95 | void 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) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
107 | QVector<auto isOutsidePreeditArea = [=](const QTextLayout::FormatRange >::Iterator&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(); | - |
| while(), ranges.end(), | |
112 | isOutsidePreeditArea); | - |
113 | ifTRUE | never evaluated | FALSE | never evaluated |
(it != ranges.end()) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
| if (it->start >= preeditAreaStartTRUE | never evaluated | FALSE | never evaluated |
| |
| && it->start + it->length <= preeditAreaStart + preeditAreaLength) {TRUE | never evaluated | FALSE | never evaluated |
| |
| ++it;TRUE | never evaluated | FALSE | never evaluated |
| |
| } elseTRUE | never evaluated | FALSE | never evaluated |
{TRUE | never evaluated | FALSE | never evaluated |
| |
114 | it =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 TRUE | never evaluated | FALSE | never 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()) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
124 | QTextLayout::FormatRange r; | - |
125 | | - |
126 | while (i < formatChanges.count() && formatChanges.at(i) == r.format)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never evaluated |
| 0 |
127 | ++i; never executed: ++i; | 0 |
128 | | - |
129 | if (i == formatChanges.count())TRUE | never evaluated | FALSE | never 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)TRUE | never evaluated | FALSE | never evaluated |
TRUE | never evaluated | FALSE | never 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) {TRUE | never evaluated | FALSE | never evaluated |
| 0 |
142 | if (r.start >= preeditAreaStart)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
143 | r.start += preeditAreaLength; never executed: r.start += preeditAreaLength; | 0 |
144 | else if (r.start + r.length >= preeditAreaStart)TRUE | never evaluated | FALSE | never 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) {TRUE | never evaluated | FALSE | never 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 | | - |
158 | void QSyntaxHighlighterPrivate::_q_reformatBlocks(int from, int charsRemoved, int charsAdded) | - |
159 | { | - |
160 | if (!inReformatBlocks) | - |
161 | reformatBlocks(from, charsRemoved, charsAdded); | - |
162 | } | - |
163 | | - |
164 | void QSyntaxHighlighterPrivate::reformatBlocks(int from, int charsRemoved, int charsAdded) | - |
165 | { | - |
166 | rehighlightPending = false; | - |
167 | | - |
168 | QTextBlock block = doc->findBlock(from); | - |
169 | if (!block.isValid()) | - |
170 | return; | - |
171 | | - |
172 | int endPosition; | - |
173 | QTextBlock lastBlock = doc->findBlock(from + charsAdded + (charsRemoved > 0 ? 1 : 0)); | - |
174 | if (lastBlock.isValid()) | - |
175 | endPosition = lastBlock.position() + lastBlock.length(); | - |
176 | else | - |
177 | endPosition = doc->docHandle()->length(); | - |
178 | | - |
179 | bool forceHighlightOfNextBlock = false; | - |
180 | | - |
181 | while (block.isValid() && (block.position() < endPosition || forceHighlightOfNextBlock)) { | - |
182 | const int stateBeforeHighlight = block.userState(); | - |
183 | | - |
184 | reformatBlock(block); | - |
185 | | - |
186 | forceHighlightOfNextBlock = (block.userState() != stateBeforeHighlight); | - |
187 | | - |
188 | block = block.next(); | - |
189 | } | - |
190 | | - |
191 | formatChanges.clear(); | - |
192 | } | - |
193 | | - |
194 | void 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 | } | - |
208 | | - |
209 | | - |
210 | | - |
211 | | - |
212 | | - |
213 | | - |
214 | | - |
215 | | - |
216 | | - |
217 | | - |
218 | | - |
219 | | - |
220 | | - |
221 | | - |
222 | | - |
223 | | - |
224 | | - |
225 | | - |
226 | | - |
227 | | - |
228 | | - |
229 | | - |
230 | | - |
231 | | - |
232 | | - |
233 | | - |
234 | | - |
235 | | - |
236 | | - |
237 | | - |
238 | | - |
239 | | - |
240 | | - |
241 | | - |
242 | | - |
243 | | - |
244 | | - |
245 | | - |
246 | | - |
247 | | - |
248 | | - |
249 | | - |
250 | | - |
251 | | - |
252 | | - |
253 | | - |
254 | | - |
255 | | - |
256 | | - |
257 | | - |
258 | | - |
259 | | - |
260 | | - |
261 | | - |
262 | | - |
263 | | - |
264 | | - |
265 | | - |
266 | | - |
267 | | - |
268 | | - |
269 | | - |
270 | | - |
271 | | - |
272 | | - |
273 | | - |
274 | | - |
275 | | - |
276 | | - |
277 | | - |
278 | | - |
279 | | - |
280 | | - |
281 | | - |
282 | | - |
283 | | - |
284 | | - |
285 | | - |
286 | | - |
287 | | - |
288 | | - |
289 | | - |
290 | | - |
291 | | - |
292 | | - |
293 | | - |
294 | | - |
295 | | - |
296 | | - |
297 | QSyntaxHighlighter::QSyntaxHighlighter(QObject *parent) | - |
298 | : QObject(*new QSyntaxHighlighterPrivate, parent) | - |
299 | { | - |
300 | if (parent->inherits("QTextEdit")) { | - |
301 | QTextDocument *doc = parent->property("document").value<QTextDocument *>(); | - |
302 | if (doc) | - |
303 | setDocument(doc); | - |
304 | } | - |
305 | } | - |
306 | | - |
307 | | - |
308 | | - |
309 | | - |
310 | | - |
311 | | - |
312 | QSyntaxHighlighter::QSyntaxHighlighter(QTextDocument *parent) | - |
313 | : QObject(*new QSyntaxHighlighterPrivate, parent) | - |
314 | { | - |
315 | setDocument(parent); | - |
316 | } | - |
317 | | - |
318 | | - |
319 | | - |
320 | | - |
321 | QSyntaxHighlighter::~QSyntaxHighlighter() | - |
322 | { | - |
323 | setDocument(0); | - |
324 | } | - |
325 | | - |
326 | | - |
327 | | - |
328 | | - |
329 | | - |
330 | void QSyntaxHighlighter::setDocument(QTextDocument *doc) | - |
331 | { | - |
332 | Q_D(QSyntaxHighlighter); | - |
333 | if (d->doc) { | - |
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()) | - |
340 | blk.layout()->clearFormats(); | - |
341 | cursor.endEditBlock(); | - |
342 | } | - |
343 | d->doc = doc; | - |
344 | if (d->doc) { | - |
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 | } | - |
350 | } | - |
351 | | - |
352 | | - |
353 | | - |
354 | | - |
355 | | - |
356 | QTextDocument *QSyntaxHighlighter::document() const | - |
357 | { | - |
358 | Q_D(const QSyntaxHighlighter); | - |
359 | return d->doc; | - |
360 | } | - |
361 | | - |
362 | | - |
363 | | - |
364 | | - |
365 | | - |
366 | | - |
367 | | - |
368 | | - |
369 | void QSyntaxHighlighter::rehighlight() | - |
370 | { | - |
371 | Q_D(QSyntaxHighlighter); | - |
372 | if (!d->doc) | - |
373 | return; | - |
374 | | - |
375 | QTextCursor cursor(d->doc); | - |
376 | d->rehighlight(cursor, QTextCursor::End); | - |
377 | } | - |
378 | | - |
379 | | - |
380 | | - |
381 | | - |
382 | | - |
383 | | - |
384 | | - |
385 | | - |
386 | void QSyntaxHighlighter::rehighlightBlock(const QTextBlock &block) | - |
387 | { | - |
388 | Q_D(QSyntaxHighlighter); | - |
389 | if (!d->doc || !block.isValid() || block.document() != d->doc) | - |
390 | return; | - |
391 | | - |
392 | const bool rehighlightPending = d->rehighlightPending; | - |
393 | | - |
394 | QTextCursor cursor(block); | - |
395 | d->rehighlight(cursor, QTextCursor::EndOfBlock); | - |
396 | | - |
397 | if (rehighlightPending) | - |
398 | d->rehighlightPending = rehighlightPending; | - |
399 | } | - |
400 | | - |
401 | | - |
402 | | - |
403 | | - |
404 | | - |
405 | | - |
406 | | - |
407 | | - |
408 | | - |
409 | | - |
410 | | - |
411 | | - |
412 | | - |
413 | | - |
414 | | - |
415 | | - |
416 | | - |
417 | | - |
418 | | - |
419 | | - |
420 | | - |
421 | | - |
422 | | - |
423 | | - |
424 | | - |
425 | | - |
426 | | - |
427 | | - |
428 | | - |
429 | | - |
430 | | - |
431 | | - |
432 | | - |
433 | | - |
434 | | - |
435 | | - |
436 | | - |
437 | | - |
438 | | - |
439 | | - |
440 | | - |
441 | | - |
442 | | - |
443 | | - |
444 | | - |
445 | | - |
446 | | - |
447 | | - |
448 | | - |
449 | | - |
450 | | - |
451 | | - |
452 | | - |
453 | | - |
454 | | - |
455 | | - |
456 | | - |
457 | | - |
458 | | - |
459 | | - |
460 | void QSyntaxHighlighter::setFormat(int start, int count, const QTextCharFormat &format) | - |
461 | { | - |
462 | Q_D(QSyntaxHighlighter); | - |
463 | if (start < 0 || start >= d->formatChanges.count()) | - |
464 | return; | - |
465 | | - |
466 | const int end = qMin(start + count, d->formatChanges.count()); | - |
467 | for (int i = start; i < end; ++i) | - |
468 | d->formatChanges[i] = format; | - |
469 | } | - |
470 | | - |
471 | | - |
472 | | - |
473 | | - |
474 | | - |
475 | | - |
476 | | - |
477 | | - |
478 | | - |
479 | | - |
480 | | - |
481 | | - |
482 | void QSyntaxHighlighter::setFormat(int start, int count, const QColor &color) | - |
483 | { | - |
484 | QTextCharFormat format; | - |
485 | format.setForeground(color); | - |
486 | setFormat(start, count, format); | - |
487 | } | - |
488 | | - |
489 | | - |
490 | | - |
491 | | - |
492 | | - |
493 | | - |
494 | | - |
495 | | - |
496 | | - |
497 | | - |
498 | | - |
499 | | - |
500 | void QSyntaxHighlighter::setFormat(int start, int count, const QFont &font) | - |
501 | { | - |
502 | QTextCharFormat format; | - |
503 | format.setFont(font); | - |
504 | setFormat(start, count, format); | - |
505 | } | - |
506 | | - |
507 | | - |
508 | | - |
509 | | - |
510 | | - |
511 | | - |
512 | | - |
513 | QTextCharFormat QSyntaxHighlighter::format(int pos) const | - |
514 | { | - |
515 | Q_D(const QSyntaxHighlighter); | - |
516 | if (pos < 0 || pos >= d->formatChanges.count()) | - |
517 | return QTextCharFormat(); | - |
518 | return d->formatChanges.at(pos); | - |
519 | } | - |
520 | | - |
521 | | - |
522 | | - |
523 | | - |
524 | | - |
525 | | - |
526 | | - |
527 | | - |
528 | int QSyntaxHighlighter::previousBlockState() const | - |
529 | { | - |
530 | Q_D(const QSyntaxHighlighter); | - |
531 | if (!d->currentBlock.isValid()) | - |
532 | return -1; | - |
533 | | - |
534 | const QTextBlock previous = d->currentBlock.previous(); | - |
535 | if (!previous.isValid()) | - |
536 | return -1; | - |
537 | | - |
538 | return previous.userState(); | - |
539 | } | - |
540 | | - |
541 | | - |
542 | | - |
543 | | - |
544 | | - |
545 | int QSyntaxHighlighter::currentBlockState() const | - |
546 | { | - |
547 | Q_D(const QSyntaxHighlighter); | - |
548 | if (!d->currentBlock.isValid()) | - |
549 | return -1; | - |
550 | | - |
551 | return d->currentBlock.userState(); | - |
552 | } | - |
553 | | - |
554 | | - |
555 | | - |
556 | | - |
557 | | - |
558 | | - |
559 | void QSyntaxHighlighter::setCurrentBlockState(int newState) | - |
560 | { | - |
561 | Q_D(QSyntaxHighlighter); | - |
562 | if (!d->currentBlock.isValid()) | - |
563 | return; | - |
564 | | - |
565 | d->currentBlock.setUserState(newState); | - |
566 | } | - |
567 | | - |
568 | | - |
569 | | - |
570 | | - |
571 | | - |
572 | | - |
573 | | - |
574 | | - |
575 | | - |
576 | | - |
577 | | - |
578 | | - |
579 | | - |
580 | | - |
581 | | - |
582 | | - |
583 | | - |
584 | | - |
585 | | - |
586 | | - |
587 | | - |
588 | | - |
589 | | - |
590 | | - |
591 | | - |
592 | | - |
593 | | - |
594 | | - |
595 | | - |
596 | | - |
597 | | - |
598 | | - |
599 | | - |
600 | | - |
601 | | - |
602 | void QSyntaxHighlighter::setCurrentBlockUserData(QTextBlockUserData *data) | - |
603 | { | - |
604 | Q_D(QSyntaxHighlighter); | - |
605 | if (!d->currentBlock.isValid()) | - |
606 | return; | - |
607 | | - |
608 | d->currentBlock.setUserData(data); | - |
609 | } | - |
610 | | - |
611 | | - |
612 | | - |
613 | | - |
614 | | - |
615 | | - |
616 | | - |
617 | QTextBlockUserData *QSyntaxHighlighter::currentBlockUserData() const | - |
618 | { | - |
619 | Q_D(const QSyntaxHighlighter); | - |
620 | if (!d->currentBlock.isValid()) | - |
621 | return 0; | - |
622 | | - |
623 | return d->currentBlock.userData(); | - |
624 | } | - |
625 | | - |
626 | | - |
627 | | - |
628 | | - |
629 | | - |
630 | | - |
631 | QTextBlock QSyntaxHighlighter::currentBlock() const | - |
632 | { | - |
633 | Q_D(const QSyntaxHighlighter); | - |
634 | return d->currentBlock; | - |
635 | } | - |
636 | | - |
637 | QT_END_NAMESPACE | - |
638 | | - |
639 | #include "moc_qsyntaxhighlighter.cpp" | - |
640 | | - |
641 | #endif // QT_NO_SYNTAXHIGHLIGHTER | - |
| | |