insilmaril@435: /**************************************************************************** insilmaril@435: ** insilmaril@435: ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved. insilmaril@435: ** insilmaril@435: ** This file is part of the example classes of the Qt Toolkit. insilmaril@435: ** insilmaril@435: ** This file may be used under the terms of the GNU General Public insilmaril@435: ** License version 2.0 as published by the Free Software Foundation insilmaril@435: ** and appearing in the file LICENSE.GPL included in the packaging of insilmaril@435: ** this file. Please review the following information to ensure GNU insilmaril@435: ** General Public Licensing requirements will be met: insilmaril@435: ** http://www.trolltech.com/products/qt/opensource.html insilmaril@435: ** insilmaril@435: ** If you are unsure which license is appropriate for your use, please insilmaril@435: ** review the following information: insilmaril@435: ** http://www.trolltech.com/products/qt/licensing.html or contact the insilmaril@435: ** sales department at sales@trolltech.com. insilmaril@435: ** insilmaril@435: ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE insilmaril@435: ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. insilmaril@435: ** insilmaril@435: ****************************************************************************/ insilmaril@435: insilmaril@435: // highlighting rules have been adapted by Uwe Drechsel to match vym syntax insilmaril@435: insilmaril@435: insilmaril@435: #include insilmaril@435: insilmaril@435: #include "highlighter.h" insilmaril@435: insilmaril@435: Highlighter::Highlighter(QTextDocument *parent) insilmaril@435: : QSyntaxHighlighter(parent) insilmaril@435: { insilmaril@435: HighlightingRule rule; insilmaril@435: insilmaril@435: keywordFormat.setForeground(Qt::darkBlue); insilmaril@435: keywordFormat.setFontWeight(QFont::Bold); insilmaril@435: QStringList keywordPatterns; insilmaril@435: keywordPatterns << "\\baddBranch\\b" << "\\baddBranchBefore\\b" insilmaril@435: << "\\baddMapInsert\\b" << "\\baddMapReplace\\b" insilmaril@435: << "\\bcolorBranch\\b" << "\\bcolorSubtree\\b" insilmaril@435: << "\\bcut\\b" << "\\bdelete\\b" insilmaril@435: << "\\bdeletepKeepChilds\\b" << "\\bdeletepChilds\\b" insilmaril@435: << "\\blinkTo\\b" << "\\bloadImage\\b" insilmaril@435: << "\\bmoveBranchUp\\b" << "\\bmoveBranchDown\\b" insilmaril@435: << "\\bmove\\b" << "\\bmoveRel\\b" insilmaril@435: << "\\bpaste\\b" insilmaril@435: << "\\bsaveImage\\b" insilmaril@435: << "\\bscroll\\b" insilmaril@435: << "\\bselect\\b" << "\\bselectLastBranch\\b" << "\\bselectLastImage\\b" insilmaril@435: << "\\bsetMapAuthor\\b" insilmaril@435: << "\\bsetMapComment\\b" insilmaril@435: << "\\bsetMapBackgroundColor\\b" insilmaril@435: << "\\bsetMapDefLinkColor\\b" insilmaril@435: << "\\bsetMapDefLinkStyle\\b" insilmaril@435: << "\\bsetHeading\\b" insilmaril@435: << "\\bsetHideExport\\b" insilmaril@435: << "\\bsetIncludeImagesHorizontally\\b" insilmaril@435: << "\\bsetIncludeImagesVertically\\b" insilmaril@435: << "\\bsetURL\\b" insilmaril@435: << "\\bsetVymLink\\b" insilmaril@435: << "\\bsetFlag\\b" insilmaril@435: << "\\bunscroll\\b" insilmaril@435: << "\\bunsetFlag\\b" insilmaril@435: ; insilmaril@435: foreach (QString pattern, keywordPatterns) { insilmaril@435: rule.pattern = QRegExp(pattern); insilmaril@435: rule.format = keywordFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: } insilmaril@435: insilmaril@435: // QT keywords insilmaril@435: /* insilmaril@435: classFormat.setFontWeight(QFont::Bold); insilmaril@435: classFormat.setForeground(Qt::darkMagenta); insilmaril@435: rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b"); insilmaril@435: rule.format = classFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: */ insilmaril@435: insilmaril@435: // Single line comments insilmaril@435: singleLineCommentFormat.setForeground(Qt::red); insilmaril@435: rule.pattern = QRegExp("#[^\n]*"); insilmaril@435: rule.format = singleLineCommentFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: insilmaril@435: // Single line comments insilmaril@435: multiLineCommentFormat.setForeground(Qt::red); insilmaril@435: commentStartExpression = QRegExp("/\\*"); insilmaril@435: commentEndExpression = QRegExp("\\*/"); insilmaril@435: insilmaril@435: // Quotations insilmaril@435: quotationFormat.setForeground(Qt::darkGreen); insilmaril@435: rule.pattern = QRegExp("\".*\""); insilmaril@435: rule.format = quotationFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: insilmaril@435: QStringList valuePatterns; insilmaril@435: valuePatterns << "\\btrue\\b" << "\\bfalse\\b"; insilmaril@435: foreach (QString pattern, valuePatterns) { insilmaril@435: rule.pattern = QRegExp(pattern); insilmaril@435: rule.format = quotationFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: } insilmaril@435: insilmaril@435: insilmaril@435: insilmaril@435: // Funtions insilmaril@435: /* insilmaril@435: functionFormat.setFontItalic(true); insilmaril@435: functionFormat.setForeground(Qt::blue); insilmaril@435: rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); insilmaril@435: rule.format = functionFormat; insilmaril@435: highlightingRules.append(rule); insilmaril@435: */ insilmaril@435: insilmaril@435: } insilmaril@435: insilmaril@435: void Highlighter::highlightBlock(const QString &text) insilmaril@435: { insilmaril@435: foreach (HighlightingRule rule, highlightingRules) { insilmaril@435: QRegExp expression(rule.pattern); insilmaril@435: int index = text.indexOf(expression); insilmaril@435: while (index >= 0) { insilmaril@435: int length = expression.matchedLength(); insilmaril@435: setFormat(index, length, rule.format); insilmaril@435: index = text.indexOf(expression, index + length); insilmaril@435: } insilmaril@435: } insilmaril@435: setCurrentBlockState(0); insilmaril@435: insilmaril@435: int startIndex = 0; insilmaril@435: if (previousBlockState() != 1) insilmaril@435: startIndex = text.indexOf(commentStartExpression); insilmaril@435: insilmaril@435: while (startIndex >= 0) { insilmaril@435: int endIndex = text.indexOf(commentEndExpression, startIndex); insilmaril@435: int commentLength; insilmaril@435: if (endIndex == -1) { insilmaril@435: setCurrentBlockState(1); insilmaril@435: commentLength = text.length() - startIndex; insilmaril@435: } else { insilmaril@435: commentLength = endIndex - startIndex insilmaril@435: + commentEndExpression.matchedLength(); insilmaril@435: } insilmaril@435: setFormat(startIndex, commentLength, multiLineCommentFormat); insilmaril@435: startIndex = text.indexOf(commentStartExpression, insilmaril@435: startIndex + commentLength); insilmaril@435: } insilmaril@435: }