mirror of
https://github.com/overte-org/overte.git
synced 2025-08-09 21:12:53 +02:00
ScriptEditor: Syntax highlighting update
This commit is contained in:
parent
1634e574d7
commit
e66822c6ee
4 changed files with 137 additions and 11 deletions
|
@ -11,8 +11,61 @@
|
||||||
|
|
||||||
#include "ScriptHighlighting.h"
|
#include "ScriptHighlighting.h"
|
||||||
|
|
||||||
ScriptHighlighting::ScriptHighlighting(QObject* parent) :
|
ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) :
|
||||||
QSyntaxHighlighter(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}");
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -18,8 +18,26 @@ class ScriptHighlighting : public QSyntaxHighlighter {
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
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
|
#endif // hifi_ScriptHighlighting_h
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
|
#include "ScriptHighlighting.h"
|
||||||
#include "ui_scriptEditorWidget.h"
|
#include "ui_scriptEditorWidget.h"
|
||||||
|
|
||||||
#include "ScriptEditorWidget.h"
|
#include "ScriptEditorWidget.h"
|
||||||
|
@ -31,7 +32,9 @@ ScriptEditorWidget::ScriptEditorWidget() :
|
||||||
|
|
||||||
// remove the title bar (see the Qt docs on setTitleBarWidget)
|
// remove the title bar (see the Qt docs on setTitleBarWidget)
|
||||||
setTitleBarWidget(new QWidget());
|
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() {
|
ScriptEditorWidget::~ScriptEditorWidget() {
|
||||||
|
|
|
@ -52,7 +52,7 @@
|
||||||
<number>0</number>
|
<number>0</number>
|
||||||
</property>
|
</property>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QTextEdit" name="textEdit">
|
<widget class="QTextEdit" name="scriptEdit">
|
||||||
<property name="font">
|
<property name="font">
|
||||||
<font>
|
<font>
|
||||||
<family>Courier</family>
|
<family>Courier</family>
|
||||||
|
@ -68,22 +68,74 @@
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QLabel" name="label">
|
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||||
<property name="text">
|
<property name="spacing">
|
||||||
<string>Debug Log:</string>
|
<number>0</number>
|
||||||
</property>
|
</property>
|
||||||
</widget>
|
<property name="leftMargin">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<property name="topMargin">
|
||||||
|
<number>0</number>
|
||||||
|
</property>
|
||||||
|
<item>
|
||||||
|
<widget class="QLabel" name="label">
|
||||||
|
<property name="sizePolicy">
|
||||||
|
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
|
||||||
|
<horstretch>0</horstretch>
|
||||||
|
<verstretch>0</verstretch>
|
||||||
|
</sizepolicy>
|
||||||
|
</property>
|
||||||
|
<property name="text">
|
||||||
|
<string>Debug Log:</string>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
<item>
|
||||||
|
<widget class="QPushButton" name="clearButton">
|
||||||
|
<property name="text">
|
||||||
|
<string>Clear</string>
|
||||||
|
</property>
|
||||||
|
<property name="iconSize">
|
||||||
|
<size>
|
||||||
|
<width>16</width>
|
||||||
|
<height>16</height>
|
||||||
|
</size>
|
||||||
|
</property>
|
||||||
|
</widget>
|
||||||
|
</item>
|
||||||
|
</layout>
|
||||||
</item>
|
</item>
|
||||||
<item>
|
<item>
|
||||||
<widget class="QPlainTextEdit" name="plainTextEdit">
|
<widget class="QPlainTextEdit" name="debugText">
|
||||||
<property name="styleSheet">
|
<property name="styleSheet">
|
||||||
<string notr="true">font: 8pt "Courier";</string>
|
<string notr="true">font: 8pt "Courier";</string>
|
||||||
</property>
|
</property>
|
||||||
|
<property name="readOnly">
|
||||||
|
<bool>true</bool>
|
||||||
|
</property>
|
||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
</widget>
|
</widget>
|
||||||
<resources/>
|
<resources/>
|
||||||
<connections/>
|
<connections>
|
||||||
|
<connection>
|
||||||
|
<sender>clearButton</sender>
|
||||||
|
<signal>clicked()</signal>
|
||||||
|
<receiver>debugText</receiver>
|
||||||
|
<slot>clear()</slot>
|
||||||
|
<hints>
|
||||||
|
<hint type="sourcelabel">
|
||||||
|
<x>663</x>
|
||||||
|
<y>447</y>
|
||||||
|
</hint>
|
||||||
|
<hint type="destinationlabel">
|
||||||
|
<x>350</x>
|
||||||
|
<y>501</y>
|
||||||
|
</hint>
|
||||||
|
</hints>
|
||||||
|
</connection>
|
||||||
|
</connections>
|
||||||
</ui>
|
</ui>
|
||||||
|
|
Loading…
Reference in a new issue