Bugfixes for prompt syntax highlighting.

pull/956/head
Adam Treat 1 year ago committed by AT
parent 60d95cdd9b
commit 68ff7001ad

@ -626,9 +626,9 @@ Window {
}
Component.onCompleted: {
responseText.textDocument = textDocument
responseText.setLinkColor(theme.linkColor);
responseText.setHeaderColor(theme.backgroundLight);
responseText.setHeaderColor(name === qsTr("Response: ") ? theme.backgroundLight : theme.backgroundLighter);
responseText.textDocument = textDocument
}
Accessible.role: Accessible.Paragraph

@ -243,6 +243,7 @@ void ResponseText::setTextDocument(QQuickTextDocument* textDocument)
m_textDocument = textDocument;
m_syntaxHighlighter->setDocument(m_textDocument->textDocument());
connect(m_textDocument->textDocument(), &QTextDocument::contentsChanged, this, &ResponseText::handleTextChanged);
handleTextChanged();
}
QString ResponseText::getLinkAtPosition(int position) const
@ -288,7 +289,7 @@ void ResponseText::handleContextLinks()
linkFormat.setFontUnderline(true);
// Regex for context links
QRegularExpression reLink("\\[Context\\]\\((context://\\d+)\\)");
static const QRegularExpression reLink("\\[Context\\]\\((context://\\d+)\\)");
QRegularExpressionMatchIterator iLink = reLink.globalMatch(doc->toPlainText());
QList<QRegularExpressionMatch> matchesLink;
@ -374,9 +375,11 @@ void ResponseText::handleCodeBlocks()
copyImageFormat.setName("qrc:/gpt4all/icons/copy.svg");
// Regex for code blocks
QRegularExpression reCode("```(.*?)(```|$)", QRegularExpression::DotMatchesEverythingOption);
static const QRegularExpression reCode("```(.*?)(```|$)", QRegularExpression::DotMatchesEverythingOption);
QRegularExpressionMatchIterator iCode = reCode.globalMatch(doc->toPlainText());
static const QRegularExpression reWhitespace("^\\s*(\\w+)");
QList<QRegularExpressionMatch> matchesCode;
while (iCode.hasNext())
matchesCode.append(iCode.next());
@ -389,21 +392,24 @@ void ResponseText::handleCodeBlocks()
cursor.removeSelectedText();
QTextFrameFormat frameFormat = frameFormatBase;
QStringList lines = matchesCode[index].captured(1).split('\n');
QString capturedText = matchesCode[index].captured(1);
QString codeLanguage;
if (!lines.empty()) {
if (lines[0].trimmed() == "python")
codeLanguage = lines.takeFirst().trimmed();
else if (lines[0].trimmed() == "cpp")
codeLanguage = lines.takeFirst().trimmed();
else if (lines[0].trimmed() == "c++")
codeLanguage = lines.takeFirst().trimmed();
else if (lines[0].trimmed() == "c")
codeLanguage = lines.takeFirst().trimmed();
else if (lines[0].trimmed() == "bash")
codeLanguage = lines.takeFirst().trimmed();
QRegularExpressionMatch match = reWhitespace.match(capturedText);
if (match.hasMatch()) {
const QString firstWord = match.captured(1).trimmed();
if (firstWord == "python"
|| firstWord == "cpp"
|| firstWord == "c++"
|| firstWord == "c"
|| firstWord == "bash") {
codeLanguage = firstWord;
capturedText.remove(0, match.captured(0).length());
}
}
\
const QStringList lines = capturedText.split('\n');
QTextFrame *mainFrame = cursor.currentFrame();
cursor.setCharFormat(textFormat);

Loading…
Cancel
Save