async parsing + tex caching

This commit is contained in:
2026-08-26 02:06:10 +02:00
parent e18875a752
commit 3946541f87
32 changed files with 2730 additions and 489 deletions
+36 -6
View File
@@ -2,7 +2,10 @@
#include "markdownparser.hpp"
#include <QCoreApplication>
#include <QDateTime>
#include <QPointer>
#include <QThreadPool>
namespace ZShell::llm {
@@ -19,8 +22,10 @@ LlmSegment::LlmSegment(Type type, qint64 timestamp, QObject* parent)
&m_markdownTimer, &QTimer::timeout, this, [this]() {
if (!m_markdownDirty)
return;
m_markdownDirty = false;
parseMarkdown();
// A parse may still be running; leave the dirty flag set so
// it re-parses the newest text when that one completes.
if (parseMarkdown())
m_markdownDirty = false;
});
}
@@ -53,7 +58,8 @@ void LlmSegment::close() {
// out the debounce.
if (m_type == Type::Content && m_markdownDirty) {
m_markdownTimer.stop();
parseMarkdown();
if (parseMarkdown())
m_markdownDirty = false;
}
}
@@ -130,9 +136,33 @@ void LlmSegment::scheduleMarkdown() {
m_markdownTimer.start();
}
void LlmSegment::parseMarkdown() {
m_markdown = MarkdownParser::parse(m_text);
Q_EMIT markdownChanged();
bool LlmSegment::parseMarkdown() {
if (m_parseInFlight)
return false;
m_parseInFlight = true;
const QString text = m_text;
QThreadPool::globalInstance()->start([this, text]() {
const QVariantList blocks = MarkdownParser::parse(text);
// Deliver through qApp (never destroyed) and re-check the
// pointer on the GUI thread: posting to `this` from the pool
// thread would race with its destruction.
QPointer<LlmSegment> guard(this);
QMetaObject::invokeMethod(
QCoreApplication::instance(),
[guard, blocks]() {
LlmSegment* seg = guard;
if (!seg)
return;
seg->m_parseInFlight = false;
seg->m_markdown = blocks;
Q_EMIT seg->markdownChanged();
// Text arrived while the worker was running; parse it.
if (seg->m_markdownDirty)
seg->parseMarkdown();
},
Qt::QueuedConnection);
});
return true;
}
} // namespace ZShell::llm