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: { Component.onCompleted: {
responseText.textDocument = textDocument
responseText.setLinkColor(theme.linkColor); responseText.setLinkColor(theme.linkColor);
responseText.setHeaderColor(theme.backgroundLight); responseText.setHeaderColor(name === qsTr("Response: ") ? theme.backgroundLight : theme.backgroundLighter);
responseText.textDocument = textDocument
} }
Accessible.role: Accessible.Paragraph Accessible.role: Accessible.Paragraph

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

Loading…
Cancel
Save