highlighter.cpp
author insilmaril
Thu, 19 Mar 2009 11:45:28 +0000
changeset 742 54d44ecd6097
parent 683 7df0b7986274
child 804 14f2b1b15242
permissions -rw-r--r--
ProgressBar during load and more fixes
     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 					<< "\\bsetHeading\\b" 
    83 					<< "\\bsetHideExport\\b" 
    84 					<< "\\bsetIncludeImagesHorizontally\\b" 
    85 					<< "\\bsetIncludeImagesVertically\\b" 
    86 					<< "\\bsetURL\\b" 
    87 					<< "\\bsetVymLink\\b" 
    88 					<< "\\bsetFlag\\b" 
    89 					<< "\\bsortChildren\\b" 
    90 					<< "\\btoggleFlag\\b" 
    91 					<< "\\bunscroll\\b" 
    92 					<< "\\bunscrollChilds\\b" 
    93 					<< "\\bunsetFlag\\b" 
    94 					;
    95     foreach (QString pattern, keywordPatterns) {
    96         rule.pattern = QRegExp(pattern);
    97         rule.format = keywordFormat;
    98         highlightingRules.append(rule);
    99     }
   100 
   101 	// QT keywords
   102 	/*
   103     classFormat.setFontWeight(QFont::Bold);
   104     classFormat.setForeground(Qt::darkMagenta);
   105     rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
   106     rule.format = classFormat;
   107     highlightingRules.append(rule);
   108 	*/
   109 
   110 	// Single line comments
   111     singleLineCommentFormat.setForeground(Qt::red);
   112     rule.pattern = QRegExp("#[^\n]*");
   113     rule.format = singleLineCommentFormat;
   114     highlightingRules.append(rule);
   115 
   116 	// multiline comments
   117     multiLineCommentFormat.setForeground(Qt::red);
   118     commentStartExpression = QRegExp("/\\*");
   119     commentEndExpression = QRegExp("\\*/");
   120 
   121 	// Quotations
   122     quotationFormat.setForeground(Qt::darkGreen);
   123     rule.pattern = QRegExp("\".*\"");
   124     rule.format = quotationFormat;
   125     highlightingRules.append(rule);
   126 
   127     QStringList valuePatterns;
   128     valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   129     foreach (QString pattern, valuePatterns) {
   130         rule.pattern = QRegExp(pattern);
   131         rule.format = quotationFormat;
   132         highlightingRules.append(rule);
   133     }
   134 
   135 
   136 
   137 	// Funtions
   138 	/*
   139     functionFormat.setFontItalic(true);
   140     functionFormat.setForeground(Qt::blue);
   141     rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   142     rule.format = functionFormat;
   143     highlightingRules.append(rule);
   144 	*/
   145 
   146 }
   147 
   148 void Highlighter::highlightBlock(const QString &text)
   149 {
   150     foreach (HighlightingRule rule, highlightingRules) {
   151         QRegExp expression(rule.pattern);
   152         int index = text.indexOf(expression);
   153         while (index >= 0) {
   154             int length = expression.matchedLength();
   155             setFormat(index, length, rule.format);
   156             index = text.indexOf(expression, index + length);
   157         }
   158     }
   159     setCurrentBlockState(0);
   160 
   161     int startIndex = 0;
   162     if (previousBlockState() != 1)
   163         startIndex = text.indexOf(commentStartExpression);
   164 
   165     while (startIndex >= 0) {
   166         int endIndex = text.indexOf(commentEndExpression, startIndex);
   167         int commentLength;
   168         if (endIndex == -1) {
   169             setCurrentBlockState(1);
   170             commentLength = text.length() - startIndex;
   171         } else {
   172             commentLength = endIndex - startIndex
   173                             + commentEndExpression.matchedLength();
   174         }
   175         setFormat(startIndex, commentLength, multiLineCommentFormat);
   176         startIndex = text.indexOf(commentStartExpression,
   177                                                 startIndex + commentLength);
   178     }
   179 }