From 2f24a5d59805ad807c83ab7bf4f8f5f506bb406a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=95=D0=B3=D0=BE=D1=80=20=D0=93=D0=B0=D0=BB=D0=BA=D0=B8?= =?UTF-8?q?=D0=BD?= Date: Wed, 13 Jan 2021 19:23:58 +0000 Subject: [PATCH] Add new file --- highlighter.cpp | 133 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 highlighter.cpp diff --git a/highlighter.cpp b/highlighter.cpp new file mode 100644 index 0000000..0458beb --- /dev/null +++ b/highlighter.cpp @@ -0,0 +1,133 @@ +#include "highlighter.h" + +#include + +Highlighter::Highlighter(const QString _filename, QObject *parent) : + QSyntaxHighlighter(parent) +{ + xml_filename=_filename; + QFile file(xml_filename); + QStringList keywords; + + if (file.open(QIODevice::ReadOnly)) + { + HighlightingRule my_format; + QDomDocument doc; + doc.setContent(&file); + auto languages = doc.elementsByTagName("language"); + for (int i = 0; i < languages.length(); ++i) + { + auto ext = languages.at(i).toElement(); + auto file_extension = QRegExp(ext.attribute("extentions")); + HighlightLanguage data; + + QDomNodeList list = ext.elementsByTagName("keyword"); + int size = list.size(); + for (int i = 0; i < size; ++i) + keywords.append(list.at(i).toElement().text()); + + data.keywordFormat.setForeground(Qt::darkBlue); + data.keywordFormat.setFontWeight(QFont::Bold); + + foreach (const QString& _pattern, keywords) + { + my_format.pattern = QRegExp("\\b"+_pattern+"\\b"); + my_format.format = data.keywordFormat; + data.highlightingRules.append(my_format); + } + keywords.clear(); + list = ext.elementsByTagName("command"); + size = list.size(); + data.preprocFormat.setForeground(Qt::darkBlue); + + for (int i = 0; i < size; ++i) + keywords.append(list.at(i).toElement().text()); + + foreach(const QString& _pattern, keywords) + { + my_format.pattern = QRegExp(_pattern); + my_format.format = data.preprocFormat; + data.highlightingRules.append(my_format); + } + + data.classFormat.setForeground(Qt::darkMagenta); + data.classFormat.setFontWeight(QFont::Bold); + my_format.pattern = QRegExp("\\bQ[A-Za-z]+\\b"); + my_format.format = data.classFormat; + data.highlightingRules.append(my_format); + data.multiLineCommentFormat.setForeground(Qt::darkGreen); + QDomNode beg_com = ext.elementsByTagName("multiline_comment").at(0); + data.commentStartExpression=QRegExp(beg_com.toElement().attribute("begin")); + data.commentEndExpression=QRegExp(beg_com.toElement().attribute("end")); + + data.singleLineCommentFormat.setForeground(Qt::darkGreen); + QDomNode single_com = ext.elementsByTagName("single_comment").at(0); + my_format.pattern = QRegExp(single_com.toElement().attribute("expression")); + my_format.format = data.singleLineCommentFormat; + data.highlightingRules.append(my_format); + + data.quotationFormat.setForeground(Qt::darkGreen); + my_format.pattern = QRegExp("\".*\""); + my_format.format = data.quotationFormat; + data.highlightingRules.append(my_format); + + data.functionFormat.setFontItalic(true); + data.functionFormat.setForeground(Qt::blue); + my_format.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); + my_format.format = data.functionFormat; + data.highlightingRules.append(my_format); + + languagesContainer.insert(file_extension, data); + } + } +} + +void Highlighter::highlightBlock(const QString& text) +{ + auto &data = languagesContainer.value(current_extension); + + foreach (const HighlightingRule& rule_, data.highlightingRules) + { + QRegExp expression(rule_.pattern); + int index = expression.indexIn(text); + while (index >= 0) + { + int len = expression.matchedLength(); + setFormat(index, len, rule_.format); + index = expression.indexIn(text, index+len); + } + } + setCurrentBlockState(0); + int begin = 0; + if (previousBlockState() != 1) + begin = data.commentStartExpression.indexIn(text); + + while (begin >= 1) + { + int end = data.commentEndExpression.indexIn(text); + int com_len; + if (end == -1) + { + setCurrentBlockState(1); + com_len = text.length() - begin + data.commentEndExpression.matchedLength(); + } + else + com_len = end - begin + data.commentEndExpression.matchedLength(); + + setFormat(begin, com_len, data.multiLineCommentFormat); + begin = data.commentStartExpression.indexIn(text, begin + com_len); + } +} + +bool Highlighter::setExtension(const QString &_filename) +{ + for (int i = 0; i < languagesContainer.size(); ++i) + { + if (languagesContainer.keys().at(i).indexIn(_filename) != -1) + { + current_extension = languagesContainer.keys().at(i); + return true; + } + } + return false; +} \ No newline at end of file -- GitLab