From 4a8e22e3f227eb0c9e4f62f3efa613b7bfec23ef Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Thu, 14 Dec 2023 11:17:42 +0100 Subject: [PATCH] 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 --- src/plugins/texteditor/syntaxhighlighter.cpp | 3 ++ src/plugins/texteditor/syntaxhighlighter.h | 7 ++++ .../texteditor/syntaxhighlighterrunner.cpp | 41 +++++++++++++++---- .../texteditor/syntaxhighlighterrunner.h | 6 +++ 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/src/plugins/texteditor/syntaxhighlighter.cpp b/src/plugins/texteditor/syntaxhighlighter.cpp index 36c3977c5ea..e37b0b104b7 100644 --- a/src/plugins/texteditor/syntaxhighlighter.cpp +++ b/src/plugins/texteditor/syntaxhighlighter.cpp @@ -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); } diff --git a/src/plugins/texteditor/syntaxhighlighter.h b/src/plugins/texteditor/syntaxhighlighter.h index e68ca74ef1b..bbbade3da24 100644 --- a/src/plugins/texteditor/syntaxhighlighter.h +++ b/src/plugins/texteditor/syntaxhighlighter.h @@ -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 m_formatRanges; + + State m_state = InProgress; }; void setExtraFormats(const QTextBlock &block, const QList &formats); diff --git a/src/plugins/texteditor/syntaxhighlighterrunner.cpp b/src/plugins/texteditor/syntaxhighlighterrunner.cpp index 3367cbdbf1a..626721efbfb 100644 --- a/src/plugins/texteditor/syntaxhighlighterrunner.cpp +++ b/src/plugins/texteditor/syntaxhighlighterrunner.cpp @@ -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 &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 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, diff --git a/src/plugins/texteditor/syntaxhighlighterrunner.h b/src/plugins/texteditor/syntaxhighlighterrunner.h index d4fead53e6a..6336c27a823 100644 --- a/src/plugins/texteditor/syntaxhighlighterrunner.h +++ b/src/plugins/texteditor/syntaxhighlighterrunner.h @@ -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 d; QPointer m_document = nullptr; @@ -60,6 +64,8 @@ protected: const QString textAdded, const QMap &blocksPreedit); + SyntaxHighlighter::State m_syntaxInfoUpdated = SyntaxHighlighter::State::Done; + private: SyntaxHighLighterCreator m_creator; QString m_definitionName;