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);
if (editors.isEmpty())
return;
if (auto widget = qobject_cast<TextEditorWidget *>(editors.first()->widget()))
if (auto widget = TextEditorWidget::fromEditor(editors.first()))
TextEditor::formatEditor(widget, command);
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -266,7 +266,7 @@ void DisassemblerAgentPrivate::configureMimeType()
Utils::MimeType mtype = Utils::mimeTypeForName(mimeType);
if (mtype.isValid()) {
foreach (IEditor *editor, DocumentModel::editorsForDocument(document))
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget()))
if (auto widget = TextEditorWidget::fromEditor(editor))
widget->configureGenericHighlighter();
} else {
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,
&titlePattern);
QTC_ASSERT(editor, return);
if (auto widget = qobject_cast<TextEditorWidget *>(editor->widget())) {
if (auto widget = TextEditorWidget::fromEditor(editor)) {
widget->setReadOnly(true);
widget->setRequestMarkEnabled(true);
}

View File

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

View File

@@ -1267,7 +1267,7 @@ void FakeVimPluginPrivate::userActionTriggered(int key)
void FakeVimPluginPrivate::createRelativeNumberWidget(IEditor *editor)
{
if (auto textEditor = qobject_cast<TextEditorWidget *>(editor->widget())) {
if (auto textEditor = TextEditorWidget::fromEditor(editor)) {
auto relativeNumbers = new RelativeNumbersColumn(textEditor);
connect(theFakeVimSetting(ConfigRelativeNumber), &SavedAction::valueChanged,
relativeNumbers, &QObject::deleteLater);
@@ -1532,10 +1532,14 @@ void FakeVimPluginPrivate::editorOpened(IEditor *editor)
return;
// 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;
auto tew = qobject_cast<TextEditorWidget *>(widget);
auto tew = TextEditorWidget::fromEditor(editor);
//qDebug() << "OPENING: " << editor << editor->widget()
// << "MODE: " << theFakeVimSetting(ConfigUseFakeVim)->value();

View File

@@ -274,7 +274,7 @@ void EditorConfiguration::setUseGlobalSettings(bool use)
d->m_useGlobal = use;
d->m_defaultCodeStyle->setCurrentDelegate(use ? TextEditorSettings::codeStyle() : nullptr);
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());
if (project && project->editorConfiguration() == this)
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);
if (editor)
return qobject_cast<TextEditorWidget *>(editor->widget());
return TextEditorWidget::fromEditor(editor);
else
return nullptr;
}
@@ -163,7 +163,7 @@ RefactoringFile::RefactoringFile(const QString &fileName, const QSharedPointer<R
{
QList<IEditor *> editors = DocumentModel::editorsForFilePath(fileName);
if (!editors.isEmpty()) {
auto editorWidget = qobject_cast<TextEditorWidget *>(editors.first()->widget());
auto editorWidget = TextEditorWidget::fromEditor(editors.first());
if (editorWidget && !editorWidget->isReadOnly())
m_editor = editorWidget;
}

View File

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

View File

@@ -556,6 +556,7 @@ public:
void setContextHelpItem(const Core::HelpItem &item);
static TextEditorWidget *currentTextEditorWidget();
static TextEditorWidget *fromEditor(const Core::IEditor *editor);
protected:
/*!
@@ -641,7 +642,8 @@ public:
using EditorCreator = std::function<BaseTextEditor *()>;
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 IndenterCreator = std::function<Indenter *(QTextDocument *)>;
using AutoCompleterCreator = std::function<AutoCompleter *()>;

View File

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

View File

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