qavrg 0.0.28
qavrghighlighter.cpp
Go to the documentation of this file.
00001 #include <QtGui>
00002 
00003 #include "qavrghighlighter.h"
00004 
00005 QavrgHighlighter::QavrgHighlighter(QTextDocument *parent)
00006   : QSyntaxHighlighter(parent)
00007 {
00008     HighlightingRule rule;
00009 
00010     keywordFormat.setForeground(Qt::darkBlue);
00011     keywordFormat.setFontWeight(QFont::Bold);
00012     QStringList keywordPatterns;
00013     keywordPatterns << "\\break\\b" << "\\bcase\\b" << "\\bcatch\\b"
00014                     << "\\bcontinue\\b" << "\\bdefault\\b" << "\\bdelete\\b"
00015                     << "\\bdo\\b" << "\\belse\\b" << "\\bfinally\\b"
00016                     << "\\bfor\\b" << "\\bfunction\\b" << "\\bif\\b"
00017                     << "\\bin\\b" << "\\binstanceof\\b" << "\\bnew\\b"
00018                     << "\\breturn\\b" << "\\bswitch\\b" << "\\bthis\\b"
00019                     << "\\bthrow\\b" << "\\btry\\b" << "\\btypeof\\b"
00020                     << "\\bvar\\b" << "\\btypedef\\b" << "\\btypename\\b"
00021                     << "\\bunion\\b" << "\\bvoid\\b" << "\\bwhile\\b"
00022                     << "\\bwith\\b"
00023                     << "\\babstract\\b" << "\\benum\\b"       << "\\bint\\b"       << "\\bshort\\b"
00024                     << "\\bboolean\\b"  << "\\bexport\\b"     << "\\binterface\\b" << "\\bstatic\\b"
00025                     << "\\bbyte\\b"     << "\\bextends\\b"    << "\\blong\\b"      << "\\bsuper\\b"
00026                     << "\\bchar\\b"     << "\\bfinal\\b"      << "\\bnative\\b"    << "\\bsynchronized\\b"
00027                     << "\\bclass\\b"    << "\\bfloat\\b"      << "\\bpackage\\b"   << "\\bthrows\\b"
00028                     << "\\bconst\\b"    << "\\bgoto\\b"       << "\\bprivate\\b"   << "\\btransient\\b"
00029                     << "\\bdebugger\\b" << "\\bimplements\\b" << "\\bprotected\\b" << "\\bvolatile\\b"
00030                     << "\\bdouble\\b"   << "\\bimport\\b"     << "\\bpublic\\b";
00031 
00032     foreach (QString pattern, keywordPatterns) {
00033         rule.pattern = QRegExp(pattern);
00034         rule.format = keywordFormat;
00035         highlightingRules.append(rule);
00036     }
00037 
00038     classFormat.setFontWeight(QFont::Bold);
00039     classFormat.setForeground(Qt::darkMagenta);
00040     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
00041     rule.format = classFormat;
00042     highlightingRules.append(rule);
00043 
00044     singleLineCommentFormat.setForeground(Qt::red);
00045     rule.pattern = QRegExp("//[^\n]*");
00046     rule.format = singleLineCommentFormat;
00047     highlightingRules.append(rule);
00048 
00049     multiLineCommentFormat.setForeground(Qt::red);
00050 
00051     quotationFormat.setForeground(Qt::darkGreen);
00052     rule.pattern = QRegExp("\".*\"");
00053     rule.format = quotationFormat;
00054     highlightingRules.append(rule);
00055 
00056     functionFormat.setFontItalic(true);
00057     functionFormat.setForeground(Qt::blue);
00058     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
00059     rule.format = functionFormat;
00060     highlightingRules.append(rule);
00061 
00062     commentStartExpression = QRegExp("/\\*");
00063     commentEndExpression = QRegExp("\\*/");
00064 }
00065 
00066 void QavrgHighlighter::highlightBlock(const QString &text)
00067 {
00068     foreach (HighlightingRule rule, highlightingRules) {
00069         QRegExp expression(rule.pattern);
00070         int index = text.indexOf(expression);
00071         while (index >= 0) {
00072             int length = expression.matchedLength();
00073             setFormat(index, length, rule.format);
00074             index = text.indexOf(expression, index + length);
00075         }
00076     }
00077     setCurrentBlockState(0);
00078 
00079     int startIndex = 0;
00080     if (previousBlockState() != 1)
00081         startIndex = text.indexOf(commentStartExpression);
00082 
00083     while (startIndex >= 0) {
00084         int endIndex = text.indexOf(commentEndExpression, startIndex);
00085         int commentLength;
00086         if (endIndex == -1) {
00087             setCurrentBlockState(1);
00088             commentLength = text.length() - startIndex;
00089         } else {
00090             commentLength = endIndex - startIndex
00091                             + commentEndExpression.matchedLength();
00092         }
00093         setFormat(startIndex, commentLength, multiLineCommentFormat);
00094         startIndex = text.indexOf(commentStartExpression,
00095                                                 startIndex + commentLength);
00096     }
00097 }