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

72 lines
2.2 KiB
C++
Raw Normal View History

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