File indexing completed on 2024-05-12 17:11:42
0001 /*************************************************************************** 0002 * Copyright (C) 2014 by Renaud Guezennec * 0003 * http://www.rolisteam.org/ * 0004 * * 0005 * This file is part of rcse * 0006 * * 0007 * rcse is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published by * 0009 * the Free Software Foundation; either version 2 of the License, or * 0010 * (at your option) any later version. * 0011 * * 0012 * rcse is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0015 * GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License * 0018 * along with this program; if not, write to the * 0019 * Free Software Foundation, Inc., * 0020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0021 ***************************************************************************/ 0022 #include "qmlhighlighter.h" 0023 0024 #include <QFile> 0025 #include <QTextStream> 0026 0027 QmlHighlighter::QmlHighlighter(QTextDocument* parent) : QSyntaxHighlighter(parent) 0028 { 0029 HighlightingRule rule; 0030 0031 keywordFormat.setForeground(Qt::darkYellow); 0032 rule.format= keywordFormat; 0033 loadDictionary(":/resources/dictionaries/keywords.txt", rule); 0034 0035 keywordFormat.setForeground(Qt::blue); 0036 rule.format= keywordFormat; 0037 loadDictionary(":/resources/dictionaries/javascript.txt", rule); 0038 0039 propertyFormat.setForeground(Qt::darkRed); 0040 rule.pattern= QRegularExpression("\\s*[0-9a-zA-Z_\\.]*\\s*:"); 0041 rule.format= propertyFormat; 0042 m_highlightingRules.append(rule); 0043 0044 itemFormat.setForeground(QColor(Qt::red)); 0045 // itemFormat.setFontWeight(QFont::Medium); 0046 rule.pattern= QRegularExpression("^\\s*[A-Z]\\w+"); 0047 rule.format= itemFormat; 0048 m_highlightingRules.append(rule); 0049 0050 commentFormat.setForeground(QColor(Qt::darkBlue).lighter()); 0051 commentFormat.setFontItalic(true); 0052 rule.pattern= QRegularExpression("\\/\\/.*"); 0053 rule.format= commentFormat; 0054 m_highlightingRules.append(rule); 0055 0056 cppObjectFormat.setForeground(QColor(Qt::blue).lighter()); 0057 cppObjectFormat.setFontItalic(true); 0058 rule.pattern= QRegularExpression("_[A-Za-z]\\w+"); 0059 rule.format= cppObjectFormat; 0060 m_highlightingRules.append(rule); 0061 0062 quotationFormat.setForeground(Qt::darkGreen); 0063 rule.pattern= QRegularExpression("['\"].*['\"]"); 0064 rule.format= quotationFormat; 0065 m_highlightingRules.append(rule); 0066 0067 lookupFormat.setForeground(Qt::magenta); 0068 rule.pattern= QRegularExpression("\\s\\d+\\s"); 0069 rule.format= lookupFormat; 0070 m_highlightingRules.append(rule); 0071 } 0072 0073 void QmlHighlighter::highlightBlock(const QString& text) 0074 { 0075 for(const HighlightingRule& rule : m_highlightingRules) 0076 { 0077 QRegularExpression expression(rule.pattern); 0078 QRegularExpressionMatch match; // = expression.match(text,0); 0079 int index= text.indexOf(expression, 0, &match); 0080 while(index >= 0) 0081 { 0082 int length= match.capturedLength(); 0083 setFormat(index, length, rule.format); 0084 index= text.indexOf(expression, index + length, &match); 0085 } 0086 } 0087 setCurrentBlockState(0); 0088 } 0089 0090 void QmlHighlighter::loadDictionary(QString filepath, HighlightingRule& rule) 0091 { 0092 QFile file(filepath); 0093 if(file.open(QIODevice::ReadOnly)) 0094 { 0095 QTextStream textStream(&file); 0096 while(!textStream.atEnd()) 0097 { 0098 rule.pattern= QRegularExpression(QStringLiteral("%1\\s").arg(textStream.readLine().trimmed())); 0099 m_highlightingRules.append(rule); 0100 } 0101 } 0102 }