status-desktop/src-cpp/dotherside/StatusSyntaxHighlighter.cpp

68 lines
2.2 KiB
C++

#include "StatusSyntaxHighlighter.h"
#include <QQuickTextDocument>
StatusSyntaxHighlighter::StatusSyntaxHighlighter(QTextDocument *parent)
: QSyntaxHighlighter(parent)
{
HighlightingRule rule;
//BOLD
singlelineBoldFormat.setFontWeight(QFont::Bold);
rule.pattern = QRegularExpression(QStringLiteral("\\*\\*(.*?)\\*\\*"));
rule.format = singlelineBoldFormat;
highlightingRules.append(rule);
//BOLD
//ITALIC
singleLineItalicFormat.setFontItalic(true);
rule.pattern = QRegularExpression(QStringLiteral("\\*(.*?)\\*"));
rule.format = singleLineItalicFormat;
highlightingRules.append(rule);
//ITALIC
//CODE
singlelineCodeBlockFormat.setFontFamily("Roboto Mono");
rule.pattern = QRegularExpression(QStringLiteral("\\`(.*?)\\`"));
rule.format = singlelineCodeBlockFormat;
highlightingRules.append(rule);
//CODE
//STRIKETHROUGH
singleLineStrikeThroughFormat.setFontStrikeOut(true);
rule.pattern = QRegularExpression(QStringLiteral("\\~+(.*?)\\~+"));
rule.format = singleLineStrikeThroughFormat;
highlightingRules.append(rule);
//STRIKETHROUGH
//CODE BLOCK
multiLineCodeBlockFormat.setFontFamily("Roboto Mono");
rule.pattern = QRegularExpression(QStringLiteral("\\`\\`\\`(.*?)\\`\\`\\`"));
rule.format = multiLineCodeBlockFormat;
highlightingRules.append(rule);
//CODE BLOCK
}
void StatusSyntaxHighlighter::highlightBlock(const QString &text)
{
for (const HighlightingRule &rule : qAsConst(highlightingRules)) {
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
while (matchIterator.hasNext()) {
QRegularExpressionMatch match = matchIterator.next();
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
}
}
setCurrentBlockState(0);
}
QQuickTextDocument *StatusSyntaxHighlighterHelper::quickTextDocument() const {
return m_quicktextdocument;
}
void StatusSyntaxHighlighterHelper::setQuickTextDocument(
QQuickTextDocument *quickTextDocument) {
m_quicktextdocument = quickTextDocument;
if (m_quicktextdocument) {
new StatusSyntaxHighlighter(m_quicktextdocument->textDocument());
}
}