2015-09-06 20:20:00 +02:00
|
|
|
#include "./javascripthighlighter.h"
|
2015-04-22 19:33:53 +02:00
|
|
|
|
|
|
|
namespace QtGui {
|
|
|
|
|
|
|
|
JavaScriptHighlighter::JavaScriptHighlighter(QTextDocument *parent)
|
|
|
|
: QSyntaxHighlighter(parent)
|
|
|
|
{
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
|
|
|
m_keywordFormat.setForeground(Qt::darkBlue);
|
|
|
|
m_keywordFormat.setFontWeight(QFont::Bold);
|
|
|
|
QStringList keywordPatterns;
|
|
|
|
keywordPatterns << "\\bvar\\b" << "\\bArray\\b" << "\\bfunction\\b"
|
|
|
|
<< "\\breturn\\b" << "\\barguments\\b" << "\\bif\\b"
|
|
|
|
<< "\\belse\\b" << "\\bfor\\b" << "\\bswitch\\b"
|
|
|
|
<< "\\bcase\\b" << "\\bbreak\\b" << "\\bwhile\\b"
|
|
|
|
<< "\\bundefined\\b" << "\\continue\\b";
|
|
|
|
foreach (const QString &pattern, keywordPatterns) {
|
|
|
|
rule.pattern = QRegExp(pattern);
|
|
|
|
rule.format = m_keywordFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
m_singleLineCommentFormat.setForeground(Qt::red);
|
|
|
|
rule.pattern = QRegExp("//[^\n]*");
|
|
|
|
rule.format = m_singleLineCommentFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
|
|
|
|
m_multiLineCommentFormat.setForeground(Qt::red);
|
|
|
|
|
|
|
|
m_quotationFormat.setForeground(Qt::darkGreen);
|
|
|
|
rule.pattern = QRegExp("\".*\"");
|
|
|
|
rule.format = m_quotationFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
|
|
|
|
m_functionFormat.setFontItalic(true);
|
|
|
|
m_functionFormat.setForeground(Qt::blue);
|
|
|
|
rule.pattern = QRegExp("(?!if)\\b[A-Za-z0-9_]+(?=\\()");
|
|
|
|
rule.format = m_functionFormat;
|
|
|
|
m_highlightingRules.append(rule);
|
|
|
|
|
|
|
|
m_commentStartExpression = QRegExp("/\\*");
|
|
|
|
m_commentEndExpression = QRegExp("\\*/");
|
|
|
|
}
|
|
|
|
|
|
|
|
void JavaScriptHighlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
|
|
|
foreach (const HighlightingRule &rule, m_highlightingRules) {
|
|
|
|
QRegExp expression(rule.pattern);
|
|
|
|
int index = expression.indexIn(text);
|
|
|
|
while (index >= 0) {
|
|
|
|
int length = expression.matchedLength();
|
|
|
|
setFormat(index, length, rule.format);
|
|
|
|
index = expression.indexIn(text, index + length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurrentBlockState(0);
|
|
|
|
|
|
|
|
int startIndex = 0;
|
|
|
|
if (previousBlockState() != 1)
|
|
|
|
startIndex = m_commentStartExpression.indexIn(text);
|
|
|
|
|
|
|
|
while (startIndex >= 0) {
|
|
|
|
int endIndex = m_commentEndExpression.indexIn(text, startIndex);
|
|
|
|
int commentLength;
|
|
|
|
if (endIndex == -1) {
|
|
|
|
setCurrentBlockState(1);
|
|
|
|
commentLength = text.length() - startIndex;
|
|
|
|
} else {
|
|
|
|
commentLength = endIndex - startIndex
|
|
|
|
+ m_commentEndExpression.matchedLength();
|
|
|
|
}
|
|
|
|
setFormat(startIndex, commentLength, m_multiLineCommentFormat);
|
|
|
|
startIndex = m_commentStartExpression.indexIn(text, startIndex + commentLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|