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