highlighter.cpp
author insilmaril
Wed, 02 May 2007 15:31:17 +0000
changeset 482 24c7902a3e14
parent 471 9c26d66d4c53
child 488 bd28847feede
permissions -rw-r--r--
Mainly documentation fixes
     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" 
    40 					<< "\\baddBranchBefore\\b" 
    41                     << "\\baddMapInsert\\b" 
    42 					<< "\\baddMapReplace\\b"
    43                     << "\\bcolorBranch\\b" 
    44 					<< "\\bcolorSubtree\\b"
    45                     << "\\bcut\\b" 
    46 					<< "\\bdelete\\b" 
    47 					<< "\\bdeletepKeepChilds\\b" 
    48 					<< "\\bdeletepChilds\\b"
    49 					<< "\\blinkTo\\b" 
    50 					<< "\\bloadImage\\b"
    51 					<< "\\bmoveBranchUp\\b" 
    52 					<< "\\bmoveBranchDown\\b"
    53 					<< "\\bmove\\b" 
    54 					<< "\\bmoveRel\\b"
    55 					<< "\\bpaste\\b" 
    56 					<< "\\bsaveImage\\b" 
    57 					<< "\\bscroll\\b" 
    58 					<< "\\bselect\\b" 
    59 					<< "\\bselectLastBranch\\b" 
    60 					<< "\\bselectLastImage\\b"
    61 					<< "\\bsetFrameType\\b" 
    62 					<< "\\bsetFramePenColor\\b" 
    63 					<< "\\bsetFrameBrushColor\\b" 
    64 					<< "\\bsetFramePadding\\b" 
    65 					<< "\\bsetFrameBorderWidth\\b" 
    66 					<< "\\bsetMapAuthor\\b" 
    67 					<< "\\bsetMapComment\\b" 
    68 					<< "\\bsetMapBackgroundColor\\b" 
    69 					<< "\\bsetMapDefLinkColor\\b" 
    70 					<< "\\bsetMapDefLinkStyle\\b" 
    71 					<< "\\bsetHeading\\b" 
    72 					<< "\\bsetHideExport\\b" 
    73 					<< "\\bsetIncludeImagesHorizontally\\b" 
    74 					<< "\\bsetIncludeImagesVertically\\b" 
    75 					<< "\\bsetURL\\b" 
    76 					<< "\\bsetVymLink\\b" 
    77 					<< "\\bsetFlag\\b" 
    78 					<< "\\btoggleFlag\\b" 
    79 					<< "\\bunscroll\\b" 
    80 					<< "\\bunscrollChilds\\b" 
    81 					<< "\\bunsetFlag\\b" 
    82 					;
    83     foreach (QString pattern, keywordPatterns) {
    84         rule.pattern = QRegExp(pattern);
    85         rule.format = keywordFormat;
    86         highlightingRules.append(rule);
    87     }
    88 
    89 	// QT keywords
    90 	/*
    91     classFormat.setFontWeight(QFont::Bold);
    92     classFormat.setForeground(Qt::darkMagenta);
    93     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    94     rule.format = classFormat;
    95     highlightingRules.append(rule);
    96 	*/
    97 
    98 	// Single line comments
    99     singleLineCommentFormat.setForeground(Qt::red);
   100     rule.pattern = QRegExp("#[^\n]*");
   101     rule.format = singleLineCommentFormat;
   102     highlightingRules.append(rule);
   103 
   104 	// multiline comments
   105     multiLineCommentFormat.setForeground(Qt::red);
   106     commentStartExpression = QRegExp("/\\*");
   107     commentEndExpression = QRegExp("\\*/");
   108 
   109 	// Quotations
   110     quotationFormat.setForeground(Qt::darkGreen);
   111     rule.pattern = QRegExp("\".*\"");
   112     rule.format = quotationFormat;
   113     highlightingRules.append(rule);
   114 
   115     QStringList valuePatterns;
   116     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   117     foreach (QString pattern, valuePatterns) {
   118         rule.pattern = QRegExp(pattern);
   119         rule.format = quotationFormat;
   120         highlightingRules.append(rule);
   121     }
   122 
   123 
   124 
   125 	// Funtions
   126 	/*
   127     functionFormat.setFontItalic(true);
   128     functionFormat.setForeground(Qt::blue);
   129     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   130     rule.format = functionFormat;
   131     highlightingRules.append(rule);
   132 	*/
   133 
   134 }
   135 
   136 void Highlighter::highlightBlock(const QString &text)
   137 {
   138     foreach (HighlightingRule rule, highlightingRules) {
   139         QRegExp expression(rule.pattern);
   140         int index = text.indexOf(expression);
   141         while (index >= 0) {
   142             int length = expression.matchedLength();
   143             setFormat(index, length, rule.format);
   144             index = text.indexOf(expression, index + length);
   145         }
   146     }
   147     setCurrentBlockState(0);
   148 
   149     int startIndex = 0;
   150     if (previousBlockState() != 1)
   151         startIndex = text.indexOf(commentStartExpression);
   152 
   153     while (startIndex >= 0) {
   154         int endIndex = text.indexOf(commentEndExpression, startIndex);
   155         int commentLength;
   156         if (endIndex == -1) {
   157             setCurrentBlockState(1);
   158             commentLength = text.length() - startIndex;
   159         } else {
   160             commentLength = endIndex - startIndex
   161                             + commentEndExpression.matchedLength();
   162         }
   163         setFormat(startIndex, commentLength, multiLineCommentFormat);
   164         startIndex = text.indexOf(commentStartExpression,
   165                                                 startIndex + commentLength);
   166     }
   167 }