fix indentation: wrap highlighted HTML in <pre> with white-space:pre-wrap

HTML RichText collapses multiple spaces, destroying JSON indentation.
Wrap all highlighted output in <pre> tags and use real newlines instead
of <br>. pre-wrap allows wrapping at container width.
This commit is contained in:
Levi Neely 2026-07-16 07:20:55 +02:00
parent 5c093a3bb2
commit cc24400ba7
1 changed files with 10 additions and 6 deletions

View File

@ -78,8 +78,9 @@ QVariant ChatBlockModel::data(const QModelIndex &index, int role) const
if (trimmed.startsWith(QLatin1String("---")) || trimmed.startsWith(QLatin1String("diff "))) {
return highlightedHtml(content, QStringLiteral("Diff"));
}
// Plain text: escape and add line breaks
return content.toHtmlEscaped().replace('\n', QLatin1String("<br>"));
// Plain text: preserve whitespace
return QStringLiteral("<pre style=\"margin:0;white-space:pre-wrap;\">")
+ content.toHtmlEscaped() + QStringLiteral("</pre>");
}
}
return {};
@ -374,7 +375,8 @@ QString ChatBlockModel::highlightedHtml(const QString &code, const QString &lang
def = repo().definitionForFileName("file." + language);
}
if (!def.isValid()) {
QString plain = code.toHtmlEscaped().replace('\n', QStringLiteral("<br>"));
QString plain = QStringLiteral("<pre style=\"margin:0;white-space:pre-wrap;\">")
+ code.toHtmlEscaped() + QStringLiteral("</pre>");
m_highlightCache.insert(cacheKey, plain);
return plain;
}
@ -390,14 +392,16 @@ QString ChatBlockModel::highlightedHtml(const QString &code, const QString &lang
KSyntaxHighlighting::State state;
const QStringList lines = code.split('\n');
QString result = QStringLiteral("<pre style=\"margin:0;white-space:pre-wrap;\">");
for (int i = 0; i < lines.size(); ++i) {
state = builder.processLine(lines[i], state);
if (i < lines.size() - 1)
builder.html += QStringLiteral("<br>");
builder.html += '\n';
}
result += builder.html + QStringLiteral("</pre>");
m_highlightCache.insert(cacheKey, builder.html);
return builder.html;
m_highlightCache.insert(cacheKey, result);
return result;
}
QString ChatBlockModel::prettyJson(const QString &text) const