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:
Artem Sokolovskii
2023-05-08 17:11:54 +02:00
parent d5ddf391c1
commit 62ea85ee6a
33 changed files with 603 additions and 142 deletions

View File

@@ -743,7 +743,7 @@ void QmlJSEditorWidget::inspectElementUnderCursor() const
widget->setReadOnly(true);
widget->textDocument()->setTemporary(true);
widget->textDocument()->setSyntaxHighlighterCreator([] {return new QmlJSHighlighter();});
widget->textDocument()->resetSyntaxHighlighter([] { return new QmlJSHighlighter(); });
const QString buf = inspectCppComponent(cppValue);
widget->textDocument()->setPlainText(buf);
@@ -1159,7 +1159,7 @@ QmlJSEditorFactory::QmlJSEditorFactory(Utils::Id _id)
void QmlJSEditorFactory::decorateEditor(TextEditorWidget *editor)
{
editor->textDocument()->setSyntaxHighlighterCreator([] { return new QmlJSHighlighter(); });
editor->textDocument()->resetSyntaxHighlighter([] { return new QmlJSHighlighter(); });
editor->textDocument()->setIndenter(new Internal::Indenter(editor->textDocument()->document()));
editor->setAutoCompleter(new AutoCompleter);
}

View File

@@ -820,7 +820,7 @@ QmlJSEditorDocument::QmlJSEditorDocument(Utils::Id id)
d, &Internal::QmlJSEditorDocumentPrivate::invalidateFormatterCache);
connect(this, &TextEditor::TextDocument::openFinishedSuccessfully,
d, &Internal::QmlJSEditorDocumentPrivate::settingsChanged);
setSyntaxHighlighterCreator([] { return new QmlJSHighlighter(); });
resetSyntaxHighlighter([] { return new QmlJSHighlighter(); });
setCodec(QTextCodec::codecForName("UTF-8")); // qml files are defined to be utf-8
setIndenter(new Internal::Indenter(document()));
}

View File

@@ -569,7 +569,7 @@ void SemanticHighlighter::applyResults(int from, int to)
if (m_enableHighlighting)
TextEditor::SemanticHighlighter::incrementalApplyExtraAdditionalFormats(
m_document->syntaxHighlighter(), m_watcher.future(), from, to, m_extraFormats);
m_document->syntaxHighlighterRunner(), m_watcher.future(), from, to, m_extraFormats);
}
void SemanticHighlighter::finished()
@@ -584,7 +584,7 @@ void SemanticHighlighter::finished()
if (m_enableHighlighting)
TextEditor::SemanticHighlighter::clearExtraAdditionalFormatsUntilEnd(
m_document->syntaxHighlighter(), m_watcher.future());
m_document->syntaxHighlighterRunner(), m_watcher.future());
}
void SemanticHighlighter::run(QPromise<Use> &promise,