highlighter.cpp
changeset 442 dfbc371b7280
child 447 72afe12da1c8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/highlighter.cpp	Sat Mar 31 09:28:27 2007 +0000
     1.3 @@ -0,0 +1,150 @@
     1.4 +/****************************************************************************
     1.5 +**
     1.6 +** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
     1.7 +**
     1.8 +** This file is part of the example classes of the Qt Toolkit.
     1.9 +**
    1.10 +** This file may be used under the terms of the GNU General Public
    1.11 +** License version 2.0 as published by the Free Software Foundation
    1.12 +** and appearing in the file LICENSE.GPL included in the packaging of
    1.13 +** this file.  Please review the following information to ensure GNU
    1.14 +** General Public Licensing requirements will be met:
    1.15 +** http://www.trolltech.com/products/qt/opensource.html
    1.16 +**
    1.17 +** If you are unsure which license is appropriate for your use, please
    1.18 +** review the following information:
    1.19 +** http://www.trolltech.com/products/qt/licensing.html or contact the
    1.20 +** sales department at sales@trolltech.com.
    1.21 +**
    1.22 +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    1.23 +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    1.24 +**
    1.25 +****************************************************************************/
    1.26 +
    1.27 +// highlighting rules have been adapted by Uwe Drechsel to match vym syntax
    1.28 +
    1.29 +
    1.30 +#include <QtGui>
    1.31 +
    1.32 +#include "highlighter.h"
    1.33 +
    1.34 +Highlighter::Highlighter(QTextDocument *parent)
    1.35 +    : QSyntaxHighlighter(parent)
    1.36 +{
    1.37 +    HighlightingRule rule;
    1.38 +
    1.39 +    keywordFormat.setForeground(Qt::darkBlue);
    1.40 +    keywordFormat.setFontWeight(QFont::Bold);
    1.41 +    QStringList keywordPatterns;
    1.42 +    keywordPatterns << "\\baddBranch\\b" << "\\baddBranchBefore\\b" 
    1.43 +                    << "\\baddMapInsert\\b" << "\\baddMapReplace\\b"
    1.44 +                    << "\\bcolorBranch\\b" << "\\bcolorSubtree\\b"
    1.45 +                    << "\\bcut\\b" << "\\bdelete\\b" 
    1.46 +					<< "\\bdeletepKeepChilds\\b" << "\\bdeletepChilds\\b"
    1.47 +					<< "\\blinkTo\\b" << "\\bloadImage\\b"
    1.48 +					<< "\\bmoveBranchUp\\b" << "\\bmoveBranchDown\\b"
    1.49 +					<< "\\bmove\\b" << "\\bmoveRel\\b"
    1.50 +					<< "\\bpaste\\b" 
    1.51 +					<< "\\bsaveImage\\b" 
    1.52 +					<< "\\bscroll\\b" 
    1.53 +					<< "\\bselect\\b" << "\\bselectLastBranch\\b" << "\\bselectLastImage\\b"
    1.54 +					<< "\\bsetMapAuthor\\b" 
    1.55 +					<< "\\bsetMapComment\\b" 
    1.56 +					<< "\\bsetMapBackgroundColor\\b" 
    1.57 +					<< "\\bsetMapDefLinkColor\\b" 
    1.58 +					<< "\\bsetMapDefLinkStyle\\b" 
    1.59 +					<< "\\bsetHeading\\b" 
    1.60 +					<< "\\bsetHideExport\\b" 
    1.61 +					<< "\\bsetIncludeImagesHorizontally\\b" 
    1.62 +					<< "\\bsetIncludeImagesVertically\\b" 
    1.63 +					<< "\\bsetURL\\b" 
    1.64 +					<< "\\bsetVymLink\\b" 
    1.65 +					<< "\\bsetFlag\\b" 
    1.66 +					<< "\\bunscroll\\b" 
    1.67 +					<< "\\bunsetFlag\\b" 
    1.68 +					;
    1.69 +    foreach (QString pattern, keywordPatterns) {
    1.70 +        rule.pattern = QRegExp(pattern);
    1.71 +        rule.format = keywordFormat;
    1.72 +        highlightingRules.append(rule);
    1.73 +    }
    1.74 +
    1.75 +	// QT keywords
    1.76 +	/*
    1.77 +    classFormat.setFontWeight(QFont::Bold);
    1.78 +    classFormat.setForeground(Qt::darkMagenta);
    1.79 +    rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    1.80 +    rule.format = classFormat;
    1.81 +    highlightingRules.append(rule);
    1.82 +	*/
    1.83 +
    1.84 +	// Single line comments
    1.85 +    singleLineCommentFormat.setForeground(Qt::red);
    1.86 +    rule.pattern = QRegExp("#[^\n]*");
    1.87 +    rule.format = singleLineCommentFormat;
    1.88 +    highlightingRules.append(rule);
    1.89 +
    1.90 +	// Single line comments
    1.91 +    multiLineCommentFormat.setForeground(Qt::red);
    1.92 +    commentStartExpression = QRegExp("/\\*");
    1.93 +    commentEndExpression = QRegExp("\\*/");
    1.94 +
    1.95 +	// Quotations
    1.96 +    quotationFormat.setForeground(Qt::darkGreen);
    1.97 +    rule.pattern = QRegExp("\".*\"");
    1.98 +    rule.format = quotationFormat;
    1.99 +    highlightingRules.append(rule);
   1.100 +
   1.101 +    QStringList valuePatterns;
   1.102 +    valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   1.103 +    foreach (QString pattern, valuePatterns) {
   1.104 +        rule.pattern = QRegExp(pattern);
   1.105 +        rule.format = quotationFormat;
   1.106 +        highlightingRules.append(rule);
   1.107 +    }
   1.108 +
   1.109 +
   1.110 +
   1.111 +	// Funtions
   1.112 +	/*
   1.113 +    functionFormat.setFontItalic(true);
   1.114 +    functionFormat.setForeground(Qt::blue);
   1.115 +    rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   1.116 +    rule.format = functionFormat;
   1.117 +    highlightingRules.append(rule);
   1.118 +	*/
   1.119 +
   1.120 +}
   1.121 +
   1.122 +void Highlighter::highlightBlock(const QString &text)
   1.123 +{
   1.124 +    foreach (HighlightingRule rule, highlightingRules) {
   1.125 +        QRegExp expression(rule.pattern);
   1.126 +        int index = text.indexOf(expression);
   1.127 +        while (index >= 0) {
   1.128 +            int length = expression.matchedLength();
   1.129 +            setFormat(index, length, rule.format);
   1.130 +            index = text.indexOf(expression, index + length);
   1.131 +        }
   1.132 +    }
   1.133 +    setCurrentBlockState(0);
   1.134 +
   1.135 +    int startIndex = 0;
   1.136 +    if (previousBlockState() != 1)
   1.137 +        startIndex = text.indexOf(commentStartExpression);
   1.138 +
   1.139 +    while (startIndex >= 0) {
   1.140 +        int endIndex = text.indexOf(commentEndExpression, startIndex);
   1.141 +        int commentLength;
   1.142 +        if (endIndex == -1) {
   1.143 +            setCurrentBlockState(1);
   1.144 +            commentLength = text.length() - startIndex;
   1.145 +        } else {
   1.146 +            commentLength = endIndex - startIndex
   1.147 +                            + commentEndExpression.matchedLength();
   1.148 +        }
   1.149 +        setFormat(startIndex, commentLength, multiLineCommentFormat);
   1.150 +        startIndex = text.indexOf(commentStartExpression,
   1.151 +                                                startIndex + commentLength);
   1.152 +    }
   1.153 +}