mirror of
https://github.com/nomic-ai/gpt4all.git
synced 2024-10-01 01:06:10 -04:00
responsetext : fix markdown code block trimming (#2232)
Signed-off-by: Jared Van Bortel <jared@nomic.ai>
This commit is contained in:
parent
4ebb0c6ac0
commit
be93ee75de
@ -1045,8 +1045,6 @@ void ResponseText::handleCodeBlocks()
|
||||
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());
|
||||
@ -1062,9 +1060,13 @@ void ResponseText::handleCodeBlocks()
|
||||
QString capturedText = matchesCode[index].captured(1);
|
||||
QString codeLanguage;
|
||||
|
||||
QRegularExpressionMatch match = reWhitespace.match(capturedText);
|
||||
if (match.hasMatch()) {
|
||||
const QString firstWord = match.captured(1).trimmed();
|
||||
QStringList lines = capturedText.split('\n');
|
||||
if (lines.last().isEmpty()) {
|
||||
lines.removeLast();
|
||||
}
|
||||
|
||||
if (lines.count() >= 2) {
|
||||
const auto &firstWord = lines.first();
|
||||
if (firstWord == "python"
|
||||
|| firstWord == "cpp"
|
||||
|| firstWord == "c++"
|
||||
@ -1082,12 +1084,10 @@ void ResponseText::handleCodeBlocks()
|
||||
|| firstWord == "html"
|
||||
|| firstWord == "php") {
|
||||
codeLanguage = firstWord;
|
||||
capturedText.remove(0, match.captured(0).length());
|
||||
}
|
||||
lines.removeFirst();
|
||||
}
|
||||
|
||||
const QStringList lines = capturedText.split('\n');
|
||||
|
||||
QTextFrame *mainFrame = cursor.currentFrame();
|
||||
cursor.setCharFormat(textFormat);
|
||||
|
||||
@ -1141,10 +1141,6 @@ void ResponseText::handleCodeBlocks()
|
||||
} else {
|
||||
codeCursor.insertText(lines.join("\n"));
|
||||
}
|
||||
if (codeCursor.position() > 0) {
|
||||
codeCursor.setPosition(codeCursor.position() - 1);
|
||||
codeCursor.deleteChar();
|
||||
}
|
||||
|
||||
cursor = mainFrame->lastCursorPosition();
|
||||
cursor.setCharFormat(QTextCharFormat());
|
||||
|
Loading…
Reference in New Issue
Block a user