2017-10-01 19:09:42 +00:00
|
|
|
#include "Highlighter.h"
|
|
|
|
#include "MainWindow.h"
|
2017-03-29 10:18:37 +00:00
|
|
|
|
2017-10-15 19:19:48 +00:00
|
|
|
#include <QtGui>
|
|
|
|
|
2017-10-09 18:08:35 +00:00
|
|
|
Highlighter::Highlighter(QTextDocument *parent) :
|
2017-03-29 10:18:37 +00:00
|
|
|
QSyntaxHighlighter(parent)
|
|
|
|
{
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2017-10-09 18:08:35 +00:00
|
|
|
core = CutterCore::getInstance();
|
|
|
|
|
2017-03-29 10:18:37 +00:00
|
|
|
keywordFormat.setForeground(QColor(65, 131, 215));
|
|
|
|
keywordFormat.setFontWeight(QFont::Bold);
|
|
|
|
|
2017-10-09 18:08:35 +00:00
|
|
|
foreach (const QString &pattern, this->core->opcodes)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
rule.pattern = QRegExp("\\b" + pattern + "\\b");
|
|
|
|
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
rule.format = keywordFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
regFormat.setForeground(QColor(236, 100, 75));
|
2017-03-29 10:18:37 +00:00
|
|
|
regFormat.setFontWeight(QFont::Bold);
|
|
|
|
|
2017-10-09 18:08:35 +00:00
|
|
|
foreach (const QString &pattern, this->core->regs)
|
2017-04-09 19:55:06 +00:00
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
rule.pattern = QRegExp("\\b" + pattern + "\\b");
|
|
|
|
rule.pattern.setCaseSensitivity(Qt::CaseInsensitive);
|
|
|
|
rule.format = regFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
singleLineCommentFormat.setFontWeight(QFont::Bold);
|
|
|
|
singleLineCommentFormat.setForeground(QColor(63, 195, 128));
|
|
|
|
rule.pattern = QRegExp(";[^\n]*");
|
|
|
|
rule.format = singleLineCommentFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
commentStartExpression = QRegExp("/\\*");
|
|
|
|
commentEndExpression = QRegExp("\\*/");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Highlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
2017-04-09 19:55:06 +00:00
|
|
|
foreach (const HighlightingRule &rule, highlightingRules)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
QRegExp expression(rule.pattern);
|
|
|
|
int index = expression.indexIn(text);
|
2017-04-09 19:55:06 +00:00
|
|
|
while (index >= 0)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
int length = expression.matchedLength();
|
|
|
|
setFormat(index, length, rule.format);
|
|
|
|
index = expression.indexIn(text, index + length);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setCurrentBlockState(0);
|
|
|
|
|
|
|
|
int startIndex = 0;
|
|
|
|
if (previousBlockState() != 1)
|
|
|
|
startIndex = commentStartExpression.indexIn(text);
|
|
|
|
|
2017-04-09 19:55:06 +00:00
|
|
|
while (startIndex >= 0)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
int endIndex = commentEndExpression.indexIn(text, startIndex);
|
|
|
|
int commentLength;
|
2017-04-09 19:55:06 +00:00
|
|
|
if (endIndex == -1)
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
setCurrentBlockState(1);
|
|
|
|
commentLength = text.length() - startIndex;
|
2017-04-09 19:55:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-03-29 10:18:37 +00:00
|
|
|
commentLength = endIndex - startIndex
|
|
|
|
+ commentEndExpression.matchedLength();
|
|
|
|
}
|
|
|
|
setFormat(startIndex, commentLength, multiLineCommentFormat);
|
|
|
|
startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
|
|
|
|
}
|
|
|
|
}
|