forked from qt-creator/qt-creator
SyntaxHighlighter: Move SyntaxHighlighter to separate thread
This change involves the relocation of SyntaxHighlighter processing to another thread. The core idea is to create a duplicate of the original TextDocument using SyntaxHighlighterRunnerPrivate::cloneDocument. A new SyntaxHighlighter is then instantiated by SyntaxHighLighterCreator for the cloned document. The entire SyntaxHighLighterCreator class is moved to a new thread, where it performs highlighting on the cloned document. Upon completion of the highlighting process, the resultsReady signal is emitted, and the updated highlighting data is applied to the original document. This shift of SyntaxHighlighter to another thread enhances the user experience by preventing UI slowdowns during the highlighting process. - Introduction of BaseSyntaxHighlighterRunner as an interface class for future *SyntaxHighlighterRunner. - Inclusion of DirectSyntaxHighlighterRunner class for performing highlighting in the main thread, suitable for syntax highlighters that cannot be moved to another thread. - Introduction of ThreadedSyntaxHighlighterRunner class for highlighting in a separate thread, preventing UI blocking during the process. - Addition of Result data to the SyntaxHighlighter class to facilitate data exchange between threads. Task-number: QTCREATORBUG-28727 Change-Id: I4b6a38d15f5ec9b8828055d38d2a0c6f21a657b4 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
75
src/plugins/texteditor/syntaxhighlighterrunner.h
Normal file
75
src/plugins/texteditor/syntaxhighlighterrunner.h
Normal file
@@ -0,0 +1,75 @@
|
||||
// Copyright (C) 2023 The Qt Company Ltd.
|
||||
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <texteditor/syntaxhighlighter.h>
|
||||
|
||||
#include <KSyntaxHighlighting/Definition>
|
||||
|
||||
#include <QObject>
|
||||
#include <QPointer>
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
class QTextDocument;
|
||||
QT_END_NAMESPACE
|
||||
|
||||
namespace TextEditor {
|
||||
|
||||
class SyntaxHighlighterRunnerPrivate;
|
||||
|
||||
class TEXTEDITOR_EXPORT BaseSyntaxHighlighterRunner : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
using SyntaxHighLighterCreator = std::function<SyntaxHighlighter *()>;
|
||||
struct BlockPreeditData {
|
||||
int position;
|
||||
QString text;
|
||||
};
|
||||
|
||||
BaseSyntaxHighlighterRunner(SyntaxHighLighterCreator creator, QTextDocument *document);
|
||||
virtual ~BaseSyntaxHighlighterRunner();
|
||||
|
||||
void setExtraFormats(const QMap<int, QList<QTextLayout::FormatRange>> &formats);
|
||||
void clearExtraFormats(const QList<int> &blockNumbers);
|
||||
void clearAllExtraFormats();
|
||||
void setFontSettings(const TextEditor::FontSettings &fontSettings);
|
||||
void setLanguageFeaturesFlags(unsigned int flags);
|
||||
void setEnabled(bool enabled);
|
||||
void rehighlight();
|
||||
|
||||
virtual KSyntaxHighlighting::Definition getDefinition();
|
||||
|
||||
QTextDocument *document() const { return m_document; }
|
||||
SyntaxHighLighterCreator creator() const { return m_creator; }
|
||||
|
||||
protected:
|
||||
std::unique_ptr<SyntaxHighlighterRunnerPrivate> d;
|
||||
QPointer<QTextDocument> m_document = nullptr;
|
||||
void applyFormatRanges(const SyntaxHighlighter::Result &result);
|
||||
void cloneDocumentData(int from, int charsRemoved, int charsAdded);
|
||||
void cloneDocument(int from,
|
||||
int charsRemoved,
|
||||
const QString textAdded,
|
||||
const QMap<int, BlockPreeditData> &blocksPreedit);
|
||||
|
||||
private:
|
||||
SyntaxHighLighterCreator m_creator;
|
||||
};
|
||||
|
||||
class TEXTEDITOR_EXPORT ThreadedSyntaxHighlighterRunner : public BaseSyntaxHighlighterRunner
|
||||
{
|
||||
public:
|
||||
ThreadedSyntaxHighlighterRunner(SyntaxHighLighterCreator SyntaxHighLighterCreator,
|
||||
QTextDocument *document);
|
||||
~ThreadedSyntaxHighlighterRunner();
|
||||
|
||||
KSyntaxHighlighting::Definition getDefinition() override;
|
||||
|
||||
private:
|
||||
QThread m_thread;
|
||||
};
|
||||
|
||||
} // namespace TextEditor
|
||||
|
||||
Reference in New Issue
Block a user