History

ScriptEditor: Syntax highlighting update

Thijs Wenker committed 12 years ago

Commit e66822c1 parent 1634e574 files · +137 −11Browse files

Showing the changes to interface/src/ScriptHighlighting.cpp (1 of 4 files). Show all

Unified · Split

interface/src/ScriptHighlighting.cpp+55−2
@@ -11,8 +11,61 @@ · 10 unmodified lines
1111
1212 #include "ScriptHighlighting.h"
1313
14−ScriptHighlighting::ScriptHighlighting(QObject* parent) :
14+ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) :
1515 QSyntaxHighlighter(parent)
1616 {
17+ 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");
18+ qoutedTextRegex = QRegExp("\".*\"");
19+ multiLineCommentBegin = QRegExp("/\\*");
20+ multiLineCommentEnd = QRegExp("\\*/");
21+ numberRegex = QRegExp("[0-9]+(\\.[0-9]+){0,1}");
22+}
1723
18−}
24+void ScriptHighlighting::highlightBlock(const QString &text) {
25+ this->highlightKeywords(text);
26+ this->formatComments(text);
27+ this->formatQoutedText(text);
28+ this->formatNumbers(text);
29+}
30+
31+void ScriptHighlighting::highlightKeywords(const QString &text) {
32+ int index = keywordRegex.indexIn(text);
33+ while (index >= 0) {
34+ int length = keywordRegex.matchedLength();
35+ setFormat(index, length, Qt::blue);
36+ index = keywordRegex.indexIn(text, index + length);
37+ }
38+}
39+
40+void ScriptHighlighting::formatComments(const QString &text) {
41+
42+ setCurrentBlockState(BlockStateClean);
43+
44+ int start = (previousBlockState() != BlockStateInMultiComment) ? text.indexOf(multiLineCommentBegin) : 0;
45+
46+ while (start > -1) {
47+ int end = text.indexOf(multiLineCommentEnd, start);
48+ int length = (end == -1 ? text.length() : (end + multiLineCommentEnd.matchedLength())) - start;
49+ setFormat(start, length, Qt::lightGray);
50+ start = text.indexOf(multiLineCommentBegin, start + length);
51+ if (end == -1) setCurrentBlockState(BlockStateInMultiComment);
52+ }
53+}
54+
55+void ScriptHighlighting::formatQoutedText(const QString &text){
56+ int index = qoutedTextRegex.indexIn(text);
57+ while (index >= 0) {
58+ int length = qoutedTextRegex.matchedLength();
59+ setFormat(index, length, Qt::red);
60+ index = qoutedTextRegex.indexIn(text, index + length);
61+ }
62+}
63+
64+void ScriptHighlighting::formatNumbers(const QString &text){
65+ int index = numberRegex.indexIn(text);
66+ while (index >= 0) {
67+ int length = numberRegex.matchedLength();
68+ setFormat(index, length, Qt::green);
69+ index = numberRegex.indexIn(text, index + length);
70+ }
71+}