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 <jaroslaw.kobus@qt.io>
Reviewed-by: <github-actions-qt-creator@cristianadam.eu>
This commit is contained in:
Artem Sokolovskii
2023-05-04 14:43:37 +02:00
parent b8c2be4cf8
commit 4bfc3476f4
13 changed files with 42 additions and 30 deletions

View File

@@ -131,7 +131,8 @@ ClangFormatConfigWidget::ClangFormatConfigWidget(TextEditor::ICodeStylePreferenc
d->preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0])); d->preview->setPlainText(QLatin1String(CppEditor::Constants::DEFAULT_CODE_STYLE_SNIPPETS[0]));
d->preview->textDocument()->setIndenter(new ClangFormatIndenter(d->preview->document())); d->preview->textDocument()->setIndenter(new ClangFormatIndenter(d->preview->document()));
d->preview->textDocument()->setFontSettings(TextEditor::TextEditorSettings::fontSettings()); 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); d->preview->textDocument()->indenter()->setFileName(fileName);
using namespace Layouting; using namespace Layouting;

View File

@@ -79,7 +79,7 @@ private:
CppEditorDocument::CppEditorDocument() CppEditorDocument::CppEditorDocument()
{ {
setId(CppEditor::Constants::CPPEDITOR_ID); setId(CppEditor::Constants::CPPEDITOR_ID);
setSyntaxHighlighter(new CppHighlighter); setSyntaxHighlighterCreator([] { return new CppHighlighter(); });
ICodeStylePreferencesFactory *factory ICodeStylePreferencesFactory *factory
= TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID); = TextEditorSettings::codeStyleFactory(Constants::CPP_SETTINGS_ID);

View File

@@ -854,7 +854,7 @@ namespace Internal {
void decorateCppEditor(TextEditor::TextEditorWidget *editor) void decorateCppEditor(TextEditor::TextEditorWidget *editor)
{ {
editor->textDocument()->setSyntaxHighlighter(new CppHighlighter); editor->textDocument()->setSyntaxHighlighterCreator([] { return new CppHighlighter(); });
editor->textDocument()->setIndenter( editor->textDocument()->setIndenter(
new CppQtStyleIndenter(editor->textDocument()->document())); new CppQtStyleIndenter(editor->textDocument()->document()));
editor->setAutoCompleter(new CppAutoCompleter); editor->setAutoCompleter(new CppAutoCompleter);

View File

@@ -93,7 +93,7 @@ DescriptionEditorWidget::DescriptionEditorWidget(QWidget *parent)
context->setContext(Context(Constants::C_DIFF_EDITOR_DESCRIPTION)); context->setContext(Context(Constants::C_DIFF_EDITOR_DESCRIPTION));
ICore::addContextObject(context); ICore::addContextObject(context);
textDocument()->setSyntaxHighlighter(new SyntaxHighlighter); textDocument()->setSyntaxHighlighterCreator([] { return new SyntaxHighlighter(); });
} }
QSize DescriptionEditorWidget::sizeHint() const QSize DescriptionEditorWidget::sizeHint() const

View File

@@ -246,9 +246,11 @@ void GitEditorWidget::init()
return; return;
const QChar commentChar = gitClient().commentChar(source()); const QChar commentChar = gitClient().commentChar(source());
if (isCommitEditor) if (isCommitEditor)
textDocument()->setSyntaxHighlighter(new GitSubmitHighlighter(commentChar)); textDocument()->setSyntaxHighlighterCreator(
[commentChar] { return new GitSubmitHighlighter(commentChar); });
else if (isRebaseEditor) else if (isRebaseEditor)
textDocument()->setSyntaxHighlighter(new GitRebaseHighlighter(commentChar)); textDocument()->setSyntaxHighlighterCreator(
[commentChar] { return new GitRebaseHighlighter(commentChar); });
} }
void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk) void GitEditorWidget::addDiffActions(QMenu *menu, const DiffChunk &chunk)

View File

