forked from qt-creator/qt-creator
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:
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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()));
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -124,7 +124,9 @@ public:
|
||||
|
||||
bool setPlainText(const QString &text);
|
||||
QTextDocument *document() const;
|
||||
void setSyntaxHighlighter(SyntaxHighlighter *highlighter);
|
||||
|
||||
using SyntaxHighLighterCreator = std::function<SyntaxHighlighter *()>;
|
||||
void setSyntaxHighlighterCreator(const SyntaxHighLighterCreator &creator);
|
||||
SyntaxHighlighter *syntaxHighlighter() const;
|
||||
|
||||
bool reload(QString *errorString, QTextCodec *codec);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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; });
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user