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