From 849d6e213cb9cba078be4ea2964558d1e5c855a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D1=81=D0=BE=D0=B2=D0=B8=D0=BA=20=D0=9F=D0=BE=D0=BB?= =?UTF-8?q?=D0=B8=D0=BD=D0=B0=20=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD?= =?UTF-8?q?=D0=B4=D1=80=D0=BE=D0=B2=D0=BD=D0=B0?= Date: Sat, 16 Jan 2021 15:43:31 +0000 Subject: [PATCH] Upload New File --- highlighter.cpp | 75 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 highlighter.cpp diff --git a/highlighter.cpp b/highlighter.cpp new file mode 100644 index 0000000..0589460 --- /dev/null +++ b/highlighter.cpp @@ -0,0 +1,75 @@ +#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); + } +} -- GitLab