From 4bfc3476f444ad614a6aeba100d85e869ec69b1f Mon Sep 17 00:00:00 2001 From: Artem Sokolovskii Date: Thu, 4 May 2023 14:43:37 +0200 Subject: [PATCH] SyntaxHighlighter: Move highlighter creating to TextDocument Added setSyntaxHighlighterCreator function to TextDocument which allows the creation of highlighters inside TextDocument. Change-Id: I454f800c878c48a154dad5abd68b7a4f9ceb378a Reviewed-by: Jarek Kobus Reviewed-by: --- .../clangformat/clangformatconfigwidget.cpp | 3 +- src/plugins/cppeditor/cppeditordocument.cpp | 2 +- src/plugins/cppeditor/cpptoolsreuse.cpp | 2 +- src/plugins/diffeditor/diffeditor.cpp | 2 +- src/plugins/git/giteditor.cpp | 6 ++-- src/plugins/nim/editor/nimeditorfactory.cpp | 2 +- .../bindingeditor/bindingeditorwidget.cpp | 3 +- src/plugins/qmljseditor/qmljseditor.cpp | 4 +-- .../qmljseditor/qmljseditordocument.cpp | 2 +- src/plugins/texteditor/textdocument.cpp | 5 ++-- src/plugins/texteditor/textdocument.h | 4 ++- src/plugins/texteditor/texteditor.cpp | 28 ++++++++++--------- src/plugins/vcsbase/vcsbaseeditor.cpp | 9 ++++-- 13 files changed, 42 insertions(+), 30 deletions(-) diff --git a/src/plugins/clangformat/clangformatconfigwidget.cpp b/src/plugins/clangformat/clangformatconfigwidget.cpp index e3328bb2695..23916bf00f3 100644 --- a/src/plugins/clangformat/clangformatconfigwidget.cpp +++ b/src/plugins/clangformat/clangformatconfigwidget.cpp @@ -131,7 +131,8 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc d->preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0])); d->preview->textDocument()->setIndenter(new ClangFormatIndenter(d->preview->document())); d->preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings()); - d->preview->textDocument()->setSyntaxHighlighter(new CppEditor::CppHighlighter); + d->preview->textDocument()->setSyntaxHighlighterCreator( + [] { return new CppEditor::CppHighlighter(); }); d->preview->textDocument()->indenter()->setFileName(fileName); using namespace Layouting; diff --git a/src/plugins/cppeditor/cppeditordocument.cpp b/src/plugins/cppeditor/cppeditordocument.cpp index 18c729f7778..dfc5af1c889 100644 --- a/src/plugins/cppeditor/cppeditordocument.cpp +++ b/src/plugins/cppeditor/cppeditordocument.cpp @@ -79,7 +79,7 @@ private: CppEditorDocument::CppEditorDocument() { setId(CppEditor::Constants::CPPEDITOR_ID); - setSyntaxHighlighter(new CppHighlighter); + setSyntaxHighlighterCreator([] { return new CppHighlighter(); }); ICodeStylePreferencesFactory *factory = TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID); diff --git a/src/plugins/cppeditor/cpptoolsreuse.cpp b/src/plugins/cppeditor/cpptoolsreuse.cpp index 9951eea9ff5..26627b6c66a 100644 --- a/src/plugins/cppeditor/cpptoolsreuse.cpp +++ b/src/plugins/cppeditor/cpptoolsreuse.cpp @@ -854,7 +854,7 @@ namespace Internal { void decorateCppEditor(TextEditor::TextEditorWidget *editor) { - editor->textDocument()->setSyntaxHighlighter(new CppHighlighter); + editor->textDocument()->setSyntaxHighlighterCreator([] { return new CppHighlighter(); }); editor->textDocument()->setIndenter( new CppQtStyleIndenter(editor->textDocument()->document())); editor->setAutoCompleter(new CppAutoCompleter); diff --git a/src/plugins/diffeditor/diffeditor.cpp b/src/plugins/diffeditor/diffeditor.cpp index d202fb4338a..4998dfbd9a0 100644 --- a/src/plugins/diffeditor/diffeditor.cpp +++ b/src/plugins/diffeditor/diffeditor.cpp @@ -93,7 +93,7 @@ DescriptionEditorWidget::DescriptionEditorWidget(QWidget *parent) context->setContext(Context(Constants::C_DIFF_EDITOR_DESCRIPTION)); ICore::addContextObject(context); - textDocument()->setSyntaxHighlighter(new SyntaxHighlighter); + textDocument()->setSyntaxHighlighterCreator([] { return new SyntaxHighlighter(); }); } QSize DescriptionEditorWidget::sizeHint() const diff --git a/src/plugins/git/giteditor.cpp b/src/plugins/git/giteditor.cpp index a8613688ea8..05be7e7a4a4 100644 --- a/src/plugins/git/giteditor.cpp +++ b/src/plugins/git/giteditor.cpp @@ -246,9 +246,11 @@ void GitEditorWidget::init() return; const QChar commentChar = gitClient().commentChar(source()); if (isCommitEditor) - textDocument()->setSyntaxHighlighter(new GitSubmitHighlighter(commentChar)); + textDocument()->setSyntaxHighlighterCreator( + [commentChar] { return new GitSubmitHighlighter(commentChar); }); else if (isRebaseEditor) - textDocument()->setSyntaxHighlighter(new GitRebaseHighlighter(commentChar)); + textDocument()->setSyntaxHighlighterCreator( + [commentChar] { return new GitRebaseHighlighter(commentChar); }); } void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk) diff --git a/src/plugins/nim/editor/nimeditorfactory.cpp b/src/plugins/nim/editor/nimeditorfactory.cpp index 08e0ea2c327..62e49595385 100644 --- a/src/plugins/nim/editor/nimeditorfactory.cpp +++ b/src/plugins/nim/editor/nimeditorfactory.cpp @@ -51,7 +51,7 @@ NimEditorFactory::NimEditorFactory() void NimEditorFactory::decorateEditor(TextEditorWidget *editor) { - editor->textDocument()->setSyntaxHighlighter(new NimHighlighter()); + editor->textDocument()->setSyntaxHighlighterCreator([] { return new NimHighlighter();}); editor->textDocument()->setIndenter(new NimIndenter(editor->textDocument()->document())); } diff --git a/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp b/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp index f0c10d7420a..6f6e3ffaa84 100644 --- a/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp +++ b/src/plugins/qmldesigner/components/bindingeditor/bindingeditorwidget.cpp @@ -167,7 +167,8 @@ BindingEditorFactory::BindingEditorFactory() void BindingEditorFactory::decorateEditor(TextEditor::TextEditorWidget *editor) { - editor->textDocument()->setSyntaxHighlighter(new QmlJSEditor::QmlJSHighlighter); + editor->textDocument()->setSyntaxHighlighterCreator( + [] { return new QmlJSEditor::QmlJSHighlighter(); }); editor->textDocument()->setIndenter(new QmlJSEditor::Internal::Indenter( editor->textDocument()->document())); editor->setAutoCompleter(new QmlJSEditor::AutoCompleter); diff --git a/src/plugins/qmljseditor/qmljseditor.cpp b/src/plugins/qmljseditor/qmljseditor.cpp index d6b132b9700..a590c0fd834 100644 --- a/src/plugins/qmljseditor/qmljseditor.cpp +++ b/src/plugins/qmljseditor/qmljseditor.cpp @@ -743,7 +743,7 @@ void QmlJSEditorWidget::inspectElementUnderCursor() const widget->setReadOnly(true); widget->textDocument()->setTemporary(true); - widget->textDocument()->setSyntaxHighlighter(new QmlJSHighlighter(widget->document())); + widget->textDocument()->setSyntaxHighlighterCreator([] {return new QmlJSHighlighter();}); const QString buf = inspectCppComponent(cppValue); widget->textDocument()->setPlainText(buf); @@ -1160,7 +1160,7 @@ QmlJSEditorFactory::QmlJSEditorFactory(Utils::Id _id) void QmlJSEditorFactory::decorateEditor(TextEditorWidget *editor) { - editor->textDocument()->setSyntaxHighlighter(new QmlJSHighlighter); + editor->textDocument()->setSyntaxHighlighterCreator([] { return new QmlJSHighlighter(); }); editor->textDocument()->setIndenter(new Internal::Indenter(editor->textDocument()->document())); editor->setAutoCompleter(new AutoCompleter); } diff --git a/src/plugins/qmljseditor/qmljseditordocument.cpp b/src/plugins/qmljseditor/qmljseditordocument.cpp index 37f49d08712..5923521ab06 100644 --- a/src/plugins/qmljseditor/qmljseditordocument.cpp +++ b/src/plugins/qmljseditor/qmljseditordocument.cpp @@ -820,7 +820,7 @@ QmlJSEditorDocument::QmlJSEditorDocument(Utils::Id id) d, &Internal::QmlJSEditorDocumentPrivate::invalidateFormatterCache); connect(this, &TextEditor::TextDocument::openFinishedSuccessfully, d, &Internal::QmlJSEditorDocumentPrivate::settingsChanged); - setSyntaxHighlighter(new QmlJSHighlighter(document())); + setSyntaxHighlighterCreator([] { return new QmlJSHighlighter(); }); setCodec(QTextCodec::codecForName("UTF-8")); // qml files are defined to be utf-8 setIndenter(new Internal::Indenter(document())); } diff --git a/src/plugins/texteditor/textdocument.cpp b/src/plugins/texteditor/textdocument.cpp index be0d1a95774..2a6f4da0a7c 100644 --- a/src/plugins/texteditor/textdocument.cpp +++ b/src/plugins/texteditor/textdocument.cpp @@ -901,11 +901,12 @@ bool TextDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type return reload(errorString); } -void TextDocument::setSyntaxHighlighter(SyntaxHighlighter *highlighter) +void TextDocument::setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator) { if (d->m_highlighter) delete d->m_highlighter; - d->m_highlighter = highlighter; + + d->m_highlighter = creator(); d->m_highlighter->setParent(this); d->m_highlighter->setDocument(&d->m_document); } diff --git a/src/plugins/texteditor/textdocument.h b/src/plugins/texteditor/textdocument.h index a4dd3dd45f0..e05abbdba39 100644 --- a/src/plugins/texteditor/textdocument.h +++ b/src/plugins/texteditor/textdocument.h @@ -124,7 +124,9 @@ public: bool setPlainText(const QString &text); QTextDocument *document() const; - void setSyntaxHighlighter(SyntaxHighlighter *highlighter); + + using SyntaxHighLighterCreator = std::function; + void setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator); SyntaxHighlighter *syntaxHighlighter() const; bool reload(QString *errorString, QTextCodec *codec); diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 653bd310ebb..0d433f35c96 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -3690,16 +3690,19 @@ void TextEditorWidgetPrivate::removeSyntaxInfoBar() void TextEditorWidgetPrivate::configureGenericHighlighter( const KSyntaxHighlighting::Definition &definition) { - auto highlighter = new Highlighter(); - m_document->setSyntaxHighlighter(highlighter); - if (definition.isValid()) { - highlighter->setDefinition(definition); setupFromDefinition(definition); } else { q->setCodeFoldingSupported(false); } + m_document->setSyntaxHighlighterCreator([definition] { + auto highlighter = new Highlighter(); + if (definition.isValid()) + highlighter->setDefinition(definition); + return highlighter; + }); + m_document->setFontSettings(TextEditorSettings::fontSettings()); } @@ -6222,8 +6225,7 @@ void TextEditorWidget::mouseReleaseEvent(QMouseEvent *e) if (self && self->openLink(symbolLink, inNextSplit)) self->d->clearLink(); }, true, inNextSplit); - } else if (button == Qt::MiddleButton - && !isReadOnly() + } else if (button == Qt::MiddleButton && !isReadOnly() && QGuiApplication::clipboard()->supportsSelection()) { if (!(e->modifiers() & Qt::AltModifier)) doSetTextCursor(cursorForPosition(e->pos())); @@ -6302,12 +6304,10 @@ void TextEditorWidget::keyReleaseEvent(QKeyEvent *e) { if (e->key() == Qt::Key_Control) { d->clearLink(); - } else if (e->key() == Qt::Key_Shift - && d->m_behaviorSettings.m_constrainHoverTooltips - && ToolTip::isVisible()) { + } else if (e->key() == Qt::Key_Shift && d->m_behaviorSettings.m_constrainHoverTooltips + && ToolTip::isVisible()) { ToolTip::hide(); - } else if (e->key() == Qt::Key_Alt - && d->m_maybeFakeTooltipEvent) { + } else if (e->key() == Qt::Key_Alt && d->m_maybeFakeTooltipEvent) { d->m_maybeFakeTooltipEvent = false; d->processTooltipRequest(textCursor()); } @@ -6912,7 +6912,9 @@ void TextEditorWidget::findTypeAt(const QTextCursor &cursor, bool TextEditorWidget::openLink(const Utils::Link &link, bool inNextSplit) { #ifdef WITH_TESTS - struct Signaller { ~Signaller() { emit EditorManager::instance()->linkOpened(); } } s; + struct Signaller { + ~Signaller() { emit EditorManager::instance()->linkOpened(); } + } s; #endif if (!link.hasValidTarget()) @@ -9442,7 +9444,7 @@ void TextEditorFactory::setEditorCreator(const EditorCreator &creator) doc->setIndenter(d->m_indenterCreator(doc->document())); if (d->m_syntaxHighlighterCreator) - doc->setSyntaxHighlighter(d->m_syntaxHighlighterCreator()); + doc->setSyntaxHighlighterCreator(d->m_syntaxHighlighterCreator); doc->setCompletionAssistProvider(d->m_completionAssistProvider ? d->m_completionAssistProvider : &basicSnippetProvider); diff --git a/src/plugins/vcsbase/vcsbaseeditor.cpp b/src/plugins/vcsbase/vcsbaseeditor.cpp index 1b6f4811369..bb89b1cd321 100644 --- a/src/plugins/vcsbase/vcsbaseeditor.cpp +++ b/src/plugins/vcsbase/vcsbaseeditor.cpp @@ -760,9 +760,11 @@ void VcsBaseEditorWidget::init() break; } if (hasDiff()) { - auto dh = new DiffAndLogHighlighter(d->m_diffFilePattern, d->m_logEntryPattern); setCodeFoldingSupported(true); - textDocument()->setSyntaxHighlighter(dh); + textDocument()->setSyntaxHighlighterCreator( + [diffFilePattern = d->m_diffFilePattern, logEntryPattern = d->m_logEntryPattern] { + return new DiffAndLogHighlighter(diffFilePattern, logEntryPattern); + }); } // override revisions display (green or red bar on the left, marking changes): setRevisionsVisible(false); @@ -1102,7 +1104,8 @@ void VcsBaseEditorWidget::slotActivateAnnotation() ah->setChangeNumbers(changes); ah->rehighlight(); } else { - textDocument()->setSyntaxHighlighter(createAnnotationHighlighter(changes)); + BaseAnnotationHighlighter *highlighter = createAnnotationHighlighter(changes); + textDocument()->setSyntaxHighlighterCreator([highlighter] { return highlighter; }); } }