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