highlighter.cpp
author insilmaril
Wed, 25 Apr 2007 16:02:54 +0000
changeset 474 d9add21ee9d3
parent 471 9c26d66d4c53
child 482 24c7902a3e14
permissions -rw-r--r--
started doxygen documentation
     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 					<< "\\bsetMapAuthor\\b" 
    62 					<< "\\bsetMapComment\\b" 
    63 					<< "\\bsetMapBackgroundColor\\b" 
    64 					<< "\\bsetMapDefLinkColor\\b" 
    65 					<< "\\bsetMapDefLinkStyle\\b" 
    66 					<< "\\bsetHeading\\b" 
    67 					<< "\\bsetHideExport\\b" 
    68 					<< "\\bsetIncludeImagesHorizontally\\b" 
    69 					<< "\\bsetIncludeImagesVertically\\b" 
    70 					<< "\\bsetURL\\b" 
    71 					<< "\\bsetVymLink\\b" 
    72 					<< "\\bsetFlag\\b" 
    73 					<< "\\btoggleFlag\\b" 
    74 					<< "\\bunscroll\\b" 
    75 					<< "\\bunscrollChilds\\b" 
    76 					<< "\\bunsetFlag\\b" 
    77 					;
    78     foreach (QString pattern, keywordPatterns) {
    79         rule.pattern = QRegExp(pattern);
    80         rule.format = keywordFormat;
    81         highlightingRules.append(rule);
    82     }
    83 
    84 	// QT keywords
    85 	/*
    86     classFormat.setFontWeight(QFont::Bold);
    87     classFormat.setForeground(Qt::darkMagenta);
    88     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    89     rule.format = classFormat;
    90     highlightingRules.append(rule);
    91 	*/
    92 
    93 	// Single line comments
    94     singleLineCommentFormat.setForeground(Qt::red);
    95     rule.pattern = QRegExp("#[^\n]*");
    96     rule.format = singleLineCommentFormat;
    97     highlightingRules.append(rule);
    98 
    99 	// multiline comments
   100     multiLineCommentFormat.setForeground(Qt::red);
   101     commentStartExpression = QRegExp("/\\*");
   102     commentEndExpression = QRegExp("\\*/");
   103 
   104 	// Quotations
   105     quotationFormat.setForeground(Qt::darkGreen);
   106     rule.pattern = QRegExp("\".*\"");
   107     rule.format = quotationFormat;
   108     highlightingRules.append(rule);
   109 
   110     QStringList valuePatterns;
   111     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   112     foreach (QString pattern, valuePatterns) {
   113         rule.pattern = QRegExp(pattern);
   114         rule.format = quotationFormat;
   115         highlightingRules.append(rule);
   116     }
   117 
   118 
   119 
   120 	// Funtions
   121 	/*
   122     functionFormat.setFontItalic(true);
   123     functionFormat.setForeground(Qt::blue);
   124     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   125     rule.format = functionFormat;
   126     highlightingRules.append(rule);
   127 	*/
   128 
   129 }
   130 
   131 void Highlighter::highlightBlock(const QString &text)
   132 {
   133     foreach (HighlightingRule rule, highlightingRules) {
   134         QRegExp expression(rule.pattern);
   135         int index = text.indexOf(expression);
   136         while (index >= 0) {
   137             int length = expression.matchedLength();
   138             setFormat(index, length, rule.format);
   139             index = text.indexOf(expression, index + length);
   140         }
   141     }
   142     setCurrentBlockState(0);
   143 
   144     int startIndex = 0;
   145     if (previousBlockState() != 1)
   146         startIndex = text.indexOf(commentStartExpression);
   147 
   148     while (startIndex >= 0) {
   149         int endIndex = text.indexOf(commentEndExpression, startIndex);
   150         int commentLength;
   151         if (endIndex == -1) {
   152             setCurrentBlockState(1);
   153             commentLength = text.length() - startIndex;
   154         } else {
   155             commentLength = endIndex - startIndex
   156                             + commentEndExpression.matchedLength();
   157         }
   158         setFormat(startIndex, commentLength, multiLineCommentFormat);
   159         startIndex = text.indexOf(commentStartExpression,
   160                                                 startIndex + commentLength);
   161     }
   162 }