From 64f22688fd6579ae2bcd6c87e8f9be098336de26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=BA=D0=B0=D1=82=D0=B5=D1=80=D0=B8=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=A1=D0=BF=D0=BE=D1=80=D0=BE=D0=B2=D0=B0?= Date: Thu, 14 Jan 2021 14:01:54 +0000 Subject: [PATCH] Add new file --- text_lighter.cpp | 134 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 134 insertions(+) create mode 100644 text_lighter.cpp diff --git a/text_lighter.cpp b/text_lighter.cpp new file mode 100644 index 0000000..7606b9b --- /dev/null +++ b/text_lighter.cpp @@ -0,0 +1,134 @@ +#include "text_lighter.h" +#include + +Lighter_class::Lighter_class(QTextDocument *parent) + : QSyntaxHighlighter(parent), + _isRightExt(false) +{} + +Lighter_class::Lighter_class(QTextDocument *parent, const QString& ext) + : QSyntaxHighlighter(parent), + _isRightExt(false), + _currentExt(ext) +{ + _multiLineCommentFormat.setForeground(Qt::red); + + _commentStartExpression = QRegularExpression(QStringLiteral("/\\*")); + _commentEndExpression = QRegularExpression(QStringLiteral("\\*/")); + +} + + +void Lighter_class::highlightBlock(const QString &text) +{ + qDebug() << text; + for (const HighlightingRule &rule : qAsConst(_highlightingRules)) { + QRegularExpressionMatchIterator matchIterator = rule._pattern.globalMatch(text); + while (matchIterator.hasNext()) { + QRegularExpressionMatch match = matchIterator.next(); + setFormat(match.capturedStart(), match.capturedLength(), rule._format); + } + } + setCurrentBlockState(0); + + int startIndex = 0; + if (previousBlockState() != 1) + startIndex = text.indexOf(_commentStartExpression); + + while (startIndex >= 0) { + QRegularExpressionMatch match = _commentEndExpression.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(_commentStartExpression, startIndex + commentLength); + } +} + +//parser + + +bool Lighter_class::startElement(const QString&, const QString &, const QString& tagName, const QXmlAttributes& attrs) +{ + qDebug() << tagName << attrs.count(); + if (tagName == "syntax") + { + qDebug() << "tagName == syntax"; + for(int i = 0; i < attrs.count(); i++) + { + if(attrs.localName(i) == "ext_list") + { + QStringList extList = attrs.value(i).split(" "); + if(extList.contains(_currentExt)) + _isRightExt = true; + else + _isRightExt = false; + + qDebug() << "ext_list" << attrs.value(i); + } + } + } + else if (tagName == "pattern") + { + if(_isRightExt) + { + qDebug() << "pattern"; + _pattern = QRegularExpression(attrs.value(0)); + qDebug() << "read regExp = " << _pattern; + } + } + else if (tagName == "format") + { + if(_isRightExt) + { + qDebug() << "format"; + _color = attrs.value(0); + _fontWeight = attrs.value(1).toInt(); + qDebug() << "read fontWeight = " << _fontWeight; + } + } + + return true; +} + +bool Lighter_class::characters(const QString& strText) +{ + _currentText = strText; + return true; +} + +bool Lighter_class::endElement(const QString &, const QString&, const QString &tagName) +{ + qDebug() << "In end element with tagName = " + tagName; + if (tagName == "rule") + { + if(_isRightExt) + { + HighlightingRule rule; + + rule._pattern = _pattern; + QTextCharFormat currFormat; + currFormat.setForeground(QColor(_color)); + currFormat.setFontWeight(_fontWeight); + rule._format = currFormat; + _highlightingRules.append(rule); + } + + } + qDebug() << "MOST USEFUL QDEBUG"; + return true; +} + +bool Lighter_class::fatalError(const QXmlParseException& exception) +{ + qDebug() << "Line:" << exception.lineNumber() + << ", Column:" << exception.columnNumber() + << ", Message:" << exception.message(); + return false; +} \ No newline at end of file -- GitLab