overte-HifiExperiments/interface/src/ScriptHighlighting.cpp
Thijs Wenker 6c89521f1d Style
2014-04-22 13:12:21 +02:00

95 lines
3.3 KiB
C++

//
// ScriptHighlighting.cpp
// interface/src
//
// Created by Thijs Wenker on 4/15/14.
// Copyright 2014 High Fidelity, Inc.
//
// Distributed under the Apache License, Version 2.0.
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "ScriptHighlighting.h"
#include <QTextDocument>
ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) :
QSyntaxHighlighter(parent)
{
_keywordRegex = QRegExp("\\b(break|case|catch|continue|debugger|default|delete|do|else|finally|for|function|if|in|instanceof|new|return|switch|this|throw|try|typeof|var|void|while|with)\\b");
_qoutedTextRegex = QRegExp("\".*\"");
_multiLineCommentBegin = QRegExp("/\\*");
_multiLineCommentEnd = QRegExp("\\*/");
_numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}");
_singleLineComment = QRegExp("//[^\n]*");
_truefalseRegex = QRegExp("\\b(true|false)\\b");
}
void ScriptHighlighting::highlightBlock(const QString& text) {
this->highlightKeywords(text);
this->formatNumbers(text);
this->formatTrueFalse(text);
this->formatQoutedText(text);
this->formatComments(text);
}
void ScriptHighlighting::highlightKeywords(const QString& text) {
int index = _keywordRegex.indexIn(text);
while (index >= 0) {
int length = _keywordRegex.matchedLength();
setFormat(index, length, Qt::blue);
index = _keywordRegex.indexIn(text, index + length);
}
}
void ScriptHighlighting::formatComments(const QString& text) {
setCurrentBlockState(BlockStateClean);
int start = (previousBlockState() != BlockStateInMultiComment) ? text.indexOf(_multiLineCommentBegin) : 0;
while (start > -1) {
int end = text.indexOf(_multiLineCommentEnd, start);
int length = (end == -1 ? text.length() : (end + _multiLineCommentEnd.matchedLength())) - start;
setFormat(start, length, Qt::lightGray);
start = text.indexOf(_multiLineCommentBegin, start + length);
if (end == -1) {
setCurrentBlockState(BlockStateInMultiComment);
}
}
int index = _singleLineComment.indexIn(text);
while (index >= 0) {
int length = _singleLineComment.matchedLength();
setFormat(index, length, Qt::lightGray);
index = _singleLineComment.indexIn(text, index + length);
}
}
void ScriptHighlighting::formatQoutedText(const QString& text){
int index = _qoutedTextRegex.indexIn(text);
while (index >= 0) {
int length = _qoutedTextRegex.matchedLength();
setFormat(index, length, Qt::red);
index = _qoutedTextRegex.indexIn(text, index + length);
}
}
void ScriptHighlighting::formatNumbers(const QString& text){
int index = _numberRegex.indexIn(text);
while (index >= 0) {
int length = _numberRegex.matchedLength();
setFormat(index, length, Qt::green);
index = _numberRegex.indexIn(text, index + length);
}
}
void ScriptHighlighting::formatTrueFalse(const QString& text){
int index = _truefalseRegex.indexIn(text);
while (index >= 0) {
int length = _truefalseRegex.matchedLength();
QFont* font = new QFont(this->document()->defaultFont());
font->setBold(true);
setFormat(index, length, *font);
index = _truefalseRegex.indexIn(text, index + length);
}
}