@@ -51,7 +51,7 @@ NimEditorFactory::NimEditorFactory()
void NimEditorFactory::decorateEditor(TextEditorWidget *editor) void NimEditorFactory::decorateEditor(TextEditorWidget *editor)
{ {
editor->textDocument()->setSyntaxHighlighter(new NimHighlighter()); editor->textDocument()->setSyntaxHighlighterCreator([] { return new NimHighlighter();});
editor->textDocument()->setIndenter(new NimIndenter(editor->textDocument()->document())); editor->textDocument()->setIndenter(new NimIndenter(editor->textDocument()->document()));
} }

View File

@@ -167,7 +167,8 @@ BindingEditorFactory::BindingEditorFactory()
void BindingEditorFactory::decorateEditor(TextEditor::TextEditorWidget *editor) 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()->setIndenter(new QmlJSEditor::Internal::Indenter(
editor->textDocument()->document())); editor->textDocument()->document()));
editor->setAutoCompleter(new QmlJSEditor::AutoCompleter); editor->setAutoCompleter(new QmlJSEditor::AutoCompleter);

View File

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

View File

@@ -820,7 +820,7 @@ QmlJSEditorDocument::QmlJSEditorDocument(Utils::Id id)
d, &Internal::QmlJSEditorDocumentPrivate::invalidateFormatterCache); d, &Internal::QmlJSEditorDocumentPrivate::invalidateFormatterCache);
connect(this, &TextEditor::TextDocument::openFinishedSuccessfully, connect(this, &TextEditor::TextDocument::openFinishedSuccessfully,
d, &Internal::QmlJSEditorDocumentPrivate::settingsChanged); 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 setCodec(QTextCodec::codecForName("UTF-8")); // qml files are defined to be utf-8
setIndenter(new Internal::Indenter(document())); setIndenter(new Internal::Indenter(document()));
} }

View File

@@ -901,11 +901,12 @@ bool TextDocument::reload(QString *errorString, ReloadFlag flag, ChangeType type
return reload(errorString); return reload(errorString);
} }
void TextDocument::setSyntaxHighlighter(SyntaxHighlighter *highlighter) void TextDocument::setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator)
{ {
if (d->m_highlighter) if (d->m_highlighter)
delete d->m_highlighter; delete d->m_highlighter;
d->m_highlighter = highlighter;
d->m_highlighter = creator();
d->m_highlighter->setParent(this); d->m_highlighter->setParent(this);
d->m_highlighter->setDocument(&d->m_document); d->m_highlighter->setDocument(&d->m_document);
} }

View File

@@ -124,7 +124,9 @@ public:
bool setPlainText(const QString &text); bool setPlainText(const QString &text);
QTextDocument *document() const; QTextDocument *document() const;
void setSyntaxHighlighter(SyntaxHighlighter *highlighter);
using SyntaxHighLighterCreator = std::function<SyntaxHighlighter *()>;
void setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator);
SyntaxHighlighter *syntaxHighlighter() const; SyntaxHighlighter *syntaxHighlighter() const;
bool reload(QString *errorString, QTextCodec *codec); bool reload(QString *errorString, QTextCodec *codec);

View File

