gui: preserve original indentation in fenced code/diff blocks

- C++: Use <pre style="white-space:pre-wrap;"> instead of <div> with <br>
  so raw newlines are preserved through to the HTML renderer
- QML: Use TextEdit (preserves whitespace) instead of Label (collapses it)
  for rendering blockRenderedHtml
This commit is contained in:
Levi Neely 2026-08-01 12:00:14 +02:00
parent 7f083c0d7a
commit 7d75ba8524
2 changed files with 12 additions and 11 deletions

View File

@ -151,7 +151,7 @@ QString ChatBlockModel::highlightCode(const QString &code, const QString &langua
if (!def.isValid()) {
delete repo;
return QStringLiteral("<div>") + escapeHtml(code).replace('\n', QStringLiteral("<br>")) + QStringLiteral("</div>");
return QStringLiteral("<pre style=\"white-space:pre-wrap;\">") + escapeHtml(code) + QStringLiteral("</pre>");
}
HtmlHighlighter builder;
@ -163,29 +163,29 @@ QString ChatBlockModel::highlightCode(const QString &code, const QString &langua
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';
}
delete repo;
return QStringLiteral("<div>") + builder.html + QStringLiteral("</div>");
return QStringLiteral("<pre style=\"white-space:pre-wrap;\">") + builder.html + QStringLiteral("</pre>");
}
QString ChatBlockModel::renderDiff(const QString &diff) const
{
QString html = QStringLiteral("<div>");
QString html = QStringLiteral("<pre style=\"white-space:pre-wrap;\">");
const QStringList lines = diff.split('\n');
for (const QString &line : lines) {
if (line.startsWith('+')) {
html += QStringLiteral("<span style=\"color:#4caf50;\">") + escapeHtml(line) + QStringLiteral("</span><br>");
html += QStringLiteral("<span style=\"color:#4caf50;\">") + escapeHtml(line) + QStringLiteral("</span>\n");
} else if (line.startsWith('-')) {
html += QStringLiteral("<span style=\"color:#f44336;\">") + escapeHtml(line) + QStringLiteral("</span><br>");
html += QStringLiteral("<span style=\"color:#f44336;\">") + escapeHtml(line) + QStringLiteral("</span>\n");
} else if (line.startsWith("@@")) {
html += QStringLiteral("<span style=\"color:#2196f3;\">") + escapeHtml(line) + QStringLiteral("</span><br>");
html += QStringLiteral("<span style=\"color:#2196f3;\">") + escapeHtml(line) + QStringLiteral("</span>\n");
} else {
html += escapeHtml(line) + QStringLiteral("<br>");
html += escapeHtml(line) + '\n';
}
}
html += QStringLiteral("</div>");
html += QStringLiteral("</pre>");
return html;
}

View File

@ -716,12 +716,13 @@ ApplicationWindow {
spacing: 0
// Rendered HTML (fenced code/diff) — shown when available
Label {
TextEdit {
Layout.fillWidth: true
visible: blockRenderedHtml && blockRenderedHtml.length > 0
text: blockRenderedHtml
textFormat: Text.RichText
wrapMode: Text.WordWrap
readOnly: true
wrapMode: TextEdit.Wrap
font.family: theme.fontFamily || "monospace"
font.pointSize: theme.fontSize > 0 ? theme.fontSize : 12
color: theme.textColor