qavrg 0.0.28
Classes | Public Member Functions | Protected Member Functions | Private Attributes
QavrgHighlighter Class Reference

#include <qavrghighlighter.h>

List of all members.

Classes

struct  HighlightingRule

Public Member Functions

 QavrgHighlighter (QTextDocument *parent=0)

Protected Member Functions

void highlightBlock (const QString &text)

Private Attributes

QVector< HighlightingRulehighlightingRules
QRegExp commentStartExpression
QRegExp commentEndExpression
QTextCharFormat keywordFormat
QTextCharFormat classFormat
QTextCharFormat singleLineCommentFormat
QTextCharFormat multiLineCommentFormat
QTextCharFormat quotationFormat
QTextCharFormat functionFormat

Detailed Description

Definition at line 11 of file qavrghighlighter.h.


Constructor & Destructor Documentation

QavrgHighlighter::QavrgHighlighter ( QTextDocument *  parent = 0)

Definition at line 5 of file qavrghighlighter.cpp.

References classFormat, commentEndExpression, commentStartExpression, QavrgHighlighter::HighlightingRule::format, functionFormat, highlightingRules, keywordFormat, multiLineCommentFormat, QavrgHighlighter::HighlightingRule::pattern, quotationFormat, and singleLineCommentFormat.

  : QSyntaxHighlighter(parent)
{
    HighlightingRule rule;

    keywordFormat.setForeground(Qt::darkBlue);
    keywordFormat.setFontWeight(QFont::Bold);
    QStringList keywordPatterns;
    keywordPatterns << "\\break\\b" << "\\bcase\\b" << "\\bcatch\\b"
                    << "\\bcontinue\\b" << "\\bdefault\\b" << "\\bdelete\\b"
                    << "\\bdo\\b" << "\\belse\\b" << "\\bfinally\\b"
                    << "\\bfor\\b" << "\\bfunction\\b" << "\\bif\\b"
                    << "\\bin\\b" << "\\binstanceof\\b" << "\\bnew\\b"
                    << "\\breturn\\b" << "\\bswitch\\b" << "\\bthis\\b"
                    << "\\bthrow\\b" << "\\btry\\b" << "\\btypeof\\b"
                    << "\\bvar\\b" << "\\btypedef\\b" << "\\btypename\\b"
                    << "\\bunion\\b" << "\\bvoid\\b" << "\\bwhile\\b"
                    << "\\bwith\\b"
                    << "\\babstract\\b" << "\\benum\\b"       << "\\bint\\b"       << "\\bshort\\b"
                    << "\\bboolean\\b"  << "\\bexport\\b"     << "\\binterface\\b" << "\\bstatic\\b"
                    << "\\bbyte\\b"     << "\\bextends\\b"    << "\\blong\\b"      << "\\bsuper\\b"
                    << "\\bchar\\b"     << "\\bfinal\\b"      << "\\bnative\\b"    << "\\bsynchronized\\b"
                    << "\\bclass\\b"    << "\\bfloat\\b"      << "\\bpackage\\b"   << "\\bthrows\\b"
                    << "\\bconst\\b"    << "\\bgoto\\b"       << "\\bprivate\\b"   << "\\btransient\\b"
                    << "\\bdebugger\\b" << "\\bimplements\\b" << "\\bprotected\\b" << "\\bvolatile\\b"
                    << "\\bdouble\\b"   << "\\bimport\\b"     << "\\bpublic\\b";

    foreach (QString pattern, keywordPatterns) {
        rule.pattern = QRegExp(pattern);
        rule.format = keywordFormat;
        highlightingRules.append(rule);
    }

    classFormat.setFontWeight(QFont::Bold);
    classFormat.setForeground(Qt::darkMagenta);
    rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    rule.format = classFormat;
    highlightingRules.append(rule);

    singleLineCommentFormat.setForeground(Qt::red);
    rule.pattern = QRegExp("//[^\n]*");
    rule.format = singleLineCommentFormat;
    highlightingRules.append(rule);

    multiLineCommentFormat.setForeground(Qt::red);

    quotationFormat.setForeground(Qt::darkGreen);
    rule.pattern = QRegExp("\".*\"");
    rule.format = quotationFormat;
    highlightingRules.append(rule);

    functionFormat.setFontItalic(true);
    functionFormat.setForeground(Qt::blue);
    rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
    rule.format = functionFormat;
    highlightingRules.append(rule);

    commentStartExpression = QRegExp("/\\*");
    commentEndExpression = QRegExp("\\*/");
}

Member Function Documentation

void QavrgHighlighter::highlightBlock ( const QString &  text) [protected]

Definition at line 66 of file qavrghighlighter.cpp.

References commentEndExpression, commentStartExpression, QavrgHighlighter::HighlightingRule::format, highlightingRules, multiLineCommentFormat, and QavrgHighlighter::HighlightingRule::pattern.

{
    foreach (HighlightingRule rule, highlightingRules) {
        QRegExp expression(rule.pattern);
        int index = text.indexOf(expression);
        while (index >= 0) {
            int length = expression.matchedLength();
            setFormat(index, length, rule.format);
            index = text.indexOf(expression, index + length);
        }
    }
    setCurrentBlockState(0);

    int startIndex = 0;
    if (previousBlockState() != 1)
        startIndex = text.indexOf(commentStartExpression);

    while (startIndex >= 0) {
        int endIndex = text.indexOf(commentEndExpression, startIndex);
        int commentLength;
        if (endIndex == -1) {
            setCurrentBlockState(1);
            commentLength = text.length() - startIndex;
        } else {
            commentLength = endIndex - startIndex
                            + commentEndExpression.matchedLength();
        }
        setFormat(startIndex, commentLength, multiLineCommentFormat);
        startIndex = text.indexOf(commentStartExpression,
                                                startIndex + commentLength);
    }
}

Member Data Documentation

QTextCharFormat QavrgHighlighter::classFormat [private]

Definition at line 33 of file qavrghighlighter.h.

Referenced by QavrgHighlighter().

Definition at line 30 of file qavrghighlighter.h.

Referenced by highlightBlock(), and QavrgHighlighter().

Definition at line 29 of file qavrghighlighter.h.

Referenced by highlightBlock(), and QavrgHighlighter().

QTextCharFormat QavrgHighlighter::functionFormat [private]

Definition at line 37 of file qavrghighlighter.h.

Referenced by QavrgHighlighter().

Definition at line 27 of file qavrghighlighter.h.

Referenced by highlightBlock(), and QavrgHighlighter().

QTextCharFormat QavrgHighlighter::keywordFormat [private]

Definition at line 32 of file qavrghighlighter.h.

Referenced by QavrgHighlighter().

QTextCharFormat QavrgHighlighter::multiLineCommentFormat [private]

Definition at line 35 of file qavrghighlighter.h.

Referenced by highlightBlock(), and QavrgHighlighter().

QTextCharFormat QavrgHighlighter::quotationFormat [private]

Definition at line 36 of file qavrghighlighter.h.

Referenced by QavrgHighlighter().

QTextCharFormat QavrgHighlighter::singleLineCommentFormat [private]

Definition at line 34 of file qavrghighlighter.h.

Referenced by QavrgHighlighter().


The documentation for this class was generated from the following files: