Showing the changes to interface/src/ScriptHighlighting.cpp (1 of 4 files). Show all
Unified · Split
| @@ -11,8 +11,61 @@ · 10 unmodified lines | ||
| 11 | 11 | |
| 12 | 12 | #include "ScriptHighlighting.h" |
| 13 | 13 | |
| 14 | −ScriptHighlighting::ScriptHighlighting(QObject* parent) : | |
| 14 | +ScriptHighlighting::ScriptHighlighting(QTextDocument* parent) : | |
| 15 | 15 | QSyntaxHighlighter(parent) |
| 16 | 16 | { |
| 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 | +} | |
| 17 | 23 | |
| 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 | +} | |