mirror of
https://github.com/overte-org/overte.git
synced 2025-04-10 16:12:28 +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"
|
||||
|
||||
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}");
|
||||
}
|
||||
|
||||
}
|
||||
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
|
||||
|
||||
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
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include <QWidget>
|
||||
|
||||
#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() {
|
||||
|
|
|
@ -52,7 +52,7 @@
|
|||
<number>0</number>
|
||||
</property>
|
||||
<item>
|
||||
<widget class="QTextEdit" name="textEdit">
|
||||
<widget class="QTextEdit" name="scriptEdit">
|
||||
<property name="font">
|
||||
<font>
|
||||
<family>Courier</family>
|
||||
|
@ -68,22 +68,74 @@
|
|||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QLabel" name="label">
|
||||
<property name="text">
|
||||
<string>Debug Log:</string>
|
||||
<layout class="QHBoxLayout" name="horizontalLayout">
|
||||
<property name="spacing">
|
||||
<number>0</number>
|
||||
</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>
|
||||
<widget class="QPlainTextEdit" name="plainTextEdit">
|
||||
<widget class="QPlainTextEdit" name="debugText">
|
||||
<property name="styleSheet">
|
||||
<string notr="true">font: 8pt "Courier";</string>
|
||||
</property>
|
||||
<property name="readOnly">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
</widget>
|
||||
<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>
|
||||
|
|
Loading…
Reference in a new issue