SyntaxHighlighter: Add signal that highlighting is finished

Added function syntaxInfoUpdated shows whether highlighting inProgress
or Done and signal highlightingFinished.

Change-Id: I4cf2b5cfa97d73c882e69ee1df81497ec50a81f7
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Artem Sokolovskii
2023-12-14 11:17:42 +01:00
parent 13b48144a6
commit 4a8e22e3f2
4 changed files with 50 additions and 7 deletions

View File

@@ -221,6 +221,9 @@ void SyntaxHighlighterPrivate::reformatBlocks(int from, int charsRemoved, int ch
formatChanges.clear();
foldValidator.finalize();
SyntaxHighlighter::Result res;
res.m_state = SyntaxHighlighter::State::Done;
vecRes << res;
emit q->resultsReady(vecRes);
}

View File

@@ -52,6 +52,11 @@ public:
void setNoAutomaticHighlighting(bool noAutomatic);
enum State {
InProgress,
Done
};
struct Result
{
void fillByBlock(const QTextBlock &block)
@@ -107,6 +112,8 @@ public:
QByteArray m_expectedRawStringSuffix;
int m_userState = -1;
QList<QTextLayout::FormatRange> m_formatRanges;
State m_state = InProgress;
};
void setExtraFormats(const QTextBlock &block, const QList<QTextLayout::FormatRange> &formats);

View File

@@ -25,6 +25,11 @@ public:
m_highlighter.reset(m_creator());
m_highlighter->setFontSettings(m_fontSettings);
m_highlighter->setDocument(m_document);
connect(m_highlighter.get(),
&SyntaxHighlighter::resultsReady,
this,
&SyntaxHighlighterRunnerPrivate::resultsReady);
}
SyntaxHighlighterRunnerPrivate(BaseSyntaxHighlighterRunner::SyntaxHighLighterCreator creator,
@@ -46,11 +51,6 @@ public:
m_document->setDocumentLayout(new TextDocumentLayout(m_document));
createHighlighter();
connect(m_highlighter.get(),
&SyntaxHighlighter::resultsReady,
this,
&SyntaxHighlighterRunnerPrivate::resultsReady);
}
void cloneDocument(int from,
@@ -86,7 +86,7 @@ public:
void setFontSettings(const TextEditor::FontSettings &fontSettings)
{
m_highlighter->setFontSettings(fontSettings);
m_highlighter->rehighlight();
rehighlight();
}
void setDefinitionName(const QString &name)
@@ -120,14 +120,39 @@ BaseSyntaxHighlighterRunner::BaseSyntaxHighlighterRunner(
QTextDocument *document,
const TextEditor::FontSettings &fontSettings)
: d(new SyntaxHighlighterRunnerPrivate(creator, document, fontSettings))
, m_document(document)
{
m_document = document;
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;
});
}
BaseSyntaxHighlighterRunner::~BaseSyntaxHighlighterRunner() = default;
void BaseSyntaxHighlighterRunner::applyFormatRanges(const SyntaxHighlighter::Result &result)
{
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;
@@ -145,6 +170,7 @@ void BaseSyntaxHighlighterRunner::applyFormatRanges(const SyntaxHighlighter::Res
void BaseSyntaxHighlighterRunner::cloneDocumentData(int from, int charsRemoved, int charsAdded)
{
m_syntaxInfoUpdated = SyntaxHighlighter::State::InProgress;
QMap<int, BaseSyntaxHighlighterRunner::BlockPreeditData> blocksPreedit;
QTextBlock firstBlock = m_document->findBlock(from);
QTextBlock endBlock = m_document->findBlock(from + charsAdded);
@@ -245,6 +271,7 @@ ThreadedSyntaxHighlighterRunner::ThreadedSyntaxHighlighterRunner(SyntaxHighLight
for (const SyntaxHighlighter::Result &res : result)
applyFormatRanges(res);
});
cloneDocumentData(0, 0, document->characterCount());
connect(document,
&QTextDocument::contentsChange,

View File

@@ -50,6 +50,10 @@ public:
QTextDocument *document() const { return m_document; }
SyntaxHighLighterCreator creator() const { return m_creator; }
bool syntaxInfoUpdated() const { return m_syntaxInfoUpdated == SyntaxHighlighter::State::Done; }
signals:
void highlightingFinished();
protected:
std::unique_ptr<SyntaxHighlighterRunnerPrivate> d;
QPointer<QTextDocument> m_document = nullptr;
@@ -60,6 +64,8 @@ protected:
const QString textAdded,
const QMap<int, BlockPreeditData> &blocksPreedit);
SyntaxHighlighter::State m_syntaxInfoUpdated = SyntaxHighlighter::State::Done;
private:
SyntaxHighLighterCreator m_creator;
QString m_definitionName;