forked from vnotex/vnote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvcodeblockhighlighthelper.cpp
More file actions
307 lines (261 loc) · 9.6 KB
/
vcodeblockhighlighthelper.cpp
File metadata and controls
307 lines (261 loc) · 9.6 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
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
298
299
300
301
302
303
304
305
306
307
#include "vcodeblockhighlighthelper.h"
#include <QDebug>
#include <QStringList>
#include "vdocument.h"
#include "utils/vutils.h"
#include "pegmarkdownhighlighter.h"
VCodeBlockHighlightHelper::VCodeBlockHighlightHelper(PegMarkdownHighlighter *p_highlighter,
VDocument *p_vdoc,
MarkdownConverterType p_type)
: QObject(p_highlighter),
m_highlighter(p_highlighter),
m_vdocument(p_vdoc),
m_type(p_type),
m_timeStamp(0)
{
connect(m_highlighter, &PegMarkdownHighlighter::codeBlocksUpdated,
this, &VCodeBlockHighlightHelper::handleCodeBlocksUpdated);
connect(m_vdocument, &VDocument::textHighlighted,
this, &VCodeBlockHighlightHelper::handleTextHighlightResult);
// Web side is ready for code block highlight.
connect(m_vdocument, &VDocument::readyToHighlightText,
m_highlighter, &PegMarkdownHighlighter::updateHighlight);
}
QString VCodeBlockHighlightHelper::unindentCodeBlock(const QString &p_text)
{
if (p_text.isEmpty()) {
return p_text;
}
QStringList lines = p_text.split('\n');
Q_ASSERT(lines[0].trimmed().startsWith("```") || lines[0].trimmed().startsWith("~~~"));
Q_ASSERT(lines.size() > 1);
QRegExp regExp("(^\\s*)");
regExp.indexIn(lines[0]);
V_ASSERT(regExp.captureCount() == 1);
int nrSpaces = regExp.capturedTexts()[1].size();
if (nrSpaces == 0) {
return p_text;
}
QString res = lines[0].right(lines[0].size() - nrSpaces);
for (int i = 1; i < lines.size(); ++i) {
const QString &line = lines[i];
int idx = 0;
while (idx < nrSpaces && idx < line.size() && line[idx].isSpace()) {
++idx;
}
res = res + "\n" + line.right(line.size() - idx);
}
return res;
}
void VCodeBlockHighlightHelper::handleCodeBlocksUpdated(TimeStamp p_timeStamp,
const QVector<VCodeBlock> &p_codeBlocks)
{
if (!m_vdocument->isReadyToHighlight()) {
// Immediately return empty results.
QVector<HLUnitPos> emptyRes;
for (int i = 0; i < p_codeBlocks.size(); ++i) {
updateHighlightResults(p_timeStamp, 0, emptyRes);
}
return;
}
m_timeStamp = p_timeStamp;
m_codeBlocks = p_codeBlocks;
for (int i = 0; i < m_codeBlocks.size(); ++i) {
const VCodeBlock &block = m_codeBlocks[i];
auto it = m_cache.find(block.m_text);
if (it != m_cache.end()) {
// Hit cache.
qDebug() << "code block highlight hit cache" << p_timeStamp << i;
it.value().m_timeStamp = p_timeStamp;
updateHighlightResults(p_timeStamp, block.m_startPos, it.value().m_units);
} else {
QString unindentedText = unindentCodeBlock(block.m_text);
m_vdocument->highlightTextAsync(unindentedText, i, p_timeStamp);
}
}
}
void VCodeBlockHighlightHelper::handleTextHighlightResult(const QString &p_html,
int p_id,
unsigned long long p_timeStamp)
{
// Abandon obsolete result.
if (m_timeStamp != p_timeStamp) {
return;
}
parseHighlightResult(p_timeStamp, p_id, p_html);
}
static void revertEscapedHtml(QString &p_html)
{
p_html.replace(">", ">").replace("<", "<").replace("&", "&");
}
// Search @p_tokenStr in @p_text from p_index. Spaces after `\n` will not make
// a difference in the match. The matched range will be returned as
// [@p_start, @p_end]. Update @p_index to @p_end + 1.
// Set @p_start and @p_end to -1 to indicate mismatch.
static void matchTokenRelaxed(const QString &p_text, const QString &p_tokenStr,
int &p_index, int &p_start, int &p_end)
{
QString regStr = QRegExp::escape(p_tokenStr);
// Remove the leading spaces.
int nonSpaceIdx = 0;
while (nonSpaceIdx < regStr.size() && regStr[nonSpaceIdx].isSpace()) {
++nonSpaceIdx;
}
if (nonSpaceIdx > 0 && nonSpaceIdx < regStr.size()) {
regStr.remove(0, nonSpaceIdx);
}
// Do not replace the ending '\n'.
regStr.replace(QRegExp("\n(?!$)"), "\\s+");
QRegExp regExp(regStr);
p_start = p_text.indexOf(regExp, p_index);
if (p_start == -1) {
p_end = -1;
return;
}
p_end = p_start + regExp.matchedLength() - 1;
p_index = p_end + 1;
}
// For now, we could only handle code blocks outside the list.
void VCodeBlockHighlightHelper::parseHighlightResult(TimeStamp p_timeStamp,
int p_idx,
const QString &p_html)
{
const VCodeBlock &block = m_codeBlocks.at(p_idx);
int startPos = block.m_startPos;
QString text = block.m_text;
QVector<HLUnitPos> hlUnits;
bool failed = true;
QXmlStreamReader xml(p_html);
// Must have a fenced line at the front.
// textIndex is the start index in the code block text to search for.
int textIndex = text.indexOf('\n');
if (textIndex == -1) {
goto exit;
}
++textIndex;
if (xml.readNextStartElement()) {
if (xml.name() != "pre") {
goto exit;
}
if (!xml.readNextStartElement()) {
goto exit;
}
if (xml.name() != "code") {
goto exit;
}
while (xml.readNext()) {
if (xml.isCharacters()) {
// Revert the HTML escape to match.
QString tokenStr = xml.text().toString();
revertEscapedHtml(tokenStr);
int start, end;
matchTokenRelaxed(text, tokenStr, textIndex, start, end);
if (start == -1) {
failed = true;
goto exit;
}
} else if (xml.isStartElement()) {
if (xml.name() != "span") {
failed = true;
goto exit;
}
if (!parseSpanElement(xml, text, textIndex, hlUnits)) {
failed = true;
goto exit;
}
} else if (xml.isEndElement()) {
if (xml.name() != "code" && xml.name() != "pre") {
failed = true;
} else {
failed = false;
}
goto exit;
} else {
failed = true;
goto exit;
}
}
}
exit:
// Pass result back to highlighter.
// Abandon obsolete result.
if (m_timeStamp != p_timeStamp) {
return;
}
if (xml.hasError() || failed) {
qWarning() << "fail to parse highlighted result"
<< "stamp:" << p_timeStamp << "index:" << p_idx << p_html;
hlUnits.clear();
}
// Add it to cache.
addToHighlightCache(text, p_timeStamp, hlUnits);
updateHighlightResults(p_timeStamp, startPos, hlUnits);
}
void VCodeBlockHighlightHelper::updateHighlightResults(TimeStamp p_timeStamp,
int p_startPos,
QVector<HLUnitPos> p_units)
{
for (int i = 0; i < p_units.size(); ++i) {
p_units[i].m_position += p_startPos;
}
// We need to call this function anyway to trigger the rehighlight.
m_highlighter->setCodeBlockHighlights(p_timeStamp, p_units);
}
bool VCodeBlockHighlightHelper::parseSpanElement(QXmlStreamReader &p_xml,
const QString &p_text,
int &p_index,
QVector<HLUnitPos> &p_units)
{
int unitStart = p_index;
QString style = p_xml.attributes().value("class").toString();
while (p_xml.readNext()) {
if (p_xml.isCharacters()) {
// Revert the HTML escape to match.
QString tokenStr = p_xml.text().toString();
revertEscapedHtml(tokenStr);
int start, end;
matchTokenRelaxed(p_text, tokenStr, p_index, start, end);
if (start == -1) {
return false;
}
} else if (p_xml.isStartElement()) {
if (p_xml.name() != "span") {
return false;
}
// Sub-span.
if (!parseSpanElement(p_xml, p_text, p_index, p_units)) {
return false;
}
} else if (p_xml.isEndElement()) {
if (p_xml.name() != "span") {
return false;
}
// Got a complete span. Use relative position here.
HLUnitPos unit(unitStart, p_index - unitStart, style);
p_units.append(unit);
return true;
} else {
return false;
}
}
return false;
}
void VCodeBlockHighlightHelper::addToHighlightCache(const QString &p_text,
TimeStamp p_timeStamp,
const QVector<HLUnitPos> &p_units)
{
const int c_maxEntries = 100;
const TimeStamp c_maxTimeStampSpan = 3;
if (m_cache.size() >= c_maxEntries) {
// Remove the oldest one.
TimeStamp ts = p_timeStamp - c_maxTimeStampSpan;
for (auto it = m_cache.begin(); it != m_cache.end();) {
if (it.value().m_timeStamp < ts) {
it = m_cache.erase(it);
} else {
++it;
}
}
}
m_cache.insert(p_text, HLResult(p_timeStamp, p_units));
}