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