Slightly improved scripting abilities
authorinsilmaril
Wed, 21 Mar 2007 11:51:38 +0000
changeset 435bd71dfb2292c
parent 434 c585be63ec69
child 436 19e5907b7818
Slightly improved scripting abilities
demos/todo.vym
highlighter.cpp
     1.1 Binary file demos/todo.vym has changed
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/highlighter.cpp	Wed Mar 21 11:51:38 2007 +0000
     2.3 @@ -0,0 +1,150 @@
     2.4 +/****************************************************************************
     2.5 +**
     2.6 +** Copyright (C) 2005-2006 Trolltech ASA. All rights reserved.
     2.7 +**
     2.8 +** This file is part of the example classes of the Qt Toolkit.
     2.9 +**
    2.10 +** This file may be used under the terms of the GNU General Public
    2.11 +** License version 2.0 as published by the Free Software Foundation
    2.12 +** and appearing in the file LICENSE.GPL included in the packaging of
    2.13 +** this file.  Please review the following information to ensure GNU
    2.14 +** General Public Licensing requirements will be met:
    2.15 +** http://www.trolltech.com/products/qt/opensource.html
    2.16 +**
    2.17 +** If you are unsure which license is appropriate for your use, please
    2.18 +** review the following information:
    2.19 +** http://www.trolltech.com/products/qt/licensing.html or contact the
    2.20 +** sales department at sales@trolltech.com.
    2.21 +**
    2.22 +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
    2.23 +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
    2.24 +**
    2.25 +****************************************************************************/
    2.26 +
    2.27 +// highlighting rules have been adapted by Uwe Drechsel to match vym syntax
    2.28 +
    2.29 +
    2.30 +#include <QtGui>
    2.31 +
    2.32 +#include "highlighter.h"
    2.33 +
    2.34 +Highlighter::Highlighter(QTextDocument *parent)
    2.35 +    : QSyntaxHighlighter(parent)
    2.36 +{
    2.37 +    HighlightingRule rule;
    2.38 +
    2.39 +    keywordFormat.setForeground(Qt::darkBlue);
    2.40 +    keywordFormat.setFontWeight(QFont::Bold);
    2.41 +    QStringList keywordPatterns;
    2.42 +    keywordPatterns << "\\baddBranch\\b" << "\\baddBranchBefore\\b" 
    2.43 +                    << "\\baddMapInsert\\b" << "\\baddMapReplace\\b"
    2.44 +                    << "\\bcolorBranch\\b" << "\\bcolorSubtree\\b"
    2.45 +                    << "\\bcut\\b" << "\\bdelete\\b" 
    2.46 +					<< "\\bdeletepKeepChilds\\b" << "\\bdeletepChilds\\b"
    2.47 +					<< "\\blinkTo\\b" << "\\bloadImage\\b"
    2.48 +					<< "\\bmoveBranchUp\\b" << "\\bmoveBranchDown\\b"
    2.49 +					<< "\\bmove\\b" << "\\bmoveRel\\b"
    2.50 +					<< "\\bpaste\\b" 
    2.51 +					<< "\\bsaveImage\\b" 
    2.52 +					<< "\\bscroll\\b" 
    2.53 +					<< "\\bselect\\b" << "\\bselectLastBranch\\b" << "\\bselectLastImage\\b"
    2.54 +					<< "\\bsetMapAuthor\\b" 
    2.55 +					<< "\\bsetMapComment\\b" 
    2.56 +					<< "\\bsetMapBackgroundColor\\b" 
    2.57 +					<< "\\bsetMapDefLinkColor\\b" 
    2.58 +					<< "\\bsetMapDefLinkStyle\\b" 
    2.59 +					<< "\\bsetHeading\\b" 
    2.60 +					<< "\\bsetHideExport\\b" 
    2.61 +					<< "\\bsetIncludeImagesHorizontally\\b" 
    2.62 +					<< "\\bsetIncludeImagesVertically\\b" 
    2.63 +					<< "\\bsetURL\\b" 
    2.64 +					<< "\\bsetVymLink\\b" 
    2.65 +					<< "\\bsetFlag\\b" 
    2.66 +					<< "\\bunscroll\\b" 
    2.67 +					<< "\\bunsetFlag\\b" 
    2.68 +					;
    2.69 +    foreach (QString pattern, keywordPatterns) {
    2.70 +        rule.pattern = QRegExp(pattern);
    2.71 +        rule.format = keywordFormat;
    2.72 +        highlightingRules.append(rule);
    2.73 +    }
    2.74 +
    2.75 +	// QT keywords
    2.76 +	/*
    2.77 +    classFormat.setFontWeight(QFont::Bold);
    2.78 +    classFormat.setForeground(Qt::darkMagenta);
    2.79 +    rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b");
    2.80 +    rule.format = classFormat;
    2.81 +    highlightingRules.append(rule);
    2.82 +	*/
    2.83 +
    2.84 +	// Single line comments
    2.85 +    singleLineCommentFormat.setForeground(Qt::red);
    2.86 +    rule.pattern = QRegExp("#[^\n]*");
    2.87 +    rule.format = singleLineCommentFormat;
    2.88 +    highlightingRules.append(rule);
    2.89 +
    2.90 +	// Single line comments
    2.91 +    multiLineCommentFormat.setForeground(Qt::red);
    2.92 +    commentStartExpression = QRegExp("/\\*");
    2.93 +    commentEndExpression = QRegExp("\\*/");
    2.94 +
    2.95 +	// Quotations
    2.96 +    quotationFormat.setForeground(Qt::darkGreen);
    2.97 +    rule.pattern = QRegExp("\".*\"");
    2.98 +    rule.format = quotationFormat;
    2.99 +    highlightingRules.append(rule);
   2.100 +
   2.101 +    QStringList valuePatterns;
   2.102 +    valuePatterns << "\\btrue\\b" << "\\bfalse\\b";
   2.103 +    foreach (QString pattern, valuePatterns) {
   2.104 +        rule.pattern = QRegExp(pattern);
   2.105 +        rule.format = quotationFormat;
   2.106 +        highlightingRules.append(rule);
   2.107 +    }
   2.108 +
   2.109 +
   2.110 +
   2.111 +	// Funtions
   2.112 +	/*
   2.113 +    functionFormat.setFontItalic(true);
   2.114 +    functionFormat.setForeground(Qt::blue);
   2.115 +    rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()");
   2.116 +    rule.format = functionFormat;
   2.117 +    highlightingRules.append(rule);
   2.118 +	*/
   2.119 +
   2.120 +}
   2.121 +
   2.122 +void Highlighter::highlightBlock(const QString &text)
   2.123 +{
   2.124 +    foreach (HighlightingRule rule, highlightingRules) {
   2.125 +        QRegExp expression(rule.pattern);
   2.126 +        int index = text.indexOf(expression);
   2.127 +        while (index >= 0) {
   2.128 +            int length = expression.matchedLength();
   2.129 +            setFormat(index, length, rule.format);
   2.130 +            index = text.indexOf(expression, index + length);
   2.131 +        }
   2.132 +    }
   2.133 +    setCurrentBlockState(0);
   2.134 +
   2.135 +    int startIndex = 0;
   2.136 +    if (previousBlockState() != 1)
   2.137 +        startIndex = text.indexOf(commentStartExpression);
   2.138 +
   2.139 +    while (startIndex >= 0) {
   2.140 +        int endIndex = text.indexOf(commentEndExpression, startIndex);
   2.141 +        int commentLength;
   2.142 +        if (endIndex == -1) {
   2.143 +            setCurrentBlockState(1);
   2.144 +            commentLength = text.length() - startIndex;
   2.145 +        } else {
   2.146 +            commentLength = endIndex - startIndex
   2.147 +                            + commentEndExpression.matchedLength();
   2.148 +        }
   2.149 +        setFormat(startIndex, commentLength, multiLineCommentFormat);
   2.150 +        startIndex = text.indexOf(commentStartExpression,
   2.151 +                                                startIndex + commentLength);
   2.152 +    }
   2.153 +}