From e66822c6ee56cc12854628e6b1448bdc635fdb82 Mon Sep 17 00:00:00 2001 From: Thijs Wenker Date: Wed, 16 Apr 2014 00:55:06 +0200 Subject: [PATCH] ScriptEditor: Syntax highlighting update --- interface/src/ScriptHighlighting.cpp | 57 ++++++++++++++++++++- interface/src/ScriptHighlighting.h | 20 +++++++- interface/src/ui/ScriptEditorWidget.cpp | 5 +- interface/ui/ScriptEditorWidget.ui | 66 ++++++++++++++++++++++--- 4 files changed, 137 insertions(+), 11 deletions(-) diff --git a/interface/src/ScriptHighlighting.cpp b/interface/src/ScriptHighlighting.cpp index 15505ac49a..119926742c 100644 --- a/interface/src/ScriptHighlighting.cpp +++ b/interface/src/ScriptHighlighting.cpp @@ -11,8 +11,61 @@ #include "ScriptHighlighting.h" -ScriptHighlighting::ScriptHighlighting(QObject* parent) : +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}"); +} -} \ No newline at end of file +void ScriptHighlighting::highlightBlock(const QString &text) { + this->highlightKeywords(text); + this->formatComments(text); + this->formatQoutedText(text); + this->formatNumbers(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); + } +} + +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); + } +} diff --git a/interface/src/ScriptHighlighting.h b/interface/src/ScriptHighlighting.h index f2bd97930e..b9567cb06a 100644 --- a/interface/src/ScriptHighlighting.h +++ b/interface/src/ScriptHighlighting.h @@ -18,8 +18,26 @@ class ScriptHighlighting : public QSyntaxHighlighter { Q_OBJECT public: - ScriptHighlighting(QObject* parent = 0); + ScriptHighlighting(QTextDocument* parent = 0); + enum BlockState { + BlockStateClean, + BlockStateInMultiComment + }; + +protected: + void highlightBlock(const QString &text); + void highlightKeywords(const QString &text); + void formatComments(const QString &text); + void formatQoutedText(const QString &text); + void formatNumbers(const QString &text); + +private: + QRegExp keywordRegex; + QRegExp qoutedTextRegex; + QRegExp multiLineCommentBegin; + QRegExp multiLineCommentEnd; + QRegExp numberRegex; }; #endif // hifi_ScriptHighlighting_h diff --git a/interface/src/ui/ScriptEditorWidget.cpp b/interface/src/ui/ScriptEditorWidget.cpp index 4adf01a28a..618e405448 100644 --- a/interface/src/ui/ScriptEditorWidget.cpp +++ b/interface/src/ui/ScriptEditorWidget.cpp @@ -20,6 +20,7 @@ #include #include "Application.h" +#include "ScriptHighlighting.h" #include "ui_scriptEditorWidget.h" #include "ScriptEditorWidget.h" @@ -31,7 +32,9 @@ ScriptEditorWidget::ScriptEditorWidget() : // remove the title bar (see the Qt docs on setTitleBarWidget) setTitleBarWidget(new QWidget()); - //QSyntaxHighlighter* highlighter = new QSyntaxHighlighter(); + QFontMetrics fm(this->ui->scriptEdit->font()); + this->ui->scriptEdit->setTabStopWidth(fm.width('0') * 4); + ScriptHighlighting* highlighting = new ScriptHighlighting(this->ui->scriptEdit->document()); } ScriptEditorWidget::~ScriptEditorWidget() { diff --git a/interface/ui/ScriptEditorWidget.ui b/interface/ui/ScriptEditorWidget.ui index 82398d587c..5878f26c69 100644 --- a/interface/ui/ScriptEditorWidget.ui +++ b/interface/ui/ScriptEditorWidget.ui @@ -52,7 +52,7 @@ 0 - + Courier @@ -68,22 +68,74 @@ - - - Debug Log: + + + 0 - + + 0 + + + 0 + + + + + + 0 + 0 + + + + Debug Log: + + + + + + + Clear + + + + 16 + 16 + + + + + - + font: 8pt "Courier"; + + true + - + + + clearButton + clicked() + debugText + clear() + + + 663 + 447 + + + 350 + 501 + + + +