Make TextEditor setup more flexible

So far it was only possible to combine TextEditorFactory, BaseTextEditor
and TextEditorWidget directly.
That TextEditorWidget is also directly a QPlainTextEdit made it
impossible to "decorate" the text editor widget with something else
without a lot of effort.

Make it possible to create a text editor factory that returns an
arbitrary widget, as long as it can be "cast" to a TextEditorWidget with
either qobject_cast or Aggregation::query. That way the TextEditorWidget
instance can be attached to the editor widget via Aggregation.

Adapt other code that accesses TextEditorWidget from editors
accordingly. Introduce a common method how to do that.

Change-Id: I72b8721f3a8a8d8281c39af75253e9c80cbe1250
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
Reviewed-by: David Schulz <david.schulz@qt.io>
This commit is contained in:
Eike Ziller
2020-02-11 14:00:09 +01:00
parent 42f02fae03
commit d43f1662d0
14 changed files with 54 additions and 37 deletions

View File

@@ -186,7 +186,7 @@ void BeautifierPluginPrivate::autoFormatOnSave(Core::IDocument *document)
const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForDocument(document); const QList<Core::IEditor *> editors = Core::DocumentModel::editorsForDocument(document);
if (editors.isEmpty()) if (editors.isEmpty())
return; return;
if (auto widget = qobject_cast<TextEditorWidget *>(editors.first()->widget())) if (auto widget = TextEditorWidget::fromEditor(editors.first()))
TextEditor::formatEditor(widget, command); TextEditor::formatEditor(widget, command);
} }
} }

View File

