mirror of
https://github.com/overte-org/overte.git
synced 2025-05-06 21:09:50 +02:00
- ScriptHightlighting qouting RegEx fix ( "hello" + " " + "world" // the text between two quote entities isn't red anymore )
110 lines
3.9 KiB
C++
110 lines
3.9 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("\"[^\"]*(\"){0,1}");
|
|
_multiLineCommentBegin = QRegExp("/\\*");
|
|
_multiLineCommentEnd = QRegExp("\\*/");
|
|
_numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}");
|
|
_singleLineComment = QRegExp("//[^\n]*");
|
|
_truefalseRegex = QRegExp("\\b(true|false)\\b");
|
|
_alphacharRegex = QRegExp("[A-Za-z]");
|
|
}
|
|
|
|
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();
|
|
int quoted_index = _qoutedTextRegex.indexIn(text);
|
|
bool valid = true;
|
|
while (quoted_index >= 0 && valid) {
|
|
int quoted_length = _qoutedTextRegex.matchedLength();
|
|
if (quoted_index <= index && index <= (quoted_index + quoted_length)) {
|
|
valid = false;
|
|
}
|
|
quoted_index = _qoutedTextRegex.indexIn(text, quoted_index + quoted_length);
|
|
}
|
|
|
|
if (valid) {
|
|
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();
|
|
if (index == 0 || _alphacharRegex.indexIn(text, index - 1) != (index - 1)) {
|
|
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);
|
|
}
|
|
}
|