-
Notifications
You must be signed in to change notification settings - Fork 7
/
syntaxhighlighter.cpp
132 lines (113 loc) · 3.21 KB
/
syntaxhighlighter.cpp
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright 2010 Anders Bakken
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "syntaxhighlighter.h"
#include "textedit.h"
#include "textdocument.h"
#include "textlayout_p.h"
#include "textdocument_p.h"
SyntaxHighlighter::SyntaxHighlighter(QObject *parent)
: QObject(parent), d(new Private)
{
}
SyntaxHighlighter::SyntaxHighlighter(TextEdit *parent)
: QObject(parent), d(new Private)
{
if (parent) {
parent->addSyntaxHighlighter(this);
}
}
SyntaxHighlighter::~SyntaxHighlighter()
{
delete d;
}
void SyntaxHighlighter::setTextEdit(TextEdit *doc)
{
Q_ASSERT(doc);
doc->addSyntaxHighlighter(this);
}
TextEdit *SyntaxHighlighter::textEdit() const
{
return d->textEdit;
}
TextDocument * SyntaxHighlighter::document() const
{
return d->textEdit ? d->textEdit->document() : 0;
}
void SyntaxHighlighter::rehighlight()
{
if (d->textEdit) {
Q_ASSERT(d->textLayout);
d->textLayout->layoutDirty = true;
d->textEdit->viewport()->update();
}
}
void SyntaxHighlighter::setFormat(int start, int count, const QTextCharFormat &format)
{
ASSUME(d->textEdit);
Q_ASSERT(start >= 0);
Q_ASSERT(start + count <= d->currentBlock.size());
d->formatRanges.append(QTextLayout::FormatRange());
QTextLayout::FormatRange &range = d->formatRanges.last();
range.start = start;
range.length = count;
range.format = format;
}
void SyntaxHighlighter::setFormat(int start, int count, const QColor &color)
{
QTextCharFormat format;
format.setForeground(color);
setFormat(start, count, format);
}
void SyntaxHighlighter::setFormat(int start, int count, const QFont &font)
{
QTextCharFormat format;
format.setFont(font);
setFormat(start, count, format);
}
QTextCharFormat SyntaxHighlighter::format(int pos) const
{
QTextCharFormat ret;
foreach(const QTextLayout::FormatRange &range, d->formatRanges) {
if (range.start <= pos && range.start + range.length > pos) {
ret.merge(range.format);
} else if (range.start > pos) {
break;
}
}
return ret;
}
int SyntaxHighlighter::previousBlockState() const
{
return d->previousBlockState;
}
int SyntaxHighlighter::currentBlockState() const
{
return d->currentBlockState;
}
void SyntaxHighlighter::setCurrentBlockState(int s)
{
d->previousBlockState = d->currentBlockState;
d->currentBlockState = s; // ### These don't entirely follow QSyntaxHighlighter's behavior
}
int SyntaxHighlighter::currentBlockPosition() const
{
return d->currentBlockPosition;
}
QTextBlockFormat SyntaxHighlighter::blockFormat() const
{
return d->blockFormat;
}
void SyntaxHighlighter::setBlockFormat(const QTextBlockFormat &format)
{
d->blockFormat = format;
}