highlighter.cpp
author insilmaril
Sun, 26 Aug 2007 12:52:59 +0000
changeset 589 48dde9218263
parent 536 322402d9f9f9
child 628 d7d0708b1c60
permissions -rw-r--r--
Changed version in vym.pdf
     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 					<< "\\btoggleFlag\\b" 
    89 					<< "\\bunscroll\\b" 
    90 					<< "\\bunscrollChilds\\b" 
    91 					<< "\\bunsetFlag\\b" 
    92 					;
    93     foreach (QString pattern, keywordPatterns) {
    94         rule.pattern = QRegExp(pattern);
    95         rule.format = keywordFormat;
    96         highlightingRules.append(rule);
    97     }
    98 
    99 	// QT keywords
   100 	/*
   101     classFormat.setFontWeight(QFont::Bold);
   102     classFormat.setForeground(Qt::darkMagenta);
   103     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
   104     rule.format = classFormat;
   105     highlightingRules.append(rule);
   106 	*/
   107 
   108 	// Single line comments
   109     singleLineCommentFormat.setForeground(Qt::red);
   110     rule.pattern = QRegExp("#[^\n]*");
   111     rule.format = singleLineCommentFormat;
   112     highlightingRules.append(rule);
   113 
   114 	// multiline comments
   115     multiLineCommentFormat.setForeground(Qt::red);
   116     commentStartExpression = QRegExp("/\\*");
   117     commentEndExpression = QRegExp("\\*/");
   118 
   119 	// Quotations
   120     quotationFormat.setForeground(Qt::darkGreen);
   121     rule.pattern = QRegExp("\".*\"");
   122     rule.format = quotationFormat;
   123     highlightingRules.append(rule);
   124 
   125     QStringList valuePatterns;
   126     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   127     foreach (QString pattern, valuePatterns) {
   128         rule.pattern = QRegExp(pattern);
   129         rule.format = quotationFormat;
   130         highlightingRules.append(rule);
   131     }
   132 
   133 
   134 
   135 	// Funtions
   136 	/*
   137     functionFormat.setFontItalic(true);
   138     functionFormat.setForeground(Qt::blue);
   139     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   140     rule.format = functionFormat;
   141     highlightingRules.append(rule);
   142 	*/
   143 
   144 }
   145 
   146 void Highlighter::highlightBlock(const QString &text)
   147 {
   148     foreach (HighlightingRule rule, highlightingRules) {
   149         QRegExp expression(rule.pattern);
   150         int index = text.indexOf(expression);
   151         while (index >= 0) {
   152             int length = expression.matchedLength();
   153             setFormat(index, length, rule.format);
   154             index = text.indexOf(expression, index + length);
   155         }
   156     }
   157     setCurrentBlockState(0);
   158 
   159     int startIndex = 0;
   160     if (previousBlockState() != 1)
   161         startIndex = text.indexOf(commentStartExpression);
   162 
   163     while (startIndex >= 0) {
   164         int endIndex = text.indexOf(commentEndExpression, startIndex);
   165         int commentLength;
   166         if (endIndex == -1) {
   167             setCurrentBlockState(1);
   168             commentLength = text.length() - startIndex;
   169         } else {
   170             commentLength = endIndex - startIndex
   171                             + commentEndExpression.matchedLength();
   172         }
   173         setFormat(startIndex, commentLength, multiLineCommentFormat);
   174         startIndex = text.indexOf(commentStartExpression,
   175                                                 startIndex + commentLength);
   176     }
   177 }