Commit 849d6e21 authored by Осовик Полина Александровна's avatar Осовик Полина Александровна
Browse files

Upload New File

parent c94c1ab9
No related merge requests found
Showing with 75 additions and 0 deletions
+75 -0
#include "highlighter.h"
Highlighter::Highlighter(QFile *xml, QString lang, QTextDocument *parent) : QSyntaxHighlighter(parent)
{
HighlightingRule rule;
xml->open(QIODevice::ReadOnly);
QXmlStreamReader sr(xml);
bool valid = false;
do
{
sr.readNext();
if(sr.name() == "lang" && sr.isStartElement() && !valid)
{
qDebug() << sr.attributes().at(0).value().toString();
if(sr.attributes().at(0).value().toString() == lang)
{
qDebug() << sr.attributes().at(0).value().toString();
valid = true;
}
}
else if(sr.name() == "lang" && sr.isEndElement() && valid)
{
valid = false;
}
else if(!valid)
{
continue;
}
else if(sr.name() == "regexp" && sr.isStartElement())
{
sr.readNext();
rule.pattern.setPattern(sr.text().toString());
qDebug() << "REGEXP: " + sr.text();
}
else if(sr.name() == "color" && sr.isStartElement())
{
sr.readNext();
rule.format.setForeground(QColor(sr.text().toString()));
highlightingRules.append(rule);
qDebug() << "COLOR: " + sr.text();
}
} while(!sr.atEnd());
qDebug() << highlightingRules.count();
}
void Highlighter::highlightBlock(const QString &text)
{
foreach (const HighlightingRule &rule, highlightingRules) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
setCurrentBlockState(0);
QTextCharFormat multiLineCommentFormat;
multiLineCommentFormat.setForeground(Qt::red);
int startIndex = 0;
if (previousBlockState() != 1)
startIndex = text.indexOf(QRegularExpression("/\\*"));
while (startIndex >= 0) {
QRegularExpressionMatch match = QRegularExpression("\\*/").match(text, startIndex);
int endIndex = match.capturedStart();
int commentLength = 0;
if (endIndex == -1) {
setCurrentBlockState(1);
commentLength = text.length() - startIndex;
} else {
commentLength = endIndex - startIndex
+ match.capturedLength();
}
setFormat(startIndex, commentLength, multiLineCommentFormat);
startIndex = text.indexOf(QRegularExpression("/\\*"), startIndex + commentLength);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment