Add code blocks and python syntax highlighting.

This commit is contained in:
Adam Treat
2023-06-10 12:34:43 -04:00
committed by AT
parent b67cba19f0
commit 318c51c141
3 changed files with 277 additions and 24 deletions

View File

@@ -7,22 +7,12 @@
#include <QSyntaxHighlighter>
#include <QRegularExpression>
struct HighlightingRule
{
QRegularExpression pattern;
QTextCharFormat format;
};
class SyntaxHighlighter : public QSyntaxHighlighter {
Q_OBJECT
public:
SyntaxHighlighter(QObject *parent);
~SyntaxHighlighter();
void highlightBlock(const QString &text) override;
private:
QVector<HighlightingRule> m_highlightingRules;
};
struct ContextLink {
@@ -32,6 +22,12 @@ struct ContextLink {
QString href;
};
struct CodeCopy {
int startPos = -1;
int endPos = -1;
QString text;
};
class ResponseText : public QObject
{
Q_OBJECT
@@ -44,19 +40,26 @@ public:
void setTextDocument(QQuickTextDocument* textDocument);
Q_INVOKABLE void setLinkColor(const QColor &c) { m_linkColor = c; }
Q_INVOKABLE void setHeaderColor(const QColor &c) { m_headerColor = c; }
Q_INVOKABLE QString getLinkAtPosition(int position) const;
Q_INVOKABLE bool tryCopyAtPosition(int position) const;
Q_SIGNALS:
void textDocumentChanged();
private Q_SLOTS:
void handleTextChanged();
void handleContextLinks();
void handleCodeBlocks();
private:
QQuickTextDocument *m_textDocument;
SyntaxHighlighter *m_syntaxHighlighter;
QVector<ContextLink> m_links;
QVector<CodeCopy> m_copies;
QColor m_linkColor;
QColor m_headerColor;
bool m_isProcessingText = false;
};