better scrollbar in codeblocks + enter anim for delegates
This commit is contained in:
@@ -43,6 +43,10 @@ namespace ZShell::llm {
|
||||
// means "no highlighting" (unknown language or grammar not installed).
|
||||
// token is passed back unchanged so the caller can drop results for
|
||||
// superseded code; a destroyed target is simply skipped.
|
||||
//
|
||||
// Successful results are cached by (language, code). A request for
|
||||
// unchanged code delivers the cached spans directly, without re-parsing
|
||||
// — while a segment streams, only the grown tail is ever re-parsed.
|
||||
class CodeHighlighter : public QObject {
|
||||
Q_OBJECT
|
||||
QML_ELEMENT
|
||||
@@ -76,13 +80,31 @@ class CodeHighlighter : public QObject {
|
||||
// Maps a tree-sitter capture name to a role index (0 = unstyled).
|
||||
[[nodiscard]] static uint8_t roleFor(const char* name, uint32_t length);
|
||||
[[nodiscard]] static const char* roleName(uint8_t role);
|
||||
// Maps a fence language tag to the grammar id (see aliases()).
|
||||
[[nodiscard]] static QString resolveId(const QString& language);
|
||||
[[nodiscard]] static QString cacheKey(const QString& id, const QString& code);
|
||||
// The parsing work; runs on worker threads, so the per-language
|
||||
// state must be initialized under m_stateMutex and is shared as an
|
||||
// immutable object afterwards.
|
||||
[[nodiscard]] QVariantList doHighlight(const QString& code, const QString& language) const;
|
||||
// Exact-match span cache. lookupSpans() runs on the GUI thread,
|
||||
// storeSpans() on worker threads; both take m_cacheMutex.
|
||||
[[nodiscard]] QVariantList lookupSpans(const QString& code, const QString& language) const;
|
||||
void storeSpans(const QString& code, const QString& language,
|
||||
const QVariantList& spans) const;
|
||||
|
||||
struct SpanCacheEntry {
|
||||
QString code; // re-compared on lookup; a hash collision can
|
||||
// never deliver the wrong spans
|
||||
QVariantList spans;
|
||||
};
|
||||
|
||||
mutable QHash<QString, std::shared_ptr<const State>> m_states;
|
||||
mutable QMutex m_stateMutex;
|
||||
mutable QHash<QString, SpanCacheEntry> m_spanCache;
|
||||
mutable QStringList m_spanCacheOrder; // LRU order, oldest first
|
||||
mutable int m_spanCacheBytes = 0;
|
||||
mutable QMutex m_cacheMutex;
|
||||
static CodeHighlighter* s_instance;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user