2024-06-28 11:10:20 -04:00
|
|
|
#include "chatviewtextprocessor.h"
|
2023-06-10 10:15:38 -04:00
|
|
|
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QBrush>
|
|
|
|
#include <QChar>
|
|
|
|
#include <QClipboard>
|
|
|
|
#include <QFont>
|
2023-06-10 10:15:38 -04:00
|
|
|
#include <QFontMetricsF>
|
2023-06-10 12:34:43 -04:00
|
|
|
#include <QGuiApplication>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QList>
|
2023-12-21 13:59:33 -05:00
|
|
|
#include <QPainter>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QQuickTextDocument>
|
|
|
|
#include <QRegularExpression>
|
2024-06-24 18:49:23 -04:00
|
|
|
#include <QStringList>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QTextBlock>
|
|
|
|
#include <QTextCharFormat>
|
|
|
|
#include <QTextCursor>
|
|
|
|
#include <QTextDocument>
|
2024-06-24 23:20:24 -04:00
|
|
|
#include <QTextDocumentFragment>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QTextFrame>
|
2024-06-24 18:49:23 -04:00
|
|
|
#include <QTextFrameFormat>
|
2024-06-04 14:47:11 -04:00
|
|
|
#include <QTextTableCell>
|
|
|
|
#include <QVariant>
|
2024-06-24 18:49:23 -04:00
|
|
|
#include <Qt>
|
|
|
|
#include <QtGlobal>
|
2023-06-10 12:34:43 -04:00
|
|
|
|
2024-06-28 11:46:20 -04:00
|
|
|
#include <algorithm>
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
enum Language {
|
|
|
|
None,
|
2023-06-11 19:58:48 -04:00
|
|
|
Python,
|
|
|
|
Cpp,
|
2023-06-19 16:12:37 -04:00
|
|
|
Bash,
|
2023-06-29 11:43:45 -04:00
|
|
|
TypeScript,
|
2023-07-05 11:04:13 -04:00
|
|
|
Java,
|
2023-07-10 16:23:32 -04:00
|
|
|
Go,
|
|
|
|
Json,
|
|
|
|
Csharp,
|
2023-07-13 17:01:08 -04:00
|
|
|
Latex,
|
|
|
|
Html,
|
2024-06-28 11:10:20 -04:00
|
|
|
Php,
|
|
|
|
Markdown
|
2023-06-10 12:34:43 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static Language stringToLanguage(const QString &language)
|
|
|
|
{
|
|
|
|
if (language == "python")
|
|
|
|
return Python;
|
2023-06-11 19:58:48 -04:00
|
|
|
if (language == "cpp")
|
|
|
|
return Cpp;
|
|
|
|
if (language == "c++")
|
|
|
|
return Cpp;
|
2023-07-10 16:23:32 -04:00
|
|
|
if (language == "csharp")
|
|
|
|
return Csharp;
|
|
|
|
if (language == "c#")
|
|
|
|
return Csharp;
|
2023-06-11 19:58:48 -04:00
|
|
|
if (language == "c")
|
|
|
|
return Cpp;
|
|
|
|
if (language == "bash")
|
2023-06-12 07:58:11 -04:00
|
|
|
return Bash;
|
2023-06-19 16:12:37 -04:00
|
|
|
if (language == "javascript")
|
|
|
|
return TypeScript;
|
|
|
|
if (language == "typescript")
|
|
|
|
return TypeScript;
|
2023-06-29 11:43:45 -04:00
|
|
|
if (language == "java")
|
|
|
|
return Java;
|
2023-07-05 11:04:13 -04:00
|
|
|
if (language == "go")
|
|
|
|
return Go;
|
|
|
|
if (language == "golang")
|
|
|
|
return Go;
|
2023-07-10 16:23:32 -04:00
|
|
|
if (language == "json")
|
|
|
|
return Json;
|
2023-07-13 17:01:08 -04:00
|
|
|
if (language == "latex")
|
|
|
|
return Latex;
|
|
|
|
if (language == "html")
|
|
|
|
return Html;
|
|
|
|
if (language == "php")
|
|
|
|
return Php;
|
2023-06-10 12:34:43 -04:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2024-06-30 19:15:01 -04:00
|
|
|
enum Code {
|
|
|
|
Default,
|
|
|
|
Keyword,
|
|
|
|
Function,
|
|
|
|
FunctionCall,
|
|
|
|
Comment,
|
|
|
|
String,
|
|
|
|
Number,
|
|
|
|
Header,
|
|
|
|
Preprocessor,
|
|
|
|
Type,
|
|
|
|
Arrow,
|
|
|
|
Command,
|
|
|
|
Variable,
|
|
|
|
Key,
|
|
|
|
Value,
|
|
|
|
Parameter,
|
|
|
|
AttributeName,
|
|
|
|
AttributeValue,
|
|
|
|
SpecialCharacter,
|
|
|
|
DocType
|
|
|
|
};
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
struct HighlightingRule {
|
|
|
|
QRegularExpression pattern;
|
2024-06-30 19:15:01 -04:00
|
|
|
Code format;
|
2023-06-10 12:34:43 -04:00
|
|
|
};
|
|
|
|
|
2024-06-30 19:15:01 -04:00
|
|
|
static QColor formatToColor(Code c, const CodeColors &colors)
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case Default: return colors.defaultColor;
|
|
|
|
case Keyword: return colors.keywordColor;
|
|
|
|
case Function: return colors.functionColor;
|
|
|
|
case FunctionCall: return colors.functionCallColor;
|
|
|
|
case Comment: return colors.commentColor;
|
|
|
|
case String: return colors.stringColor;
|
|
|
|
case Number: return colors.numberColor;
|
|
|
|
case Header: return colors.headerColor;
|
|
|
|
case Preprocessor: return colors.preprocessorColor;
|
|
|
|
case Type: return colors.typeColor;
|
|
|
|
case Arrow: return colors.arrowColor;
|
|
|
|
case Command: return colors.commandColor;
|
|
|
|
case Variable: return colors.variableColor;
|
|
|
|
case Key: return colors.keyColor;
|
|
|
|
case Value: return colors.valueColor;
|
|
|
|
case Parameter: return colors.parameterColor;
|
|
|
|
case AttributeName: return colors.attributeNameColor;
|
|
|
|
case AttributeValue: return colors.attributeValueColor;
|
|
|
|
case SpecialCharacter: return colors.specialCharacterColor;
|
|
|
|
case DocType: return colors.doctypeColor;
|
|
|
|
default: Q_UNREACHABLE();
|
|
|
|
}
|
|
|
|
return QColor();
|
|
|
|
}
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
static QVector<HighlightingRule> pythonHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = FunctionCall;
|
2023-06-10 12:34:43 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\bdef\\s+(\\w+)\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Function;
|
2023-06-10 12:34:43 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-06-10 12:34:43 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bdef\\b", "\\bclass\\b", "\\bif\\b", "\\belse\\b", "\\belif\\b",
|
|
|
|
"\\bwhile\\b", "\\bfor\\b", "\\breturn\\b", "\\bprint\\b", "\\bimport\\b",
|
|
|
|
"\\bfrom\\b", "\\bas\\b", "\\btry\\b", "\\bexcept\\b", "\\braise\\b",
|
|
|
|
"\\bwith\\b", "\\bfinally\\b", "\\bcontinue\\b", "\\bbreak\\b", "\\bpass\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-06-10 12:34:43 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-06-10 12:34:43 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\'.*?\'");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-06-10 12:34:43 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("#[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-06-10 12:34:43 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
2023-06-10 10:15:38 -04:00
|
|
|
|
2023-07-01 11:34:21 -04:00
|
|
|
static QVector<HighlightingRule> csharpHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-07-01 11:34:21 -04:00
|
|
|
// Function call highlighting
|
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = FunctionCall;
|
2023-07-01 11:34:21 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
// Function definition highlighting
|
|
|
|
rule.pattern = QRegularExpression("\\bvoid|int|double|string|bool\\s+(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Function;
|
2023-07-01 11:34:21 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
// Number highlighting
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-07-01 11:34:21 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
// Keyword highlighting
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bvoid\\b", "\\bint\\b", "\\bdouble\\b", "\\bstring\\b", "\\bbool\\b",
|
|
|
|
"\\bclass\\b", "\\bif\\b", "\\belse\\b", "\\bwhile\\b", "\\bfor\\b",
|
|
|
|
"\\breturn\\b", "\\bnew\\b", "\\bthis\\b", "\\bpublic\\b", "\\bprivate\\b",
|
|
|
|
"\\bprotected\\b", "\\bstatic\\b", "\\btrue\\b", "\\bfalse\\b", "\\bnull\\b",
|
|
|
|
"\\bnamespace\\b", "\\busing\\b", "\\btry\\b", "\\bcatch\\b", "\\bfinally\\b",
|
|
|
|
"\\bthrow\\b", "\\bvar\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-07-01 11:34:21 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
// String highlighting
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-07-01 11:34:21 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
// Single-line comment highlighting
|
|
|
|
rule.pattern = QRegularExpression("//[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-01 11:34:21 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
// Multi-line comment highlighting
|
|
|
|
rule.pattern = QRegularExpression("/\\*.*?\\*/");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-01 11:34:21 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
2023-06-11 19:58:48 -04:00
|
|
|
static QVector<HighlightingRule> cppHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-06-11 19:58:48 -04:00
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = FunctionCall;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[a-zA-Z_][a-zA-Z0-9_]*\\s+(\\w+)\\s*\\(");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Function;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bauto\\b", "\\bbool\\b", "\\bbreak\\b", "\\bcase\\b", "\\bcatch\\b",
|
|
|
|
"\\bchar\\b", "\\bclass\\b", "\\bconst\\b", "\\bconstexpr\\b", "\\bcontinue\\b",
|
|
|
|
"\\bdefault\\b", "\\bdelete\\b", "\\bdo\\b", "\\bdouble\\b", "\\belse\\b",
|
2023-06-12 07:58:11 -04:00
|
|
|
"\\belifdef\\b", "\\belifndef\\b", "\\bembed\\b", "\\benum\\b", "\\bexplicit\\b",
|
|
|
|
"\\bextern\\b", "\\bfalse\\b", "\\bfloat\\b", "\\bfor\\b", "\\bfriend\\b", "\\bgoto\\b",
|
|
|
|
"\\bif\\b", "\\binline\\b", "\\bint\\b", "\\blong\\b", "\\bmutable\\b", "\\bnamespace\\b",
|
|
|
|
"\\bnew\\b", "\\bnoexcept\\b", "\\bnullptr\\b", "\\boperator\\b", "\\boverride\\b",
|
|
|
|
"\\bprivate\\b", "\\bprotected\\b", "\\bpublic\\b", "\\bregister\\b", "\\breinterpret_cast\\b",
|
|
|
|
"\\breturn\\b", "\\bshort\\b", "\\bsigned\\b", "\\bsizeof\\b", "\\bstatic\\b", "\\bstatic_assert\\b",
|
2023-06-11 19:58:48 -04:00
|
|
|
"\\bstatic_cast\\b", "\\bstruct\\b", "\\bswitch\\b", "\\btemplate\\b", "\\bthis\\b",
|
|
|
|
"\\bthrow\\b", "\\btrue\\b", "\\btry\\b", "\\btypedef\\b", "\\btypeid\\b", "\\btypename\\b",
|
|
|
|
"\\bunion\\b", "\\bunsigned\\b", "\\busing\\b", "\\bvirtual\\b", "\\bvoid\\b",
|
|
|
|
"\\bvolatile\\b", "\\bwchar_t\\b", "\\bwhile\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\'.*?\'");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("//[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("/\\*.*?\\*/");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("#(?:include|define|undef|ifdef|ifndef|if|else|elif|endif|error|pragma)\\b.*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Preprocessor;
|
2023-06-11 19:58:48 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
2023-06-19 16:12:37 -04:00
|
|
|
static QVector<HighlightingRule> typescriptHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-06-19 16:12:37 -04:00
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = FunctionCall;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\bfunction\\s+(\\w+)\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Function;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bfunction\\b", "\\bvar\\b", "\\blet\\b", "\\bconst\\b", "\\bif\\b", "\\belse\\b",
|
|
|
|
"\\bfor\\b", "\\bwhile\\b", "\\breturn\\b", "\\btry\\b", "\\bcatch\\b", "\\bfinally\\b",
|
|
|
|
"\\bthrow\\b", "\\bnew\\b", "\\bdelete\\b", "\\btypeof\\b", "\\binstanceof\\b",
|
|
|
|
"\\bdo\\b", "\\bswitch\\b", "\\bcase\\b", "\\bbreak\\b", "\\bcontinue\\b",
|
|
|
|
"\\bpublic\\b", "\\bprivate\\b", "\\bprotected\\b", "\\bstatic\\b", "\\breadonly\\b",
|
|
|
|
"\\benum\\b", "\\binterface\\b", "\\bextends\\b", "\\bimplements\\b", "\\bexport\\b",
|
|
|
|
"\\bimport\\b", "\\btype\\b", "\\bnamespace\\b", "\\babstract\\b", "\\bas\\b",
|
|
|
|
"\\basync\\b", "\\bawait\\b", "\\bclass\\b", "\\bconstructor\\b", "\\bget\\b",
|
|
|
|
"\\bset\\b", "\\bnull\\b", "\\bundefined\\b", "\\btrue\\b", "\\bfalse\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList typePatterns = {
|
|
|
|
"\\bstring\\b", "\\bnumber\\b", "\\bboolean\\b", "\\bany\\b", "\\bvoid\\b",
|
|
|
|
"\\bnever\\b", "\\bunknown\\b", "\\bObject\\b", "\\bArray\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : typePatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Type;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"|'.*?'|`.*?`");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("//[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("/\\*.*?\\*/");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("=>");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Arrow;
|
2023-06-19 16:12:37 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
2023-06-29 11:43:45 -04:00
|
|
|
static QVector<HighlightingRule> javaHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-06-29 11:43:45 -04:00
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = FunctionCall;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\bvoid\\s+(\\w+)\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Function;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bpublic\\b", "\\bprivate\\b", "\\bprotected\\b", "\\bstatic\\b", "\\bfinal\\b",
|
|
|
|
"\\bclass\\b", "\\bif\\b", "\\belse\\b", "\\bwhile\\b", "\\bfor\\b",
|
|
|
|
"\\breturn\\b", "\\bnew\\b", "\\bimport\\b", "\\bpackage\\b", "\\btry\\b",
|
|
|
|
"\\bcatch\\b", "\\bthrow\\b", "\\bthrows\\b", "\\bfinally\\b", "\\binterface\\b",
|
|
|
|
"\\bextends\\b", "\\bimplements\\b", "\\bsuper\\b", "\\bthis\\b", "\\bvoid\\b",
|
|
|
|
"\\bboolean\\b", "\\bbyte\\b", "\\bchar\\b", "\\bdouble\\b", "\\bfloat\\b",
|
|
|
|
"\\bint\\b", "\\blong\\b", "\\bshort\\b", "\\bswitch\\b", "\\bcase\\b",
|
|
|
|
"\\bdefault\\b", "\\bcontinue\\b", "\\bbreak\\b", "\\babstract\\b", "\\bassert\\b",
|
|
|
|
"\\benum\\b", "\\binstanceof\\b", "\\bnative\\b", "\\bstrictfp\\b", "\\bsynchronized\\b",
|
|
|
|
"\\btransient\\b", "\\bvolatile\\b", "\\bconst\\b", "\\bgoto\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\'.*?\'");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("//[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("/\\*.*?\\*/");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-06-29 11:43:45 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
2023-07-05 11:04:13 -04:00
|
|
|
static QVector<HighlightingRule> goHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-07-05 11:04:13 -04:00
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = FunctionCall;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\bfunc\\s+(\\w+)\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Function;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bfunc\\b", "\\bpackage\\b", "\\bimport\\b", "\\bvar\\b", "\\bconst\\b",
|
|
|
|
"\\btype\\b", "\\bstruct\\b", "\\binterface\\b", "\\bfor\\b", "\\bif\\b",
|
|
|
|
"\\belse\\b", "\\bswitch\\b", "\\bcase\\b", "\\bdefault\\b", "\\breturn\\b",
|
|
|
|
"\\bbreak\\b", "\\bcontinue\\b", "\\bgoto\\b", "\\bfallthrough\\b",
|
|
|
|
"\\bdefer\\b", "\\bchan\\b", "\\bmap\\b", "\\brange\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("`.*?`");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("//[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("/\\*.*?\\*/");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
2023-06-11 19:58:48 -04:00
|
|
|
static QVector<HighlightingRule> bashHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
2023-07-05 11:04:13 -04:00
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-07-05 11:04:13 -04:00
|
|
|
QStringList commandPatterns = {
|
|
|
|
"\\b(grep|awk|sed|ls|cat|echo|rm|mkdir|cp|break|alias|eval|cd|exec|head|tail|strings|printf|touch|mv|chmod)\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : commandPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Command;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bif\\b", "\\bthen\\b", "\\belse\\b", "\\bfi\\b", "\\bfor\\b",
|
|
|
|
"\\bin\\b", "\\bdo\\b", "\\bdone\\b", "\\bwhile\\b", "\\buntil\\b",
|
|
|
|
"\\bcase\\b", "\\besac\\b", "\\bfunction\\b", "\\breturn\\b",
|
|
|
|
"\\blocal\\b", "\\bdeclare\\b", "\\bunset\\b", "\\bexport\\b",
|
|
|
|
"\\breadonly\\b", "\\bshift\\b", "\\bexit\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\'.*?\'");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\$(\\w+|\\{[^}]+\\})");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Variable;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("#[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-05 11:04:13 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-06-11 19:58:48 -04:00
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
2023-07-13 17:01:08 -04:00
|
|
|
static QVector<HighlightingRule> latexHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-07-13 17:01:08 -04:00
|
|
|
rule.pattern = QRegularExpression("\\\\[A-Za-z]+"); // Pattern for LaTeX commands
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Command;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("%[^\n]*"); // Pattern for LaTeX comments
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QVector<HighlightingRule> htmlHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-07-13 17:01:08 -04:00
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*=");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = AttributeName;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"|'.*?'");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = AttributeValue;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("<!--.*?-->");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("&[a-zA-Z0-9#]*;");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = SpecialCharacter;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("<!DOCTYPE.*?>");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = DocType;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QVector<HighlightingRule> phpHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-07-13 17:01:08 -04:00
|
|
|
rule.pattern = QRegularExpression("\\b(\\w+)\\s*(?=\\()");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = FunctionCall;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\bfunction\\s+(\\w+)\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Function;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\\b[0-9]*\\.?[0-9]+\\b");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Number;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
QStringList keywordPatterns = {
|
|
|
|
"\\bif\\b", "\\belse\\b", "\\belseif\\b", "\\bwhile\\b", "\\bfor\\b",
|
|
|
|
"\\bforeach\\b", "\\breturn\\b", "\\bprint\\b", "\\binclude\\b", "\\brequire\\b",
|
|
|
|
"\\binclude_once\\b", "\\brequire_once\\b", "\\btry\\b", "\\bcatch\\b",
|
|
|
|
"\\bfinally\\b", "\\bcontinue\\b", "\\bbreak\\b", "\\bclass\\b", "\\bfunction\\b",
|
|
|
|
"\\bnew\\b", "\\bthrow\\b", "\\barray\\b", "\\bpublic\\b", "\\bprivate\\b",
|
|
|
|
"\\bprotected\\b", "\\bstatic\\b", "\\bglobal\\b", "\\bisset\\b", "\\bunset\\b",
|
|
|
|
"\\bnull\\b", "\\btrue\\b", "\\bfalse\\b"
|
|
|
|
};
|
|
|
|
|
|
|
|
for (const QString &pattern : keywordPatterns) {
|
|
|
|
rule.pattern = QRegularExpression(pattern);
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Keyword;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\".*?\"");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("\'.*?\'");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = String;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("//[^\n]*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
rule.pattern = QRegularExpression("/\\*.*?\\*/");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Comment;
|
2023-07-13 17:01:08 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-10 16:23:32 -04:00
|
|
|
static QVector<HighlightingRule> jsonHighlightingRules()
|
|
|
|
{
|
|
|
|
static QVector<HighlightingRule> highlightingRules;
|
|
|
|
if (highlightingRules.isEmpty()) {
|
|
|
|
|
|
|
|
HighlightingRule rule;
|
|
|
|
|
2023-07-31 12:18:19 -04:00
|
|
|
rule.pattern = QRegularExpression(".*");
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Default;
|
2023-07-31 12:18:19 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
2023-07-10 16:23:32 -04:00
|
|
|
// Key string rule
|
|
|
|
rule.pattern = QRegularExpression("\".*?\":"); // keys are typically in the "key": format
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Key;
|
2023-07-10 16:23:32 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
|
|
|
|
// Value string rule
|
|
|
|
rule.pattern = QRegularExpression(":\\s*(\".*?\")"); // values are typically in the : "value" format
|
2024-06-30 19:15:01 -04:00
|
|
|
rule.format = Value;
|
2023-07-10 16:23:32 -04:00
|
|
|
highlightingRules.append(rule);
|
|
|
|
}
|
|
|
|
return highlightingRules;
|
|
|
|
}
|
|
|
|
|
2023-06-10 10:15:38 -04:00
|
|
|
SyntaxHighlighter::SyntaxHighlighter(QObject *parent)
|
|
|
|
: QSyntaxHighlighter(parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SyntaxHighlighter::~SyntaxHighlighter()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void SyntaxHighlighter::highlightBlock(const QString &text)
|
|
|
|
{
|
2023-06-10 12:34:43 -04:00
|
|
|
QTextBlock block = this->currentBlock();
|
|
|
|
|
2024-06-29 17:59:45 -04:00
|
|
|
// Search the first block of the frame we're in for the code to use for highlighting
|
|
|
|
int userState = block.userState();
|
|
|
|
if (QTextFrame *frame = block.document()->frameAt(block.position())) {
|
|
|
|
QTextBlock firstBlock = frame->begin().currentBlock();
|
|
|
|
if (firstBlock.isValid())
|
|
|
|
userState = firstBlock.userState();
|
|
|
|
}
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
QVector<HighlightingRule> rules;
|
2024-06-29 17:59:45 -04:00
|
|
|
switch (userState) {
|
|
|
|
case Python:
|
|
|
|
rules = pythonHighlightingRules(); break;
|
|
|
|
case Cpp:
|
|
|
|
rules = cppHighlightingRules(); break;
|
|
|
|
case Csharp:
|
|
|
|
rules = csharpHighlightingRules(); break;
|
|
|
|
case Bash:
|
|
|
|
rules = bashHighlightingRules(); break;
|
|
|
|
case TypeScript:
|
|
|
|
rules = typescriptHighlightingRules(); break;
|
|
|
|
case Java:
|
|
|
|
rules = javaHighlightingRules(); break;
|
|
|
|
case Go:
|
|
|
|
rules = javaHighlightingRules(); break;
|
|
|
|
case Json:
|
|
|
|
rules = jsonHighlightingRules(); break;
|
|
|
|
case Latex:
|
|
|
|
rules = latexHighlightingRules(); break;
|
|
|
|
case Html:
|
|
|
|
rules = htmlHighlightingRules(); break;
|
|
|
|
case Php:
|
|
|
|
rules = phpHighlightingRules(); break;
|
|
|
|
default: break;
|
|
|
|
}
|
2023-06-10 12:34:43 -04:00
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
for (const HighlightingRule &rule : std::as_const(rules)) {
|
2023-06-10 10:15:38 -04:00
|
|
|
QRegularExpressionMatchIterator matchIterator = rule.pattern.globalMatch(text);
|
|
|
|
while (matchIterator.hasNext()) {
|
|
|
|
QRegularExpressionMatch match = matchIterator.next();
|
2023-06-10 12:34:43 -04:00
|
|
|
int startIndex = match.capturedStart();
|
|
|
|
int length = match.capturedLength();
|
2024-06-30 19:15:01 -04:00
|
|
|
QTextCharFormat format;
|
|
|
|
format.setForeground(formatToColor(rule.format, m_codeColors));
|
|
|
|
setFormat(startIndex, length, format);
|
2023-06-10 10:15:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
// TODO (Adam) This class replaces characters in the text in order to provide markup and syntax highlighting
|
|
|
|
// which destroys the original text in favor of the replaced text. This is a problem when we select
|
|
|
|
// text and then the user tries to 'copy' the text: the original text should be placed in the clipboard
|
|
|
|
// not the replaced text. A possible solution is to have this class keep a mapping of the original
|
|
|
|
// indices and the replacement indices and then use the original text that is stored in memory in the
|
|
|
|
// chat class to populate the clipboard.
|
2024-06-28 11:10:20 -04:00
|
|
|
ChatViewTextProcessor::ChatViewTextProcessor(QObject *parent)
|
2023-06-10 10:15:38 -04:00
|
|
|
: QObject{parent}
|
|
|
|
, m_textDocument(nullptr)
|
|
|
|
, m_syntaxHighlighter(new SyntaxHighlighter(this))
|
|
|
|
, m_isProcessingText(false)
|
2024-06-28 11:10:20 -04:00
|
|
|
, m_shouldProcessText(true)
|
2024-06-30 15:10:19 -04:00
|
|
|
, m_fontPixelSize(QGuiApplication::font().pointSizeF())
|
2023-06-10 10:15:38 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
QQuickTextDocument* ChatViewTextProcessor::textDocument() const
|
2023-06-10 10:15:38 -04:00
|
|
|
{
|
|
|
|
return m_textDocument;
|
|
|
|
}
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
void ChatViewTextProcessor::setTextDocument(QQuickTextDocument* textDocument)
|
2023-06-10 10:15:38 -04:00
|
|
|
{
|
|
|
|
if (m_textDocument)
|
2024-06-28 11:10:20 -04:00
|
|
|
disconnect(m_textDocument->textDocument(), &QTextDocument::contentsChanged, this, &ChatViewTextProcessor::handleTextChanged);
|
2023-06-10 10:15:38 -04:00
|
|
|
|
|
|
|
m_textDocument = textDocument;
|
|
|
|
m_syntaxHighlighter->setDocument(m_textDocument->textDocument());
|
2024-06-28 11:10:20 -04:00
|
|
|
connect(m_textDocument->textDocument(), &QTextDocument::contentsChanged, this, &ChatViewTextProcessor::handleTextChanged);
|
2023-06-12 08:34:59 -04:00
|
|
|
handleTextChanged();
|
2023-06-10 10:15:38 -04:00
|
|
|
}
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
bool ChatViewTextProcessor::tryCopyAtPosition(int position) const
|
2023-06-10 12:34:43 -04:00
|
|
|
{
|
|
|
|
for (const auto © : m_copies) {
|
2023-06-20 18:59:08 -04:00
|
|
|
if (position >= copy.startPos && position <= copy.endPos) {
|
2023-06-10 12:34:43 -04:00
|
|
|
QClipboard *clipboard = QGuiApplication::clipboard();
|
|
|
|
clipboard->setText(copy.text);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
bool ChatViewTextProcessor::shouldProcessText() const
|
2023-06-10 10:15:38 -04:00
|
|
|
{
|
2024-06-28 11:10:20 -04:00
|
|
|
return m_shouldProcessText;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatViewTextProcessor::setShouldProcessText(bool b)
|
|
|
|
{
|
|
|
|
if (m_shouldProcessText == b)
|
|
|
|
return;
|
|
|
|
m_shouldProcessText = b;
|
|
|
|
emit shouldProcessTextChanged();
|
|
|
|
handleTextChanged();
|
|
|
|
}
|
|
|
|
|
2024-06-30 15:10:19 -04:00
|
|
|
qreal ChatViewTextProcessor::fontPixelSize() const
|
|
|
|
{
|
|
|
|
return m_fontPixelSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatViewTextProcessor::setFontPixelSize(qreal sz)
|
|
|
|
{
|
|
|
|
if (m_fontPixelSize == sz)
|
|
|
|
return;
|
|
|
|
m_fontPixelSize = sz;
|
|
|
|
emit fontPixelSizeChanged();
|
|
|
|
handleTextChanged();
|
|
|
|
}
|
|
|
|
|
2024-06-30 19:15:01 -04:00
|
|
|
CodeColors ChatViewTextProcessor::codeColors() const
|
|
|
|
{
|
|
|
|
return m_syntaxHighlighter->codeColors();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatViewTextProcessor::setCodeColors(const CodeColors &colors)
|
|
|
|
{
|
|
|
|
m_syntaxHighlighter->setCodeColors(colors);
|
|
|
|
emit codeColorsChanged();
|
|
|
|
}
|
|
|
|
|
2024-06-29 17:59:45 -04:00
|
|
|
void traverseDocument(QTextDocument *doc, QTextFrame *frame)
|
2024-06-28 11:10:20 -04:00
|
|
|
{
|
2024-06-29 17:59:45 -04:00
|
|
|
QTextFrame *rootFrame = frame ? frame : doc->rootFrame();
|
2024-06-28 11:10:20 -04:00
|
|
|
QTextFrame::iterator rootIt;
|
|
|
|
|
2024-06-29 17:59:45 -04:00
|
|
|
if (!frame)
|
|
|
|
qDebug() << "Begin traverse";
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
for (rootIt = rootFrame->begin(); !rootIt.atEnd(); ++rootIt) {
|
|
|
|
QTextFrame *childFrame = rootIt.currentFrame();
|
|
|
|
QTextBlock childBlock = rootIt.currentBlock();
|
|
|
|
|
|
|
|
if (childFrame) {
|
|
|
|
qDebug() << "Frame from" << childFrame->firstPosition() << "to" << childFrame->lastPosition();
|
2024-06-29 17:59:45 -04:00
|
|
|
traverseDocument(doc, childFrame);
|
2024-06-28 11:10:20 -04:00
|
|
|
} else if (childBlock.isValid()) {
|
2024-06-29 17:59:45 -04:00
|
|
|
qDebug() << QString(" Block %1 position:").arg(childBlock.userState()) << childBlock.position();
|
|
|
|
qDebug() << QString(" Block %1 text:").arg(childBlock.userState()) << childBlock.text();
|
2024-06-28 11:10:20 -04:00
|
|
|
|
|
|
|
// Iterate over lines within the block
|
|
|
|
for (QTextBlock::iterator blockIt = childBlock.begin(); !(blockIt.atEnd()); ++blockIt) {
|
|
|
|
QTextFragment fragment = blockIt.fragment();
|
|
|
|
if (fragment.isValid()) {
|
|
|
|
qDebug() << " Fragment text:" << fragment.text();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-29 17:59:45 -04:00
|
|
|
|
|
|
|
if (!frame)
|
|
|
|
qDebug() << "End traverse";
|
2024-06-28 11:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatViewTextProcessor::handleTextChanged()
|
|
|
|
{
|
|
|
|
if (!m_textDocument || m_isProcessingText || !m_shouldProcessText)
|
2023-06-10 10:15:38 -04:00
|
|
|
return;
|
|
|
|
|
|
|
|
m_isProcessingText = true;
|
2024-06-24 18:49:23 -04:00
|
|
|
|
|
|
|
// Force full layout of the text document to work around a bug in Qt
|
|
|
|
// TODO(jared): report the Qt bug and link to the report here
|
|
|
|
QTextDocument* doc = m_textDocument->textDocument();
|
|
|
|
(void)doc->documentLayout()->documentSize();
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
handleCodeBlocks();
|
2024-06-28 11:10:20 -04:00
|
|
|
handleMarkdown();
|
2024-06-24 18:49:23 -04:00
|
|
|
|
2023-06-19 17:51:55 -04:00
|
|
|
// We insert an invisible char at the end to make sure the document goes back to the default
|
|
|
|
// text format
|
|
|
|
QTextCursor cursor(doc);
|
|
|
|
QString invisibleCharacter = QString(QChar(0xFEFF));
|
|
|
|
cursor.insertText(invisibleCharacter, QTextCharFormat());
|
2023-06-10 12:34:43 -04:00
|
|
|
m_isProcessingText = false;
|
|
|
|
}
|
2023-06-10 10:15:38 -04:00
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
void ChatViewTextProcessor::handleCodeBlocks()
|
2023-06-10 12:34:43 -04:00
|
|
|
{
|
|
|
|
QTextDocument* doc = m_textDocument->textDocument();
|
|
|
|
QTextCursor cursor(doc);
|
|
|
|
|
|
|
|
QTextCharFormat textFormat;
|
|
|
|
textFormat.setFontFamilies(QStringList() << "Monospace");
|
|
|
|
textFormat.setForeground(QColor("white"));
|
|
|
|
|
|
|
|
QTextFrameFormat frameFormatBase;
|
2024-06-30 19:15:01 -04:00
|
|
|
frameFormatBase.setBackground(codeColors().backgroundColor);
|
2023-06-10 12:34:43 -04:00
|
|
|
|
|
|
|
QTextTableFormat tableFormat;
|
|
|
|
tableFormat.setMargin(0);
|
|
|
|
tableFormat.setPadding(0);
|
|
|
|
tableFormat.setBorder(0);
|
|
|
|
tableFormat.setBorderCollapse(true);
|
|
|
|
QList<QTextLength> constraints;
|
|
|
|
constraints << QTextLength(QTextLength::PercentageLength, 100);
|
|
|
|
tableFormat.setColumnWidthConstraints(constraints);
|
|
|
|
|
|
|
|
QTextTableFormat headerTableFormat;
|
2024-06-30 19:15:01 -04:00
|
|
|
headerTableFormat.setBackground(codeColors().headerColor);
|
2023-06-10 12:34:43 -04:00
|
|
|
headerTableFormat.setPadding(0);
|
|
|
|
headerTableFormat.setBorder(0);
|
|
|
|
headerTableFormat.setBorderCollapse(true);
|
2024-06-30 19:15:01 -04:00
|
|
|
headerTableFormat.setTopMargin(10);
|
|
|
|
headerTableFormat.setBottomMargin(10);
|
|
|
|
headerTableFormat.setLeftMargin(15);
|
|
|
|
headerTableFormat.setRightMargin(15);
|
2023-06-10 12:34:43 -04:00
|
|
|
QList<QTextLength> headerConstraints;
|
|
|
|
headerConstraints << QTextLength(QTextLength::PercentageLength, 80);
|
|
|
|
headerConstraints << QTextLength(QTextLength::PercentageLength, 20);
|
|
|
|
headerTableFormat.setColumnWidthConstraints(headerConstraints);
|
|
|
|
|
|
|
|
QTextTableFormat codeBlockTableFormat;
|
2024-06-30 19:15:01 -04:00
|
|
|
codeBlockTableFormat.setBackground(codeColors().backgroundColor);
|
2023-06-10 12:34:43 -04:00
|
|
|
codeBlockTableFormat.setPadding(0);
|
|
|
|
codeBlockTableFormat.setBorder(0);
|
|
|
|
codeBlockTableFormat.setBorderCollapse(true);
|
2024-06-30 19:15:01 -04:00
|
|
|
codeBlockTableFormat.setTopMargin(15);
|
|
|
|
codeBlockTableFormat.setBottomMargin(15);
|
|
|
|
codeBlockTableFormat.setLeftMargin(15);
|
|
|
|
codeBlockTableFormat.setRightMargin(15);
|
2023-06-10 12:34:43 -04:00
|
|
|
codeBlockTableFormat.setColumnWidthConstraints(constraints);
|
|
|
|
|
|
|
|
QTextImageFormat copyImageFormat;
|
2024-06-30 19:15:01 -04:00
|
|
|
copyImageFormat.setWidth(24);
|
|
|
|
copyImageFormat.setHeight(24);
|
2023-06-10 12:34:43 -04:00
|
|
|
copyImageFormat.setName("qrc:/gpt4all/icons/copy.svg");
|
|
|
|
|
|
|
|
// Regex for code blocks
|
2023-06-12 08:34:59 -04:00
|
|
|
static const QRegularExpression reCode("```(.*?)(```|$)", QRegularExpression::DotMatchesEverythingOption);
|
2023-06-10 12:34:43 -04:00
|
|
|
QRegularExpressionMatchIterator iCode = reCode.globalMatch(doc->toPlainText());
|
|
|
|
|
|
|
|
QList<QRegularExpressionMatch> matchesCode;
|
|
|
|
while (iCode.hasNext())
|
|
|
|
matchesCode.append(iCode.next());
|
|
|
|
|
|
|
|
QVector<CodeCopy> newCopies;
|
2024-06-28 11:10:20 -04:00
|
|
|
QVector<QTextFrame*> frames;
|
2024-06-24 23:20:24 -04:00
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
for(int index = matchesCode.count() - 1; index >= 0; --index) {
|
|
|
|
cursor.setPosition(matchesCode[index].capturedStart());
|
|
|
|
cursor.setPosition(matchesCode[index].capturedEnd(), QTextCursor::KeepAnchor);
|
|
|
|
cursor.removeSelectedText();
|
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
int startPos = cursor.position();
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
QTextFrameFormat frameFormat = frameFormatBase;
|
2023-06-12 08:34:59 -04:00
|
|
|
QString capturedText = matchesCode[index].captured(1);
|
2023-06-10 12:34:43 -04:00
|
|
|
QString codeLanguage;
|
2023-06-12 08:34:59 -04:00
|
|
|
|
2024-04-18 14:50:32 -04:00
|
|
|
QStringList lines = capturedText.split('\n');
|
|
|
|
if (lines.last().isEmpty()) {
|
|
|
|
lines.removeLast();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lines.count() >= 2) {
|
|
|
|
const auto &firstWord = lines.first();
|
2023-06-12 08:34:59 -04:00
|
|
|
if (firstWord == "python"
|
|
|
|
|| firstWord == "cpp"
|
|
|
|
|| firstWord == "c++"
|
2023-07-10 16:23:32 -04:00
|
|
|
|| firstWord == "csharp"
|
|
|
|
|| firstWord == "c#"
|
2023-06-12 08:34:59 -04:00
|
|
|
|| firstWord == "c"
|
2023-06-19 16:12:37 -04:00
|
|
|
|| firstWord == "bash"
|
|
|
|
|| firstWord == "javascript"
|
2023-06-29 11:43:45 -04:00
|
|
|
|| firstWord == "typescript"
|
2023-07-05 11:04:13 -04:00
|
|
|
|| firstWord == "java"
|
|
|
|
|| firstWord == "go"
|
2023-07-10 16:23:32 -04:00
|
|
|
|| firstWord == "golang"
|
2023-07-13 17:01:08 -04:00
|
|
|
|| firstWord == "json"
|
|
|
|
|| firstWord == "latex"
|
|
|
|
|| firstWord == "html"
|
|
|
|
|| firstWord == "php") {
|
2023-06-12 08:34:59 -04:00
|
|
|
codeLanguage = firstWord;
|
|
|
|
}
|
2024-04-18 14:50:32 -04:00
|
|
|
lines.removeFirst();
|
2023-06-11 19:58:48 -04:00
|
|
|
}
|
2023-06-12 08:34:59 -04:00
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
QTextFrame *mainFrame = cursor.currentFrame();
|
|
|
|
cursor.setCharFormat(textFormat);
|
|
|
|
|
|
|
|
QTextFrame *frame = cursor.insertFrame(frameFormat);
|
|
|
|
QTextTable *table = cursor.insertTable(codeLanguage.isEmpty() ? 1 : 2, 1, tableFormat);
|
|
|
|
|
|
|
|
if (!codeLanguage.isEmpty()) {
|
|
|
|
QTextTableCell headerCell = table->cellAt(0, 0);
|
|
|
|
QTextCursor headerCellCursor = headerCell.firstCursorPosition();
|
|
|
|
QTextTable *headerTable = headerCellCursor.insertTable(1, 2, headerTableFormat);
|
|
|
|
QTextTableCell header = headerTable->cellAt(0, 0);
|
|
|
|
QTextCursor headerCursor = header.firstCursorPosition();
|
|
|
|
headerCursor.insertText(codeLanguage);
|
|
|
|
QTextTableCell copy = headerTable->cellAt(0, 1);
|
|
|
|
QTextCursor copyCursor = copy.firstCursorPosition();
|
|
|
|
int startPos = copyCursor.position();
|
|
|
|
CodeCopy newCopy;
|
|
|
|
newCopy.text = lines.join("\n");
|
|
|
|
newCopy.startPos = copyCursor.position();
|
|
|
|
newCopy.endPos = newCopy.startPos + 1;
|
|
|
|
newCopies.append(newCopy);
|
2024-06-30 19:15:01 -04:00
|
|
|
// FIXME: There are two reasons this is commented out. Odd drawing behavior is seen when this is added
|
|
|
|
// and one selects with the mouse the code language in a code block. The other reason is the code that
|
|
|
|
// tries to do a hit test for the image is just very broken and buggy and does not always work. So I'm
|
|
|
|
// disabling this code and included functionality for v3.0.0 until I can figure out how to make this much
|
|
|
|
// less buggy
|
|
|
|
#if 0
|
|
|
|
// QTextBlockFormat blockFormat;
|
|
|
|
// blockFormat.setAlignment(Qt::AlignRight);
|
|
|
|
// copyCursor.setBlockFormat(blockFormat);
|
|
|
|
// copyCursor.insertImage(copyImageFormat, QTextFrameFormat::FloatRight);
|
|
|
|
#endif
|
2023-06-10 12:34:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QTextTableCell codeCell = table->cellAt(codeLanguage.isEmpty() ? 0 : 1, 0);
|
|
|
|
QTextCursor codeCellCursor = codeCell.firstCursorPosition();
|
|
|
|
QTextTable *codeTable = codeCellCursor.insertTable(1, 1, codeBlockTableFormat);
|
|
|
|
QTextTableCell code = codeTable->cellAt(0, 0);
|
2023-07-05 09:29:30 -04:00
|
|
|
|
|
|
|
QTextCharFormat codeBlockCharFormat;
|
2024-06-30 19:15:01 -04:00
|
|
|
codeBlockCharFormat.setForeground(codeColors().defaultColor);
|
2024-06-24 18:49:23 -04:00
|
|
|
|
2023-07-05 09:29:30 -04:00
|
|
|
QFont monospaceFont("Courier");
|
2024-06-30 15:10:19 -04:00
|
|
|
monospaceFont.setPointSize(m_fontPixelSize);
|
2023-07-05 09:29:30 -04:00
|
|
|
if (monospaceFont.family() != "Courier") {
|
|
|
|
monospaceFont.setFamily("Monospace"); // Fallback if Courier isn't available
|
|
|
|
}
|
|
|
|
|
2023-06-10 12:34:43 -04:00
|
|
|
QTextCursor codeCursor = code.firstCursorPosition();
|
2023-07-05 09:29:30 -04:00
|
|
|
codeBlockCharFormat.setFont(monospaceFont); // Update the font for the codeblock
|
|
|
|
codeCursor.setCharFormat(codeBlockCharFormat);
|
|
|
|
|
2024-06-24 18:49:23 -04:00
|
|
|
codeCursor.block().setUserState(stringToLanguage(codeLanguage));
|
2024-06-29 17:59:45 -04:00
|
|
|
codeCursor.insertText(lines.join('\n'));
|
2023-06-10 12:34:43 -04:00
|
|
|
|
|
|
|
cursor = mainFrame->lastCursorPosition();
|
|
|
|
cursor.setCharFormat(QTextCharFormat());
|
2024-06-28 11:10:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
m_copies = newCopies;
|
|
|
|
}
|
|
|
|
|
|
|
|
void replaceAndInsertMarkdown(int startIndex, int endIndex, QTextDocument *doc)
|
|
|
|
{
|
|
|
|
QTextCursor cursor(doc);
|
|
|
|
cursor.setPosition(startIndex);
|
|
|
|
cursor.setPosition(endIndex, QTextCursor::KeepAnchor);
|
|
|
|
QTextDocumentFragment fragment(cursor);
|
|
|
|
const QString plainText = fragment.toPlainText();
|
|
|
|
cursor.removeSelectedText();
|
2024-06-28 11:36:34 -04:00
|
|
|
QTextDocument::MarkdownFeatures features = static_cast<QTextDocument::MarkdownFeatures>(
|
|
|
|
QTextDocument::MarkdownNoHTML | QTextDocument::MarkdownDialectGitHub);
|
|
|
|
cursor.insertMarkdown(plainText, features);
|
2024-06-28 11:10:20 -04:00
|
|
|
cursor.block().setUserState(Markdown);
|
|
|
|
}
|
2024-06-24 23:20:24 -04:00
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
void ChatViewTextProcessor::handleMarkdown()
|
|
|
|
{
|
|
|
|
QTextDocument* doc = m_textDocument->textDocument();
|
|
|
|
QTextCursor cursor(doc);
|
|
|
|
|
|
|
|
QVector<QPair<int, int>> codeBlockPositions;
|
|
|
|
|
|
|
|
QTextFrame *rootFrame = doc->rootFrame();
|
|
|
|
QTextFrame::iterator rootIt;
|
|
|
|
|
|
|
|
bool hasAlreadyProcessedMarkdown = false;
|
|
|
|
for (rootIt = rootFrame->begin(); !rootIt.atEnd(); ++rootIt) {
|
|
|
|
QTextFrame *childFrame = rootIt.currentFrame();
|
|
|
|
QTextBlock childBlock = rootIt.currentBlock();
|
|
|
|
if (childFrame) {
|
|
|
|
codeBlockPositions.append(qMakePair(childFrame->firstPosition()-1, childFrame->lastPosition()+1));
|
|
|
|
|
|
|
|
for (QTextFrame::iterator frameIt = childFrame->begin(); !frameIt.atEnd(); ++frameIt) {
|
|
|
|
QTextBlock block = frameIt.currentBlock();
|
|
|
|
if (block.isValid() && block.userState() == Markdown)
|
|
|
|
hasAlreadyProcessedMarkdown = true;
|
|
|
|
}
|
|
|
|
} else if (childBlock.isValid() && childBlock.userState() == Markdown)
|
|
|
|
hasAlreadyProcessedMarkdown = true;
|
2023-06-10 12:34:43 -04:00
|
|
|
}
|
|
|
|
|
2024-06-24 23:20:24 -04:00
|
|
|
|
2024-06-28 11:10:20 -04:00
|
|
|
if (!hasAlreadyProcessedMarkdown) {
|
2024-06-28 11:47:31 -04:00
|
|
|
std::sort(codeBlockPositions.begin(), codeBlockPositions.end(), [](const QPair<int, int> &a, const QPair<int, int> &b) {
|
|
|
|
return a.first > b.first;
|
|
|
|
});
|
2024-06-28 11:10:20 -04:00
|
|
|
|
|
|
|
int lastIndex = doc->characterCount() - 1;
|
|
|
|
for (const auto &pos : codeBlockPositions) {
|
|
|
|
int nonCodeStart = pos.second;
|
|
|
|
int nonCodeEnd = lastIndex;
|
|
|
|
if (nonCodeEnd > nonCodeStart) {
|
|
|
|
replaceAndInsertMarkdown(nonCodeStart, nonCodeEnd, doc);
|
|
|
|
}
|
|
|
|
lastIndex = pos.first;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lastIndex > 0)
|
|
|
|
replaceAndInsertMarkdown(0, lastIndex, doc);
|
|
|
|
}
|
2023-06-10 10:15:38 -04:00
|
|
|
}
|