@@ -3690,16 +3690,19 @@ void TextEditorWidgetPrivate::removeSyntaxInfoBar()
void TextEditorWidgetPrivate::configureGenericHighlighter( void TextEditorWidgetPrivate::configureGenericHighlighter(
const KSyntaxHighlighting::Definition &definition) const KSyntaxHighlighting::Definition &definition)
{ {
auto highlighter = new Highlighter();
m_document->setSyntaxHighlighter(highlighter);
if (definition.isValid()) { if (definition.isValid()) {
highlighter->setDefinition(definition);
setupFromDefinition(definition); setupFromDefinition(definition);
} else { } else {
q->setCodeFoldingSupported(false); q->setCodeFoldingSupported(false);
} }
m_document->setSyntaxHighlighterCreator([definition] {
auto highlighter = new Highlighter();
if (definition.isValid())
highlighter->setDefinition(definition);
return highlighter;
});
m_document->setFontSettings(TextEditorSettings::fontSettings()); m_document->setFontSettings(TextEditorSettings::fontSettings());
} }
@@ -6222,8 +6225,7 @@ void TextEditorWidget::mouseReleaseEvent(QMouseEvent *e)
if (self && self->openLink(symbolLink, inNextSplit)) if (self && self->openLink(symbolLink, inNextSplit))
self->d->clearLink(); self->d->clearLink();
}, true, inNextSplit); }, true, inNextSplit);
} else if (button == Qt::MiddleButton } else if (button == Qt::MiddleButton && !isReadOnly()
&& !isReadOnly()
&& QGuiApplication::clipboard()->supportsSelection()) { && QGuiApplication::clipboard()->supportsSelection()) {
if (!(e->modifiers() & Qt::AltModifier)) if (!(e->modifiers() & Qt::AltModifier))
doSetTextCursor(cursorForPosition(e->pos())); doSetTextCursor(cursorForPosition(e->pos()));
@@ -6302,12 +6304,10 @@ void TextEditorWidget::keyReleaseEvent(QKeyEvent *e)
{ {
if (e->key() == Qt::Key_Control) { if (e->key() == Qt::Key_Control) {
d->clearLink(); d->clearLink();
} else if (e->key() == Qt::Key_Shift } else if (e->key() == Qt::Key_Shift && d->m_behaviorSettings.m_constrainHoverTooltips
&& d->m_behaviorSettings.m_constrainHoverTooltips && ToolTip::isVisible()) {
&& ToolTip::isVisible()) {
ToolTip::hide(); ToolTip::hide();
} else if (e->key() == Qt::Key_Alt } else if (e->key() == Qt::Key_Alt && d->m_maybeFakeTooltipEvent) {
&& d->m_maybeFakeTooltipEvent) {
d->m_maybeFakeTooltipEvent = false; d->m_maybeFakeTooltipEvent = false;
d->processTooltipRequest(textCursor()); d->processTooltipRequest(textCursor());
} }
@@ -6912,7 +6912,9 @@ void TextEditorWidget::findTypeAt(const QTextCursor &cursor,
bool TextEditorWidget::openLink(const Utils::Link &link, bool inNextSplit) bool TextEditorWidget::openLink(const Utils::Link &link, bool inNextSplit)
{ {
#ifdef WITH_TESTS #ifdef WITH_TESTS
struct Signaller { ~Signaller() { emit EditorManager::instance()->linkOpened(); } } s; struct Signaller {
~Signaller() { emit EditorManager::instance()->linkOpened(); }
} s;
#endif #endif
if (!link.hasValidTarget()) if (!link.hasValidTarget())
@@ -9442,7 +9444,7 @@ void TextEditorFactory::setEditorCreator(const EditorCreator &creator)
doc->setIndenter(d->m_indenterCreator(doc->document())); doc->setIndenter(d->m_indenterCreator(doc->document()));
if (d->m_syntaxHighlighterCreator) if (d->m_syntaxHighlighterCreator)
doc->setSyntaxHighlighter(d->m_syntaxHighlighterCreator()); doc->setSyntaxHighlighterCreator(d->m_syntaxHighlighterCreator);
doc->setCompletionAssistProvider(d->m_completionAssistProvider ? d->m_completionAssistProvider doc->setCompletionAssistProvider(d->m_completionAssistProvider ? d->m_completionAssistProvider
: &basicSnippetProvider); : &basicSnippetProvider);

View File

@@ -760,9 +760,11 @@ void VcsBaseEditorWidget::init()
break; break;
} }
if (hasDiff()) { if (hasDiff()) {
auto dh = new DiffAndLogHighlighter(d->m_diffFilePattern, d->m_logEntryPattern);
setCodeFoldingSupported(true); 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): // override revisions display (green or red bar on the left, marking changes):
setRevisionsVisible(false); setRevisionsVisible(false);
@@ -1102,7 +1104,8 @@ void VcsBaseEditorWidget::slotActivateAnnotation()
ah->setChangeNumbers(changes); ah->setChangeNumbers(changes);
ah->rehighlight(); ah->rehighlight();
} else { } else {
textDocument()->setSyntaxHighlighter(createAnnotationHighlighter(changes)); BaseAnnotationHighlighter *highlighter = createAnnotationHighlighter(changes);
textDocument()->setSyntaxHighlighterCreator([highlighter] { return highlighter; });
} }
} }