-
Notifications
You must be signed in to change notification settings - Fork 34
/
tikzeditorhighlighter.cpp
187 lines (165 loc) · 7.75 KB
/
tikzeditorhighlighter.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
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
/***************************************************************************
* Copyright (C) 2007 by Florian Hackenberger *
* <[email protected]> *
* Copyright (C) 2007, 2011, 2012, 2014 by Glad Deschrijver *
* <[email protected]> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
#include <QDebug>
#include <QtCore/QSettings>
#include <QtWidgets/QApplication>
#include "tikzeditorhighlighter.h"
#include "tikzcommandinserter.h"
TikzHighlighter::TikzHighlighter(QTextDocument *parent) : QSyntaxHighlighter(parent) { }
TikzHighlighter::~TikzHighlighter() { }
void TikzHighlighter::setHighlightingRules(const QVector<HighlightingRule> &highlightingRules)
{
m_highlightingRules << highlightingRules;
// add highlighting for environments and comments
HighlightingRule rule;
QStringList highlightTypeNames = getHighlightTypeNames();
const int currentIndex = highlightTypeNames.size() - 2;
// environments
QStringList keywordPatterns;
keywordPatterns << QLatin1String("\\\\begin\\{[^\\}]*\\}")
<< QLatin1String("\\\\end\\{[^\\}]*\\}");
for (const auto &pattern : keywordPatterns) {
rule.type = highlightTypeNames.at(currentIndex);
rule.pattern = QRegExp(pattern);
rule.isRegExp = true;
m_highlightingRules.append(rule);
}
// comments
rule.type = highlightTypeNames.at(currentIndex + 1);
rule.pattern = QRegExp(QLatin1String("%[^\n]*"));
rule.isRegExp = true;
m_highlightingRules.append(rule);
// m_formatList = getDefaultHighlightFormats();
}
/***************************************************************************/
QMap<QString, QTextCharFormat> TikzHighlighter::getDefaultHighlightFormats()
{
QMap<QString, QTextCharFormat> formatList = TikzCommandInserter::getDefaultHighlightFormats();
QStringList highlightTypeNames = getHighlightTypeNames();
const int currentIndex = highlightTypeNames.size() - 2;
// format for environments
QTextCharFormat keywordFormat;
keywordFormat.setForeground(Qt::darkBlue);
keywordFormat.setFont(qApp->font());
keywordFormat.setFontWeight(QFont::Bold);
formatList[highlightTypeNames.at(currentIndex)] = keywordFormat;
// format for comments
QTextCharFormat commentFormat;
commentFormat.setForeground(Qt::gray);
commentFormat.setFont(qApp->font());
commentFormat.setFontWeight(QFont::Normal);
formatList[highlightTypeNames.at(currentIndex + 1)] = commentFormat;
return formatList;
}
QStringList TikzHighlighter::getTranslatedHighlightTypeNames()
{
QStringList translatedHighlightTypeNames =
TikzCommandInserter::getTranslatedHighlightTypeNames();
translatedHighlightTypeNames << tr("Environments") << tr("Comments");
return translatedHighlightTypeNames;
}
QStringList TikzHighlighter::getHighlightTypeNames()
{
QStringList highlightTypeNames = TikzCommandInserter::getHighlightTypeNames();
highlightTypeNames << QLatin1String("Environments") << QLatin1String("Comments");
return highlightTypeNames;
}
/***************************************************************************/
void TikzHighlighter::applySettings()
{
QSettings settings;
settings.beginGroup(QLatin1String("Highlighting"));
const bool customHighlighting = settings.value(QLatin1String("Customize"), true).toBool();
m_formatList.clear();
m_formatList = getDefaultHighlightFormats();
if (customHighlighting) {
const int numOfRules = settings.value(QLatin1String("Number"), 0).toInt();
for (int i = 0; i < numOfRules; ++i) {
const QString name = settings.value(QLatin1String("Item") + QString::number(i)
+ QLatin1String("/Name"))
.toString();
const QString colorName = settings.value(QLatin1String("Item") + QString::number(i)
+ QLatin1String("/Color"))
.toString();
const QString fontName = settings.value(QLatin1String("Item") + QString::number(i)
+ QLatin1String("/Font"))
.toString();
QFont font;
font.fromString(fontName);
QTextCharFormat format;
format.setForeground(QBrush(QColor(colorName)));
format.setFont(font);
m_formatList[name] = format;
}
}
settings.endGroup();
}
namespace {
int indexOf(const QString &text, const QString &tag, int startPos = 0)
{
// valgrind --tool=callgrind --zero-before="MainWindow::loadUrl(Url const&)" gives a small win
// for this function over "text.indexOf(tag, startPos);"
const int textLength = text.length();
const int tagLength = tag.length();
for (int i = startPos, j = 0; i < textLength && j < tagLength; ++i, ++j) {
if (text.at(i) != tag.at(j)) {
j = -1;
continue;
}
if (j == tagLength - 1)
return i - tagLength + 1;
}
return -1;
}
} // anonymous namespace
void TikzHighlighter::highlightBlock(const QString &text)
{
// Try each highlighting pattern and apply formatting if it matches
// Having the outer loop loop over the highlighting rules and the inner loop over the text is
// much faster than conversely
for (const auto &rule : m_highlightingRules) {
if (!rule.isRegExp) // match the insertion string
{
const int length = rule.matchString.length();
// int index = text.indexOf(rule.matchString);
int index = indexOf(text, rule.matchString);
while (index >= 0) {
if (index == 0
|| text.at(index - 1)
!= QLatin1Char('\\')) // avoid matching e.g. "node" as an option if in
// reality "\node" as a command is written
setFormat(index, length, m_formatList[rule.type]);
// index = text.indexOf(rule.matchString, index +
// length);
index = indexOf(text, rule.matchString, index + length);
}
} else // match the pattern
{
int index = rule.pattern.indexIn(text);
while (index >= 0) {
const int length = rule.pattern.matchedLength();
if (index == 0 || text.at(index - 1) != QLatin1Char('\\'))
setFormat(index, length, m_formatList[rule.type]);
index = rule.pattern.indexIn(text, index + length);
}
}
}
setCurrentBlockState(0);
}