highlighter.cpp
author insilmaril
Wed, 21 Mar 2007 11:51:38 +0000
changeset 436 19e5907b7818
parent 435 bd71dfb2292c
child 447 72afe12da1c8
permissions -rw-r--r--
Slightly improved scripting abilities
     1 /****************************************************************************
     2 **
     3 ** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
     4 **
     5 ** This file is part of the example classes of the Qt Toolkit.
     6 **
     7 ** This file may be used under the terms of the GNU General Public
     8 ** License version 2.0 as published by the Free Software Foundation
     9 ** and appearing in the file LICENSE.GPL included in the packaging of
    10 ** this file.  Please review the following information to ensure GNU
    11 ** General Public Licensing requirements will be met:
    12 ** http://www.trolltech.com/products/qt/opensource.html
    13 **
    14 ** If you are unsure which license is appropriate for your use, please
    15 ** review the following information:
    16 ** http://www.trolltech.com/products/qt/licensing.html or contact the
    17 ** sales department at sales@trolltech.com.
    18 **
    19 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    20 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    21 **
    22 ****************************************************************************/
    23 
    24 // highlighting rules have been adapted by Uwe Drechsel to match vym syntax
    25 
    26 
    27 #include <QtGui>
    28 
    29 #include "highlighter.h"
    30 
    31 Highlighter::Highlighter(QTextDocument *parent)
    32     : QSyntaxHighlighter(parent)
    33 {
    34     HighlightingRule rule;
    35 
    36     keywordFormat.setForeground(Qt::darkBlue);
    37     keywordFormat.setFontWeight(QFont::Bold);
    38     QStringList keywordPatterns;
    39     keywordPatterns << "\\baddBranch\\b" << "\\baddBranchBefore\\b" 
    40                     << "\\baddMapInsert\\b" << "\\baddMapReplace\\b"
    41                     << "\\bcolorBranch\\b" << "\\bcolorSubtree\\b"
    42                     << "\\bcut\\b" << "\\bdelete\\b" 
    43 					<< "\\bdeletepKeepChilds\\b" << "\\bdeletepChilds\\b"
    44 					<< "\\blinkTo\\b" << "\\bloadImage\\b"
    45 					<< "\\bmoveBranchUp\\b" << "\\bmoveBranchDown\\b"
    46 					<< "\\bmove\\b" << "\\bmoveRel\\b"
    47 					<< "\\bpaste\\b" 
    48 					<< "\\bsaveImage\\b" 
    49 					<< "\\bscroll\\b" 
    50 					<< "\\bselect\\b" << "\\bselectLastBranch\\b" << "\\bselectLastImage\\b"
    51 					<< "\\bsetMapAuthor\\b" 
    52 					<< "\\bsetMapComment\\b" 
    53 					<< "\\bsetMapBackgroundColor\\b" 
    54 					<< "\\bsetMapDefLinkColor\\b" 
    55 					<< "\\bsetMapDefLinkStyle\\b" 
    56 					<< "\\bsetHeading\\b" 
    57 					<< "\\bsetHideExport\\b" 
    58 					<< "\\bsetIncludeImagesHorizontally\\b" 
    59 					<< "\\bsetIncludeImagesVertically\\b" 
    60 					<< "\\bsetURL\\b" 
    61 					<< "\\bsetVymLink\\b" 
    62 					<< "\\bsetFlag\\b" 
    63 					<< "\\bunscroll\\b" 
    64 					<< "\\bunsetFlag\\b" 
    65 					;
    66     foreach (QString pattern, keywordPatterns) {
    67         rule.pattern = QRegExp(pattern);
    68         rule.format = keywordFormat;
    69         highlightingRules.append(rule);
    70     }
    71 
    72 	// QT keywords
    73 	/*
    74     classFormat.setFontWeight(QFont::Bold);
    75     classFormat.setForeground(Qt::darkMagenta);
    76     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    77     rule.format = classFormat;
    78     highlightingRules.append(rule);
    79 	*/
    80 
    81 	// Single line comments
    82     singleLineCommentFormat.setForeground(Qt::red);
    83     rule.pattern = QRegExp("#[^\n]*");
    84     rule.format = singleLineCommentFormat;
    85     highlightingRules.append(rule);
    86 
    87 	// Single line comments
    88     multiLineCommentFormat.setForeground(Qt::red);
    89     commentStartExpression = QRegExp("/\\*");
    90     commentEndExpression = QRegExp("\\*/");
    91 
    92 	// Quotations
    93     quotationFormat.setForeground(Qt::darkGreen);
    94     rule.pattern = QRegExp("\".*\"");
    95     rule.format = quotationFormat;
    96     highlightingRules.append(rule);
    97 
    98     QStringList valuePatterns;
    99     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   100     foreach (QString pattern, valuePatterns) {
   101         rule.pattern = QRegExp(pattern);
   102         rule.format = quotationFormat;
   103         highlightingRules.append(rule);
   104     }
   105 
   106 
   107 
   108 	// Funtions
   109 	/*
   110     functionFormat.setFontItalic(true);
   111     functionFormat.setForeground(Qt::blue);
   112     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   113     rule.format = functionFormat;
   114     highlightingRules.append(rule);
   115 	*/
   116 
   117 }
   118 
   119 void Highlighter::highlightBlock(const QString &text)
   120 {
   121     foreach (HighlightingRule rule, highlightingRules) {
   122         QRegExp expression(rule.pattern);
   123         int index = text.indexOf(expression);
   124         while (index >= 0) {
   125             int length = expression.matchedLength();
   126             setFormat(index, length, rule.format);
   127             index = text.indexOf(expression, index + length);
   128         }
   129     }
   130     setCurrentBlockState(0);
   131 
   132     int startIndex = 0;
   133     if (previousBlockState() != 1)
   134         startIndex = text.indexOf(commentStartExpression);
   135 
   136     while (startIndex >= 0) {
   137         int endIndex = text.indexOf(commentEndExpression, startIndex);
   138         int commentLength;
   139         if (endIndex == -1) {
   140             setCurrentBlockState(1);
   141             commentLength = text.length() - startIndex;
   142         } else {
   143             commentLength = endIndex - startIndex
   144                             + commentEndExpression.matchedLength();
   145         }
   146         setFormat(startIndex, commentLength, multiLineCommentFormat);
   147         startIndex = text.indexOf(commentStartExpression,
   148                                                 startIndex + commentLength);
   149     }
   150 }