source: s10k/CommonLibs/jsonhighlighter/highlighter.cpp @ 1096

Last change on this file since 1096 was 1096, checked in by s10k, 17 months ago

Added zlib, quazip, basicxmlsyntaxhighlighter, conditionalsemaphore and linenumberdisplay libraries. zlib and quazip are pre-compiled, but you can compile them yourself, just delete the dll files (or equivalent binary files to your OS)

File size: 1.2 KB
Line 
1#include "highlighter.h"
2#include <QRegExp>
3#include <QTextCharFormat>
4#include <QTextDocument>
5
6Highlighter::Highlighter(QTextDocument *parent)
7    : QSyntaxHighlighter(parent)
8{
9   HighlightingRule rule;
10
11   //numbers
12   rule.pattern = QRegExp("([-0-9.]+)(?!([^\"]*\"[\\s]*\\:))");
13   rule.format.setForeground(QColor(255,192,85));
14   rules.append(rule);
15
16   //key
17   rule.pattern = QRegExp ("(\"[^\"]*\")");
18   rule.format.setForeground(QColor(145,145,192));
19   rules.append(rule);
20
21   //value
22   rule.pattern = QRegExp(":\\s*([\"](?:[^\"])*[\"])");
23   rule.format.setForeground(QColor(145,145,192));
24   rules.append(rule);
25
26   //reserved words
27   rule.pattern = QRegExp("(true|false|null)(?!\"[^\"]*\")");
28   rule.format.setForeground(QColor(0,0,255));
29   rules.append(rule);
30}
31
32void Highlighter::highlightBlock(const QString &text)
33{
34    for (const HighlightingRule &rule : rules) {
35        QRegExp expression(rule.pattern);
36        int index = expression.indexIn(text);
37
38        while (index >= 0) {
39            index = expression.pos(1);
40            int length = expression.cap(1).length();
41            setFormat(index, length, rule.format);
42            index = expression.indexIn(text, index + length);
43        }
44    }
45}
Note: See TracBrowser for help on using the repository browser.