2023-02-06 12:52:03 +00:00
|
|
|
#include "StatusQ/statussyntaxhighlighter.h"
|
2023-01-06 22:39:19 +00:00
|
|
|
|
2021-08-05 15:25:45 +00:00
|
|
|
#include <QQuickTextDocument>
|
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
StatusSyntaxHighlighter::StatusSyntaxHighlighter(QObject* parent)
|
2021-08-05 15:25:45 +00:00
|
|
|
: QSyntaxHighlighter(parent)
|
2023-01-06 22:39:19 +00:00
|
|
|
{ }
|
|
|
|
|
|
|
|
void StatusSyntaxHighlighter::componentComplete()
|
2021-08-05 15:25:45 +00:00
|
|
|
{
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
//BOLD
|
2021-08-05 15:25:45 +00:00
|
|
|
singlelineBoldFormat.setFontWeight(QFont::Bold);
|
2022-06-10 15:50:49 +00:00
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("(\\*\\*(.*?)\\*\\*)|(\\_\\_(.*?)\\_\\_)"));
|
2021-08-05 15:25:45 +00:00
|
|
|
rule.format = singlelineBoldFormat;
|
|
|
|
highlightingRules.append(rule);
|
2023-01-06 22:39:19 +00:00
|
|
|
//BOLD
|
2021-08-05 15:25:45 +00:00
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
//ITALIC
|
2021-08-05 15:25:45 +00:00
|
|
|
singleLineItalicFormat.setFontItalic(true);
|
2022-06-10 15:50:49 +00:00
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("(\\*(.*?)\\*)|(\\_(.*?)\\_)"));
|
2021-08-05 15:25:45 +00:00
|
|
|
rule.format = singleLineItalicFormat;
|
|
|
|
highlightingRules.append(rule);
|
2023-01-06 22:39:19 +00:00
|
|
|
//ITALIC
|
2021-08-05 15:25:45 +00:00
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
//STRIKETHROUGH
|
2021-08-05 15:25:45 +00:00
|
|
|
singleLineStrikeThroughFormat.setFontStrikeOut(true);
|
2022-06-10 15:50:49 +00:00
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("\\~\\~(.*?)\\~\\~"));
|
2021-08-05 15:25:45 +00:00
|
|
|
rule.format = singleLineStrikeThroughFormat;
|
|
|
|
highlightingRules.append(rule);
|
2023-01-06 22:39:19 +00:00
|
|
|
//STRIKETHROUGH
|
|
|
|
|
|
|
|
//CODE (`foo`)
|
|
|
|
codeFormat.setFontFamily(QStringLiteral("Roboto Mono"));
|
|
|
|
codeFormat.setBackground(m_codeBackgroundColor);
|
|
|
|
codeFormat.setForeground(m_codeForegroundColor);
|
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("\\`{1}(.+)\\`{1}"),
|
|
|
|
// to not match single backtick pair inside a triple backtick block below
|
|
|
|
QRegularExpression::InvertedGreedinessOption);
|
|
|
|
rule.format = codeFormat;
|
|
|
|
highlightingRules.append(rule);
|
|
|
|
//CODE
|
2021-08-05 15:25:45 +00:00
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
//CODEBLOCK (```\nfoo\nbar```)
|
|
|
|
rule.pattern = QRegularExpression(QStringLiteral("\\`{3}(.+)\\`{3}"));
|
|
|
|
rule.format = codeFormat;
|
2021-08-05 15:25:45 +00:00
|
|
|
highlightingRules.append(rule);
|
2023-01-06 22:39:19 +00:00
|
|
|
//CODEBLOCK
|
2021-08-05 15:25:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
void StatusSyntaxHighlighter::highlightBlock(const QString& text)
|
2021-08-05 15:25:45 +00:00
|
|
|
{
|
2023-01-06 22:39:19 +00:00
|
|
|
for(const HighlightingRule& rule : qAsConst(highlightingRules))
|
|
|
|
{
|
|
|
|
QRegularExpressionMatchIterator matchIterator =
|
|
|
|
rule.pattern.globalMatch(text, 0, QRegularExpression::PartialPreferCompleteMatch);
|
|
|
|
while(matchIterator.hasNext())
|
|
|
|
{
|
|
|
|
const QRegularExpressionMatch match = matchIterator.next();
|
2021-08-05 15:25:45 +00:00
|
|
|
setFormat(match.capturedStart(), match.capturedLength(), rule.format);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
QQuickTextDocument* StatusSyntaxHighlighter::quickTextDocument() const
|
|
|
|
{
|
2021-08-05 15:25:45 +00:00
|
|
|
return m_quicktextdocument;
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:39:19 +00:00
|
|
|
void StatusSyntaxHighlighter::setQuickTextDocument(QQuickTextDocument* quickTextDocument)
|
|
|
|
{
|
|
|
|
if(!quickTextDocument) return;
|
|
|
|
if(quickTextDocument == m_quicktextdocument) return;
|
|
|
|
|
2021-08-05 15:25:45 +00:00
|
|
|
m_quicktextdocument = quickTextDocument;
|
2023-01-06 22:39:19 +00:00
|
|
|
setDocument(m_quicktextdocument->textDocument());
|
|
|
|
emit quickTextDocumentChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StatusSyntaxHighlighter::codeBackgroundColor() const
|
|
|
|
{
|
|
|
|
return m_codeBackgroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatusSyntaxHighlighter::setCodeBackgroundColor(const QColor& color)
|
|
|
|
{
|
|
|
|
if(color == m_codeBackgroundColor) return;
|
|
|
|
m_codeBackgroundColor = color;
|
|
|
|
emit codeBackgroundColorChanged();
|
|
|
|
}
|
|
|
|
|
|
|
|
QColor StatusSyntaxHighlighter::codeForegroundColor() const
|
|
|
|
{
|
|
|
|
return m_codeForegroundColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StatusSyntaxHighlighter::setCodeForegroundColor(const QColor& color)
|
|
|
|
{
|
|
|
|
if(color == m_codeForegroundColor) return;
|
|
|
|
m_codeForegroundColor = color;
|
|
|
|
emit codeForegroundColorChanged();
|
2021-08-05 15:25:45 +00:00
|
|
|
}
|