highlighter.cpp
author insilmaril
Mon, 14 Jan 2008 16:26:59 +0000
changeset 645 2abfdb7e85f4
parent 628 d7d0708b1c60
child 683 7df0b7986274
permissions -rw-r--r--
Added french manual by Claude
     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 					<< "\\bcopy\\b"
    46                     << "\\bcut\\b" 
    47 					<< "\\bdelete\\b" 
    48 					<< "\\bdeleteKeepChilds\\b" 
    49 					<< "\\bdeleteChilds\\b"
    50 					<< "\\bexportASCII\\b"
    51 					<< "\\bexportImage\\b"
    52 					<< "\\bexportXHTML\\b"
    53 					<< "\\bexportXML\\b"
    54 					<< "\\bimportDir\\b"
    55 					<< "\\blinkTo\\b" 
    56 					<< "\\bloadImage\\b"
    57 					<< "\\bmoveBranchUp\\b" 
    58 					<< "\\bmoveBranchDown\\b"
    59 					<< "\\bmove\\b" 
    60 					<< "\\bmoveRel\\b"
    61 					<< "\\bnop\\b"
    62 					<< "\\bpaste\\b" 
    63 					<< "\\bqa\\b" 
    64 					<< "\\bsaveImage\\b" 
    65 					<< "\\bscroll\\b" 
    66 					<< "\\bselect\\b" 
    67 					<< "\\bselectLastBranch\\b" 
    68 					<< "\\bselectLastImage\\b"
    69 					<< "\\bselectLatestAdded\\b"
    70 					<< "\\bsetFrameType\\b" 
    71 					<< "\\bsetFramePenColor\\b" 
    72 					<< "\\bsetFrameBrushColor\\b" 
    73 					<< "\\bsetFramePadding\\b" 
    74 					<< "\\bsetFrameBorderWidth\\b" 
    75 					<< "\\bsetHideLinkUnselected\\b" 
    76 					<< "\\bsetMapAuthor\\b" 
    77 					<< "\\bsetMapComment\\b" 
    78 					<< "\\bsetMapBackgroundColor\\b" 
    79 					<< "\\bsetMapDefLinkColor\\b" 
    80 					<< "\\bsetMapDefLinkStyle\\b" 
    81 					<< "\\bsetHeading\\b" 
    82 					<< "\\bsetHideExport\\b" 
    83 					<< "\\bsetIncludeImagesHorizontally\\b" 
    84 					<< "\\bsetIncludeImagesVertically\\b" 
    85 					<< "\\bsetURL\\b" 
    86 					<< "\\bsetVymLink\\b" 
    87 					<< "\\bsetFlag\\b" 
    88 					<< "\\bsortChildren\\b" 
    89 					<< "\\btoggleFlag\\b" 
    90 					<< "\\bunscroll\\b" 
    91 					<< "\\bunscrollChilds\\b" 
    92 					<< "\\bunsetFlag\\b" 
    93 					;
    94     foreach (QString pattern, keywordPatterns) {
    95         rule.pattern = QRegExp(pattern);
    96         rule.format = keywordFormat;
    97         highlightingRules.append(rule);
    98     }
    99 
   100 	// QT keywords
   101 	/*
   102     classFormat.setFontWeight(QFont::Bold);
   103     classFormat.setForeground(Qt::darkMagenta);
   104     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
   105     rule.format = classFormat;
   106     highlightingRules.append(rule);
   107 	*/
   108 
   109 	// Single line comments
   110     singleLineCommentFormat.setForeground(Qt::red);
   111     rule.pattern = QRegExp("#[^\n]*");
   112     rule.format = singleLineCommentFormat;
   113     highlightingRules.append(rule);
   114 
   115 	// multiline comments
   116     multiLineCommentFormat.setForeground(Qt::red);
   117     commentStartExpression = QRegExp("/\\*");
   118     commentEndExpression = QRegExp("\\*/");
   119 
   120 	// Quotations
   121     quotationFormat.setForeground(Qt::darkGreen);
   122     rule.pattern = QRegExp("\".*\"");
   123     rule.format = quotationFormat;
   124     highlightingRules.append(rule);
   125 
   126     QStringList valuePatterns;
   127     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   128     foreach (QString pattern, valuePatterns) {
   129         rule.pattern = QRegExp(pattern);
   130         rule.format = quotationFormat;
   131         highlightingRules.append(rule);
   132     }
   133 
   134 
   135 
   136 	// Funtions
   137 	/*
   138     functionFormat.setFontItalic(true);
   139     functionFormat.setForeground(Qt::blue);
   140     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   141     rule.format = functionFormat;
   142     highlightingRules.append(rule);
   143 	*/
   144 
   145 }
   146 
   147 void Highlighter::highlightBlock(const QString &text)
   148 {
   149     foreach (HighlightingRule rule, highlightingRules) {
   150         QRegExp expression(rule.pattern);
   151         int index = text.indexOf(expression);
   152         while (index >= 0) {
   153             int length = expression.matchedLength();
   154             setFormat(index, length, rule.format);
   155             index = text.indexOf(expression, index + length);
   156         }
   157     }
   158     setCurrentBlockState(0);
   159 
   160     int startIndex = 0;
   161     if (previousBlockState() != 1)
   162         startIndex = text.indexOf(commentStartExpression);
   163 
   164     while (startIndex >= 0) {
   165         int endIndex = text.indexOf(commentEndExpression, startIndex);
   166         int commentLength;
   167         if (endIndex == -1) {
   168             setCurrentBlockState(1);
   169             commentLength = text.length() - startIndex;
   170         } else {
   171             commentLength = endIndex - startIndex
   172                             + commentEndExpression.matchedLength();
   173         }
   174         setFormat(startIndex, commentLength, multiLineCommentFormat);
   175         startIndex = text.indexOf(commentStartExpression,
   176                                                 startIndex + commentLength);
   177     }
   178 }