@@ -190,7 +190,7 @@ void BookmarksPluginPrivate::updateActions(bool enableToggle, int state)
void BookmarksPluginPrivate::editorOpened(IEditor *editor) void BookmarksPluginPrivate::editorOpened(IEditor *editor)
{ {
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) { if (auto widget = TextEditorWidget::fromEditor(editor)) {
connect(widget, &TextEditorWidget::markRequested, connect(widget, &TextEditorWidget::markRequested,
this, [this, editor](TextEditorWidget *, int line, TextMarkRequestKind kind) { this, [this, editor](TextEditorWidget *, int line, TextMarkRequestKind kind) {
if (kind == BookmarkRequest && !editor->document()->isTemporary()) if (kind == BookmarkRequest && !editor->document()->isTemporary())
@@ -204,7 +204,7 @@ void BookmarksPluginPrivate::editorOpened(IEditor *editor)
void BookmarksPluginPrivate::editorAboutToClose(IEditor *editor) void BookmarksPluginPrivate::editorAboutToClose(IEditor *editor)
{ {
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) { if (auto widget = TextEditorWidget::fromEditor(editor)) {
disconnect(widget, &TextEditorWidget::markContextMenuRequested, disconnect(widget, &TextEditorWidget::markContextMenuRequested,
this, &BookmarksPluginPrivate::requestContextMenu); this, &BookmarksPluginPrivate::requestContextMenu);
} }

View File

@@ -106,7 +106,7 @@ TextEditor::ProposalModelPtr completionResults(TextEditor::BaseTextEditor *textE
{ {
using namespace TextEditor; using namespace TextEditor;
auto textEditorWidget = qobject_cast<TextEditorWidget *>(textEditor->widget()); auto textEditorWidget = TextEditorWidget::fromEditor(textEditor);
QTC_ASSERT(textEditorWidget, return TextEditor::ProposalModelPtr()); QTC_ASSERT(textEditorWidget, return TextEditor::ProposalModelPtr());
AssistInterface *assistInterface = textEditorWidget->createAssistInterface( AssistInterface *assistInterface = textEditorWidget->createAssistInterface(
TextEditor::Completion, TextEditor::ExplicitlyInvoked); TextEditor::Completion, TextEditor::ExplicitlyInvoked);

View File

@@ -81,7 +81,7 @@ public:
m_editor = EditorManager::openEditor(fileName); m_editor = EditorManager::openEditor(fileName);
QVERIFY(m_editor); QVERIFY(m_editor);
closeEditorAtEndOfTestCase(m_editor); closeEditorAtEndOfTestCase(m_editor);
m_editorWidget = qobject_cast<TextEditorWidget *>(m_editor->widget()); m_editorWidget = TextEditorWidget::fromEditor(m_editor);
QVERIFY(m_editorWidget); QVERIFY(m_editorWidget);
m_textDocument = m_editorWidget->document(); m_textDocument = m_editorWidget->document();

View File

@@ -1835,7 +1835,7 @@ void DebuggerPluginPrivate::runScheduled()
void DebuggerPluginPrivate::editorOpened(IEditor *editor) void DebuggerPluginPrivate::editorOpened(IEditor *editor)
{ {
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) { if (auto widget = TextEditorWidget::fromEditor(editor)) {
connect(widget, &TextEditorWidget::markRequested, connect(widget, &TextEditorWidget::markRequested,
this, &DebuggerPluginPrivate::requestMark); this, &DebuggerPluginPrivate::requestMark);

View File

@@ -266,7 +266,7 @@ void DisassemblerAgentPrivate::configureMimeType()
Utils::MimeType mtype = Utils::mimeTypeForName(mimeType); Utils::MimeType mtype = Utils::mimeTypeForName(mimeType);
if (mtype.isValid()) { if (mtype.isValid()) {
foreach (IEditor *editor, DocumentModel::editorsForDocument(document)) foreach (IEditor *editor, DocumentModel::editorsForDocument(document))
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) if (auto widget = TextEditorWidget::fromEditor(editor))
widget->configureGenericHighlighter(); widget->configureGenericHighlighter();
} else { } else {
qWarning("Assembler mimetype '%s' not found.", qPrintable(mimeType)); qWarning("Assembler mimetype '%s' not found.", qPrintable(mimeType));
@@ -314,7 +314,7 @@ void DisassemblerAgent::setContentsToDocument(const DisassemblerLines &contents)
Core::Constants::K_DEFAULT_TEXT_EDITOR_ID, Core::Constants::K_DEFAULT_TEXT_EDITOR_ID,
&titlePattern); &titlePattern);
QTC_ASSERT(editor, return); QTC_ASSERT(editor, return);
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) { if (auto widget = TextEditorWidget::fromEditor(editor)) {
widget->setReadOnly(true); widget->setReadOnly(true);
widget->setRequestMarkEnabled(true); widget->setRequestMarkEnabled(true);
} }

View File

@@ -230,7 +230,7 @@ void clearExceptionSelection()
QList<QTextEdit::ExtraSelection> selections; QList<QTextEdit::ExtraSelection> selections;
foreach (IEditor *editor, DocumentModel::editorsForOpenedDocuments()) { foreach (IEditor *editor, DocumentModel::editorsForOpenedDocuments()) {
if (auto ed = qobject_cast<TextEditorWidget *>(editor->widget())) if (auto ed = TextEditorWidget::fromEditor(editor))
ed->setExtraSelections(TextEditorWidget::DebuggerExceptionSelection, selections); ed->setExtraSelections(TextEditorWidget::DebuggerExceptionSelection, selections);
} }
} }
@@ -244,7 +244,7 @@ QStringList highlightExceptionCode(int lineNumber, const QString &filePath, cons
QTextCharFormat errorFormat = fontSettings.toTextCharFormat(TextEditor::C_ERROR); QTextCharFormat errorFormat = fontSettings.toTextCharFormat(TextEditor::C_ERROR);
for (IEditor *editor : editors) { for (IEditor *editor : editors) {
auto ed = qobject_cast<TextEditorWidget *>(editor->widget()); auto ed = TextEditorWidget::fromEditor(editor);
if (!ed) if (!ed)
continue; continue;

View File

@@ -1267,7 +1267,7 @@ void FakeVimPluginPrivate::userActionTriggered(int key)
void FakeVimPluginPrivate::createRelativeNumberWidget(IEditor *editor) void FakeVimPluginPrivate::createRelativeNumberWidget(IEditor *editor)
{ {
if (auto textEditor = qobject_cast<TextEditorWidget *>(editor->widget())) { if (auto textEditor = TextEditorWidget::fromEditor(editor)) {
auto relativeNumbers = new RelativeNumbersColumn(textEditor); auto relativeNumbers = new RelativeNumbersColumn(textEditor);
connect(theFakeVimSetting(ConfigRelativeNumber), &SavedAction::valueChanged, connect(theFakeVimSetting(ConfigRelativeNumber), &SavedAction::valueChanged,
relativeNumbers, &QObject::deleteLater); relativeNumbers, &QObject::deleteLater);
@@ -1532,10 +1532,14 @@ void FakeVimPluginPrivate::editorOpened(IEditor *editor)
return; return;
// we can only handle QTextEdit and QPlainTextEdit // we can only handle QTextEdit and QPlainTextEdit
if (!qobject_cast<QTextEdit *>(widget) && !qobject_cast<QPlainTextEdit *>(widget)) if (auto edit = Aggregation::query<QTextEdit>(widget))
widget = edit;
else if (auto edit = Aggregation::query<QPlainTextEdit>(widget))
widget = edit;
else
return; return;
auto tew = qobject_cast<TextEditorWidget *>(widget); auto tew = TextEditorWidget::fromEditor(editor);
//qDebug() << "OPENING: " << editor << editor->widget() //qDebug() << "OPENING: " << editor << editor->widget()
// << "MODE: " << theFakeVimSetting(ConfigUseFakeVim)->value(); // << "MODE: " << theFakeVimSetting(ConfigUseFakeVim)->value();

View File

@@ -274,7 +274,7 @@ void EditorConfiguration::setUseGlobalSettings(bool use)
d->m_useGlobal = use; d->m_useGlobal = use;
d->m_defaultCodeStyle->setCurrentDelegate(use ? TextEditorSettings::codeStyle() : nullptr); d->m_defaultCodeStyle->setCurrentDelegate(use ? TextEditorSettings::codeStyle() : nullptr);
foreach (Core::IEditor *editor, Core::DocumentModel::editorsForOpenedDocuments()) { foreach (Core::IEditor *editor, Core::DocumentModel::editorsForOpenedDocuments()) {
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) { if (auto widget = TextEditorWidget::fromEditor(editor)) {
Project *project = SessionManager::projectForFile(editor->document()->filePath()); Project *project = SessionManager::projectForFile(editor->document()->filePath());
if (project && project->editorConfiguration() == this) if (project && project->editorConfiguration() == this)
switchSettings(widget); switchSettings(widget);

View File

@@ -132,7 +132,7 @@ TextEditorWidget *RefactoringChanges::openEditor(const QString &fileName, bool a
IEditor *editor = EditorManager::openEditorAt(fileName, line, column, Id(), flags); IEditor *editor = EditorManager::openEditorAt(fileName, line, column, Id(), flags);
if (editor) if (editor)
return qobject_cast<TextEditorWidget *>(editor->widget()); return TextEditorWidget::fromEditor(editor);
else else
return nullptr; return nullptr;
} }
@@ -163,7 +163,7 @@ RefactoringFile::RefactoringFile(const QString &fileName, const QSharedPointer<R
{ {
QList<IEditor *> editors = DocumentModel::editorsForFilePath(fileName); QList<IEditor *> editors = DocumentModel::editorsForFilePath(fileName);
if (!editors.isEmpty()) { if (!editors.isEmpty()) {
auto editorWidget = qobject_cast<TextEditorWidget *>(editors.first()->widget()); auto editorWidget = TextEditorWidget::fromEditor(editors.first());
if (editorWidget && !editorWidget->isReadOnly()) if (editorWidget && !editorWidget->isReadOnly())
m_editor = editorWidget; m_editor = editorWidget;
} }

View File

@@ -1461,8 +1461,14 @@ TextDocumentPtr TextEditorWidget::textDocumentPtr() const
TextEditorWidget *TextEditorWidget::currentTextEditorWidget() TextEditorWidget *TextEditorWidget::currentTextEditorWidget()
{ {
auto editor = qobject_cast<BaseTextEditor *>(EditorManager::currentEditor()); return fromEditor(EditorManager::currentEditor());
return editor ? editor->editorWidget() : nullptr; }
TextEditorWidget *TextEditorWidget::fromEditor(const IEditor *editor)
{
if (editor)
return Aggregation::query<TextEditorWidget>(editor->widget());
return nullptr;
} }
void TextEditorWidgetPrivate::editorContentsChange(int position, int charsRemoved, int charsAdded) void TextEditorWidgetPrivate::editorContentsChange(int position, int charsRemoved, int charsAdded)
@@ -8370,8 +8376,9 @@ QVector<BaseTextEditor *> BaseTextEditor::textEditorsForDocument(TextDocument *t
TextEditorWidget *BaseTextEditor::editorWidget() const TextEditorWidget *BaseTextEditor::editorWidget() const
{ {
QTC_ASSERT(qobject_cast<TextEditorWidget *>(m_widget.data()), return nullptr); auto textEditorWidget = TextEditorWidget::fromEditor(this);
return static_cast<TextEditorWidget *>(m_widget.data()); QTC_CHECK(textEditorWidget);
return textEditorWidget;
} }
void BaseTextEditor::setTextCursor(const QTextCursor &cursor) void BaseTextEditor::setTextCursor(const QTextCursor &cursor)
@@ -8649,10 +8656,12 @@ void TextEditorFactory::setParenthesesMatchingEnabled(bool on)
BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentPtr &document) BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentPtr &document)
{ {
TextEditorWidget *widget = m_widgetCreator(); QWidget *widget = m_widgetCreator();
widget->setMarksVisible(m_marksVisible); TextEditorWidget *textEditorWidget = Aggregation::query<TextEditorWidget>(widget);
widget->setParenthesesMatchingEnabled(m_paranthesesMatchinEnabled); QTC_ASSERT(textEditorWidget, return nullptr);
widget->setCodeFoldingSupported(m_codeFoldingSupported); textEditorWidget->setMarksVisible(m_marksVisible);
textEditorWidget->setParenthesesMatchingEnabled(m_paranthesesMatchinEnabled);
textEditorWidget->setCodeFoldingSupported(m_codeFoldingSupported);
BaseTextEditor *editor = m_editorCreator(); BaseTextEditor *editor = m_editorCreator();
editor->setDuplicateSupported(m_duplicatedSupported); editor->setDuplicateSupported(m_duplicatedSupported);
@@ -8663,25 +8672,25 @@ BaseTextEditor *TextEditorFactoryPrivate::createEditorHelper(const TextDocumentP
// Needs to go before setTextDocument as this copies the current settings. // Needs to go before setTextDocument as this copies the current settings.
if (m_autoCompleterCreator) if (m_autoCompleterCreator)
widget->setAutoCompleter(m_autoCompleterCreator()); textEditorWidget->setAutoCompleter(m_autoCompleterCreator());
widget->setTextDocument(document); textEditorWidget->setTextDocument(document);
widget->autoCompleter()->setTabSettings(document->tabSettings()); textEditorWidget->autoCompleter()->setTabSettings(document->tabSettings());
widget->d->m_hoverHandlers = m_hoverHandlers; textEditorWidget->d->m_hoverHandlers = m_hoverHandlers;
widget->d->m_codeAssistant.configure(widget); textEditorWidget->d->m_codeAssistant.configure(textEditorWidget);
widget->d->m_commentDefinition = m_commentDefinition; textEditorWidget->d->m_commentDefinition = m_commentDefinition;
QObject::connect(widget, QObject::connect(textEditorWidget,
&TextEditorWidget::activateEditor, &TextEditorWidget::activateEditor,
widget, textEditorWidget,
[editor](EditorManager::OpenEditorFlags flags) { [editor](EditorManager::OpenEditorFlags flags) {
EditorManager::activateEditor(editor, flags); EditorManager::activateEditor(editor, flags);
}); });
if (m_useGenericHighlighter) if (m_useGenericHighlighter)
widget->setupGenericHighlighter(); textEditorWidget->setupGenericHighlighter();
widget->finalizeInitialization(); textEditorWidget->finalizeInitialization();
editor->finalizeInitialization(); editor->finalizeInitialization();
return editor; return editor;
} }

View File

@@ -556,6 +556,7 @@ public:
void setContextHelpItem(const Core::HelpItem &item); void setContextHelpItem(const Core::HelpItem &item);
static TextEditorWidget *currentTextEditorWidget(); static TextEditorWidget *currentTextEditorWidget();
static TextEditorWidget *fromEditor(const Core::IEditor *editor);
protected: protected:
/*! /*!
@@ -641,7 +642,8 @@ public:
using EditorCreator = std::function<BaseTextEditor *()>; using EditorCreator = std::function<BaseTextEditor *()>;
using DocumentCreator = std::function<TextDocument *()>; using DocumentCreator = std::function<TextDocument *()>;
using EditorWidgetCreator = std::function<TextEditorWidget *()>; // editor widget must be castable (qobject_cast or Aggregate::query) to TextEditorWidget
using EditorWidgetCreator = std::function<QWidget *()>;
using SyntaxHighLighterCreator = std::function<SyntaxHighlighter *()>; using SyntaxHighLighterCreator = std::function<SyntaxHighlighter *()>;
using IndenterCreator = std::function<Indenter *(QTextDocument *)>; using IndenterCreator = std::function<Indenter *(QTextDocument *)>;
using AutoCompleterCreator = std::function<AutoCompleter *()>; using AutoCompleterCreator = std::function<AutoCompleter *()>;

View File

@@ -31,6 +31,8 @@
#include "texteditorconstants.h" #include "texteditorconstants.h"
#include "texteditorplugin.h" #include "texteditorplugin.h"
#include <aggregation/aggregate.h>
#include <coreplugin/locator/locatormanager.h> #include <coreplugin/locator/locatormanager.h>
#include <coreplugin/icore.h> #include <coreplugin/icore.h>
#include <coreplugin/editormanager/editormanager.h> #include <coreplugin/editormanager/editormanager.h>
@@ -592,7 +594,7 @@ TextEditorActionHandler::TextEditorActionHandler(Core::Id editorId,
if (resolver) if (resolver)
d->m_findTextWidget = resolver; d->m_findTextWidget = resolver;
else else
d->m_findTextWidget = [](Core::IEditor *editor) { return qobject_cast<TextEditorWidget *>(editor->widget()); }; d->m_findTextWidget = TextEditorWidget::fromEditor;
} }
TextEditorActionHandler::~TextEditorActionHandler() TextEditorActionHandler::~TextEditorActionHandler()

View File

@@ -863,7 +863,7 @@ void CallgrindToolPrivate::showParserResults(const ParseData *data)
void CallgrindToolPrivate::editorOpened(IEditor *editor) void CallgrindToolPrivate::editorOpened(IEditor *editor)
{ {
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) { if (auto widget = TextEditorWidget::fromEditor(editor)) {
connect(widget, &TextEditorWidget::markContextMenuRequested, connect(widget, &TextEditorWidget::markContextMenuRequested,
this, &CallgrindToolPrivate::requestContextMenu); this, &CallgrindToolPrivate::requestContextMenu);
} }