2023-05-08 17:11:54 +02:00
|
|
|
// Copyright (C) 2023 The Qt Company Ltd.
|
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
|
|
|
|
|
|
#include "syntaxhighlighterrunner.h"
|
|
|
|
|
|
2023-11-28 14:58:58 +01:00
|
|
|
#include "fontsettings.h"
|
|
|
|
|
#include "textdocumentlayout.h"
|
2023-12-13 14:52:44 +01:00
|
|
|
#include "texteditorsettings.h"
|
2023-12-21 15:01:12 +01:00
|
|
|
#include "highlighter.h"
|
2023-05-08 17:11:54 +02:00
|
|
|
|
|
|
|
|
#include <utils/textutils.h>
|
|
|
|
|
|
|
|
|
|
#include <QMetaObject>
|
|
|
|
|
#include <QTextCursor>
|
|
|
|
|
#include <QTextDocument>
|
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
|
|
namespace TextEditor {
|
|
|
|
|
|
|
|
|
|
class SyntaxHighlighterRunnerPrivate : public QObject
|
|
|
|
|
{
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
public:
|
2023-12-21 14:14:31 +01:00
|
|
|
SyntaxHighlighterRunnerPrivate(BaseSyntaxHighlighterRunner::SyntaxHighLighterCreator creator,
|
|
|
|
|
QTextDocument *document,
|
|
|
|
|
FontSettings fontSettings)
|
|
|
|
|
: m_document(document)
|
|
|
|
|
, m_fontSettings(fontSettings)
|
2023-12-13 14:52:44 +01:00
|
|
|
{
|
2023-12-21 14:14:31 +01:00
|
|
|
if (!m_document) {
|
|
|
|
|
m_document = new QTextDocument(this);
|
|
|
|
|
m_document->setDocumentLayout(new TextDocumentLayout(m_document));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
m_highlighter.reset(creator());
|
2023-12-13 14:52:44 +01:00
|
|
|
m_highlighter->setFontSettings(m_fontSettings);
|
|
|
|
|
m_highlighter->setDocument(m_document);
|
2023-12-21 14:14:31 +01:00
|
|
|
m_highlighter->setParent(m_document);
|
2023-12-14 11:17:42 +01:00
|
|
|
|
|
|
|
|
connect(m_highlighter.get(),
|
|
|
|
|
&SyntaxHighlighter::resultsReady,
|
|
|
|
|
this,
|
|
|
|
|
&SyntaxHighlighterRunnerPrivate::resultsReady);
|
2023-12-13 14:52:44 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-21 14:53:36 +01:00
|
|
|
void changeDocument(int from,
|
|
|
|
|
int charsRemoved,
|
|
|
|
|
const QString textAdded,
|
|
|
|
|
const QMap<int, BaseSyntaxHighlighterRunner::BlockPreeditData> &blocksPreedit)
|
2023-05-08 17:11:54 +02:00
|
|
|
{
|
|
|
|
|
QTextCursor cursor(m_document);
|
|
|
|
|
cursor.setPosition(qMin(m_document->characterCount() - 1, from + charsRemoved));
|
|
|
|
|
cursor.setPosition(from, QTextCursor::KeepAnchor);
|
|
|
|
|
cursor.insertText(textAdded);
|
|
|
|
|
|
|
|
|
|
for (auto it = blocksPreedit.cbegin(); it != blocksPreedit.cend(); ++it) {
|
2023-12-21 14:38:29 +01:00
|
|
|
const QTextBlock block = m_document->findBlockByNumber(it.key());
|
2023-05-08 17:11:54 +02:00
|
|
|
block.layout()->setPreeditArea(it.value().position, it.value().text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setExtraFormats(const QMap<int, QList<QTextLayout::FormatRange>> &formatMap)
|
|
|
|
|
{
|
|
|
|
|
for (auto it = formatMap.cbegin(); it != formatMap.cend(); ++it)
|
|
|
|
|
m_highlighter->setExtraFormats(m_document->findBlockByNumber(it.key()), it.value());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clearExtraFormats(const QList<int> &blockNumbers)
|
|
|
|
|
{
|
|
|
|
|
for (auto it = blockNumbers.cbegin(); it != blockNumbers.cend(); ++it)
|
|
|
|
|
m_highlighter->clearExtraFormats(m_document->findBlockByNumber(*it));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void clearAllExtraFormats() { m_highlighter->clearAllExtraFormats(); }
|
|
|
|
|
|
|
|
|
|
void setFontSettings(const TextEditor::FontSettings &fontSettings)
|
|
|
|
|
{
|
|
|
|
|
m_highlighter->setFontSettings(fontSettings);
|
2023-12-14 11:17:42 +01:00
|
|
|
rehighlight();
|
2023-05-08 17:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
2023-11-28 14:58:58 +01:00
|
|
|
void setDefinitionName(const QString &name)
|
|
|
|
|
{
|
|
|
|
|
return m_highlighter->setDefinitionName(name);
|
|
|
|
|
}
|
2023-05-08 17:11:54 +02:00
|
|
|
|
|
|
|
|
void setLanguageFeaturesFlags(unsigned int flags)
|
|
|
|
|
{
|
|
|
|
|
m_highlighter->setLanguageFeaturesFlags(flags);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setEnabled(bool enabled) { m_highlighter->setEnabled(enabled); }
|
|
|
|
|
|
|
|
|
|
void rehighlight() { m_highlighter->rehighlight(); }
|
|
|
|
|
|
|
|
|
|
std::unique_ptr<SyntaxHighlighter> m_highlighter;
|
|
|
|
|
QTextDocument *m_document = nullptr;
|
2023-12-14 16:04:07 +01:00
|
|
|
FontSettings m_fontSettings;
|
2023-12-21 15:01:12 +01:00
|
|
|
|
|
|
|
|
signals:
|
|
|
|
|
void resultsReady(const QList<SyntaxHighlighter::Result> &result);
|
|
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// ----------------------------- BaseSyntaxHighlighterRunner --------------------------------------
|
|
|
|
|
|
|
|
|
|
BaseSyntaxHighlighterRunner::BaseSyntaxHighlighterRunner(
|
2023-12-14 16:04:07 +01:00
|
|
|
BaseSyntaxHighlighterRunner::SyntaxHighLighterCreator creator,
|
|
|
|
|
QTextDocument *document,
|
|
|
|
|
const TextEditor::FontSettings &fontSettings)
|
|
|
|
|
: d(new SyntaxHighlighterRunnerPrivate(creator, document, fontSettings))
|
2023-12-14 11:17:42 +01:00
|
|
|
, m_document(document)
|
2023-05-08 17:11:54 +02:00
|
|
|
{
|
2023-12-21 15:01:12 +01:00
|
|
|
m_useGenericHighlighter = qobject_cast<Highlighter *>(d->m_highlighter.get());
|
|
|
|
|
|
2023-12-14 11:17:42 +01:00
|
|
|
if (document == nullptr)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
connect(d.get(),
|
|
|
|
|
&SyntaxHighlighterRunnerPrivate::resultsReady,
|
|
|
|
|
this,
|
|
|
|
|
[this](const QList<SyntaxHighlighter::Result> &result) {
|
|
|
|
|
auto done = std::find_if(result.cbegin(),
|
|
|
|
|
result.cend(),
|
|
|
|
|
[](const SyntaxHighlighter::Result &res) {
|
|
|
|
|
return res.m_state == SyntaxHighlighter::State::Done;
|
|
|
|
|
});
|
|
|
|
|
if (done != result.cend()) {
|
|
|
|
|
m_syntaxInfoUpdated = SyntaxHighlighter::State::Done;
|
|
|
|
|
emit highlightingFinished();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_syntaxInfoUpdated = SyntaxHighlighter::State::InProgress;
|
|
|
|
|
});
|
2023-05-08 17:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
BaseSyntaxHighlighterRunner::~BaseSyntaxHighlighterRunner() = default;
|
|
|
|
|
|
2024-01-08 11:26:44 +01:00
|
|
|
void BaseSyntaxHighlighterRunner::applyFormatRanges(const QList<SyntaxHighlighter::Result> &results)
|
2023-05-08 17:11:54 +02:00
|
|
|
{
|
2024-01-08 11:26:44 +01:00
|
|
|
if (m_document == nullptr)
|
2023-12-14 11:17:42 +01:00
|
|
|
return;
|
|
|
|
|
|
2024-01-08 11:26:44 +01:00
|
|
|
for (const SyntaxHighlighter::Result &result : results) {
|
|
|
|
|
m_syntaxInfoUpdated = result.m_state;
|
|
|
|
|
if (m_syntaxInfoUpdated == SyntaxHighlighter::State::Done) {
|
|
|
|
|
emit highlightingFinished();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
QTextBlock docBlock = m_document->findBlock(result.m_blockNumber);
|
|
|
|
|
if (!docBlock.isValid())
|
|
|
|
|
return;
|
2023-05-08 17:11:54 +02:00
|
|
|
|
2024-01-08 11:26:44 +01:00
|
|
|
result.copyToBlock(docBlock);
|
2023-05-08 17:11:54 +02:00
|
|
|
|
2024-01-08 11:26:44 +01:00
|
|
|
if (!result.m_formatRanges.empty()) {
|
|
|
|
|
TextDocumentLayout::FoldValidator foldValidator;
|
|
|
|
|
foldValidator.setup(qobject_cast<TextDocumentLayout *>(m_document->documentLayout()));
|
|
|
|
|
docBlock.layout()->setFormats(result.m_formatRanges);
|
|
|
|
|
m_document->markContentsDirty(docBlock.position(), docBlock.length());
|
|
|
|
|
foldValidator.process(docBlock);
|
|
|
|
|
}
|
2023-05-08 17:11:54 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-21 14:53:36 +01:00
|
|
|
void BaseSyntaxHighlighterRunner::changeDocument(int from, int charsRemoved, int charsAdded)
|
2023-05-08 17:11:54 +02:00
|
|
|
{
|
2023-12-21 14:53:36 +01:00
|
|
|
QTC_ASSERT(m_document, return);
|
2023-12-14 11:17:42 +01:00
|
|
|
m_syntaxInfoUpdated = SyntaxHighlighter::State::InProgress;
|
2023-05-08 17:11:54 +02:00
|
|
|
QMap<int, BaseSyntaxHighlighterRunner::BlockPreeditData> blocksPreedit;
|
2023-12-21 14:38:29 +01:00
|
|
|
QTextBlock block = m_document->findBlock(from);
|
|
|
|
|
const QTextBlock endBlock = m_document->findBlock(from + charsAdded);
|
|
|
|
|
while (block.isValid() && block != endBlock) {
|
|
|
|
|
if (QTextLayout *layout = block.layout()) {
|
|
|
|
|
if (const int pos = layout->preeditAreaPosition(); pos != -1)
|
|
|
|
|
blocksPreedit[block.blockNumber()] = {pos, layout->preeditAreaText()};
|
2023-05-08 17:11:54 +02:00
|
|
|
}
|
2023-12-21 14:38:29 +01:00
|
|
|
block = block.next();
|
2023-05-08 17:11:54 +02:00
|
|
|
}
|
|
|
|
|
const QString text = Utils::Text::textAt(QTextCursor(m_document), from, charsAdded);
|
2023-12-21 14:53:36 +01:00
|
|
|
QMetaObject::invokeMethod(d.get(), [this, from, charsRemoved, text, blocksPreedit] {
|
|
|
|
|
d->changeDocument(from, charsRemoved, text, blocksPreedit);
|
2023-05-08 17:11:54 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-21 15:01:12 +01:00
|
|
|
bool BaseSyntaxHighlighterRunner::useGenericHighlighter() const
|
|
|
|
|
{
|
|
|
|
|
return m_useGenericHighlighter;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-08 17:11:54 +02:00
|
|
|
void BaseSyntaxHighlighterRunner::setExtraFormats(
|
|
|
|
|
const QMap<int, QList<QTextLayout::FormatRange>> &formatMap)
|
|
|
|
|
{
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this, formatMap] { d->setExtraFormats(formatMap); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseSyntaxHighlighterRunner::clearExtraFormats(const QList<int> &blockNumbers)
|
|
|
|
|
{
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this, blockNumbers] { d->clearExtraFormats(blockNumbers); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseSyntaxHighlighterRunner::clearAllExtraFormats()
|
|
|
|
|
{
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this] { d->clearAllExtraFormats(); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseSyntaxHighlighterRunner::setFontSettings(const TextEditor::FontSettings &fontSettings)
|
|
|
|
|
{
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this, fontSettings] { d->setFontSettings(fontSettings); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseSyntaxHighlighterRunner::setLanguageFeaturesFlags(unsigned int flags)
|
|
|
|
|
{
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this, flags] { d->setLanguageFeaturesFlags(flags); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseSyntaxHighlighterRunner::setEnabled(bool enabled)
|
|
|
|
|
{
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this, enabled] { d->setEnabled(enabled); });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseSyntaxHighlighterRunner::rehighlight()
|
|
|
|
|
{
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this] { d->rehighlight(); });
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-28 14:58:58 +01:00
|
|
|
QString BaseSyntaxHighlighterRunner::definitionName()
|
|
|
|
|
{
|
|
|
|
|
return m_definitionName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void BaseSyntaxHighlighterRunner::setDefinitionName(const QString &name)
|
2023-05-08 17:11:54 +02:00
|
|
|
{
|
2023-11-28 14:58:58 +01:00
|
|
|
if (name.isEmpty())
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
m_definitionName = name;
|
|
|
|
|
QMetaObject::invokeMethod(d.get(), [this, name] { d->setDefinitionName(name); });
|
2023-05-08 17:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// --------------------------- ThreadedSyntaxHighlighterRunner ------------------------------------
|
|
|
|
|
|
|
|
|
|
ThreadedSyntaxHighlighterRunner::ThreadedSyntaxHighlighterRunner(SyntaxHighLighterCreator creator,
|
|
|
|
|
QTextDocument *document)
|
|
|
|
|
: BaseSyntaxHighlighterRunner(creator, nullptr)
|
|
|
|
|
{
|
|
|
|
|
QTC_ASSERT(document, return);
|
|
|
|
|
|
|
|
|
|
d->moveToThread(&m_thread);
|
|
|
|
|
connect(&m_thread, &QThread::finished, d.get(), [this] { d.release()->deleteLater(); });
|
|
|
|
|
m_thread.start();
|
|
|
|
|
|
|
|
|
|
m_document = document;
|
|
|
|
|
connect(d.get(),
|
|
|
|
|
&SyntaxHighlighterRunnerPrivate::resultsReady,
|
2024-01-08 11:26:44 +01:00
|
|
|
this,
|
|
|
|
|
&ThreadedSyntaxHighlighterRunner::applyFormatRanges);
|
2023-12-14 11:17:42 +01:00
|
|
|
|
2023-12-21 14:53:36 +01:00
|
|
|
changeDocument(0, 0, document->characterCount());
|
2023-05-08 17:11:54 +02:00
|
|
|
connect(document,
|
|
|
|
|
&QTextDocument::contentsChange,
|
|
|
|
|
this,
|
2023-12-21 14:53:36 +01:00
|
|
|
&ThreadedSyntaxHighlighterRunner::changeDocument);
|
2023-05-08 17:11:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ThreadedSyntaxHighlighterRunner::~ThreadedSyntaxHighlighterRunner()
|
|
|
|
|
{
|
|
|
|
|
m_thread.quit();
|
|
|
|
|
m_thread.wait();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace TextEditor
|
|
|
|
|
|
|
|
|
|
#include "syntaxhighlighterrunner.moc"
|